Sample
Sample
h>
void ins(int x)
current_node=current_node+1;
int temp=current_node;
heap[temp]=heap[temp/2];
temp=temp/2;
heap[temp]=x;
void display()
int i;
printf("Heap: ");
for(i =1;i<=current_node;i++)
printf("\n%d",heap[i]);
printf("\n");
void delete()
{
int n=current_node;
int temp=heap[n];
heap[1]=temp;
current_node--;
int parent=1;
int child=2;
while(child<=current_node)
if(child+1<=current_node&&heap[child]<heap[child+1])
child=child+1;
if(heap[parent]>=heap[child])
break;
temp = heap[parent];
heap[parent]=heap[child];
heap[child]=temp;
parent=child;
child=child*2;
int main()
int choice,x,root=heap[1];;
while(1)
{
printf("1.insert\n");
printf("2.display\n");
printf("3.delete\n");
printf("4.exit\n");
scanf("%d",&choice);
switch(choice)
case 1:
scanf("%d",&x);
ins(x);
break;
case 2:
display();
break;
case 3:
if (current_node>0)
delete();
} else
printf("heap is empty\n");
break;
case 4:
return 0;
default:
printf("invalid choice.\n");
return 0;
2. #include <stdio.h>
#include <stdlib.h>
struct node {
int key;
};
node->key = key;
return node;
if (node == NULL) {
return newnode(key);
return node;
current = current->left;
return current;
if (root == NULL) {
return root;
} else {
if (root->left == NULL) {
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
if (root != NULL) {
inorder(root->left);
inorder(root->right);
int main() {
while (1) {
scanf("%d", &choice);
switch (choice) {
case 1:
break;
case 2:
scanf("%d", &value);
break;
case 3:
inorder(root);
printf("\n");
break;
case 4:
exit(0);
default:
return 0;
3.
#include<stdio.h>
#include<stdlib.h>
struct node
int data;
int min;
newnode->data=item;
newnode->left=0;
newnode->right=0;
if(root==NULL)
root=newnode;
return;
else
temp=root;
while(temp!=NULL)
parent=temp;
if(item<temp->data)
temp=temp->left;
else{
temp=temp->right;
if(item<parent->data)
{
parent->left=newnode;
else{
parent->right=newnode;
temp=root;
while(temp!=NULL)
if(key==temp->data)
break;
else if(key<temp->data)
parent=temp;
temp=temp->left;
else
parent=temp;
temp=temp->right;
}
if(temp==NULL)
if(parent==NULL)
root=NULL;
else if (key<parent->data)
parent->left=NULL;
else
parent->right=NULL;
if(parent==NULL)
root=temp->right;
else if (key<parent->data)
parent->left=temp->right;
else
parent->right=temp->right;
if(parent==NULL)
root=temp->left;
else if (key<parent->data)
parent->left=temp->left;
else
parent->right=temp->left;
temp1=temp->right;
min=temp1->data;
while(temp1->left!=NULL)
parent1=temp1;
temp1=temp1->left;
min=temp->data;
if (min==temp->right->data)
temp->data=min;
temp->right=temp->right->right;
else
temp->data=min;
parent->left=temp->right;
if (root == NULL)
return;
printf("%d ", root->data);
print(root->left);
print(root->right);
if (key == temp->data)
return 1;
temp = temp->left;
else
temp = temp->right;
int main()
int n,i,item,ch,del,key;
while(1)
{
printf("\nOPERATION PERFORMED");
printf("\n1.INSERTION");
printf("\n2.DELETION");
printf("\n3.SEARCH");
printf("\n4.PRINT");
printf("\n5.EXIT");
scanf("%d",&ch);
if(ch==1)
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&item);
insertion(item);
else if(ch==2)
scanf("%d",&del);
deletion(del);
else if(ch==3)
{
printf("ENTER THE ELEMENT TO BE SEARCHED: ");
scanf("%d", &key);
if (search(key))
else
else if(ch==4)
print(root);
else
break;
4. #include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
int height;
};
node->key = key;
node->height = 1;
return node;
if (node == NULL) {
return newNode(key);
} else {
return node;
}
node->height = 1 + (height(node->left) > height(node->right) ? height(node->left) : height(node-
>right));
return node;
if (root == NULL) {
return 1;
return (balance >= -1 && balance <= 1) && isAVL(root->left) && isAVL(root->right);
if (root) {
traverseAndPrint(root->left);
traverseAndPrint(root->right);
int main() {
int n, value;
scanf("%d", &n);
scanf("%d", &value);
traverseAndPrint(root);
if (isAVL(root)) {
} else {
return 0;
5.
#include <stdio.h>
#include <stdlib.h>
struct Node {
int key;
int height;
};
node->height = 1;
return node;
if (node == NULL) {
return newNode(key);
} else {
return node;
return node;
}
if (root) {
displayBalancedNodes(root->left);
if (balance == 0 || balance == 1) {
displayBalancedNodes(root->right);
int main() {
int n, value;
scanf("%d", &n);
scanf("%d", &value);
displayBalancedNodes(root);
return 0;
}