You are on page 1of 6

#include<stdio.

h>
#include<stdlib.h>
typedef struct node
{
int MIS;
char name[100];
struct node*left;
struct node*right;
struct node*parent;
}node;
typedef struct node1
{
struct node1*next;
struct node*data;
}node1;
node1*top = NULL;
void push(node*r)
{
node1*n = malloc(sizeof(node1));
n->data = r;
n->next = NULL;
if(top==NULL)
{
top=n;
return ;
}
n->next = top;
top = n;
return ;
}
node*pop()
{
node1*temp = top;
top = top->next;
return temp->data;
}
void initBST(node**r)
{
*r = NULL;
return ;
}
int stack_empty()
{
if(top==NULL)
return 1;
else
return 0;
}
node*newnode(int c ,node*r,char n1[100])
{
node*n = malloc(sizeof(node));
n->MIS = c;
n->left = n->right = NULL;
n->parent = r;
strcpy(n->name,n1);
return n;
}
void postorder_rec(node*r)
{
if(r==NULL)
return ;
postorder_rec(r->left);
postorder_rec(r->right);
printf(" Data :%d (Root: %s) ",r->MIS,r->name);
}
node*previous = NULL;
node*insert(node*r,int c,char n[100])
{
if(r==NULL)
return newnode(c,previous,n);
else if(r->MIS >c)
{
previous = r;
r->left = insert(r->left ,c,n);
}
else if(r->MIS<c)
{
previous = r;
r->right = insert(r->right,c,n);
}
return r;
}

void inorder(node*r)
{
if(r==NULL)
return ;
inorder(r->left);
printf(" Data : %d (Root name : %s )",r->MIS,r->name);
inorder(r->right);
}

node*ipre(node*t)//in-order predecessor
{
node*r = t;
r=r->left ;
while(r->right!=NULL)
r=r->right;
if(r->left!=NULL)
{
if(r->parent->MIS>r->MIS)
{
r->parent->left = r->left;
}
else
r->parent->right = r->left;
}
else if(r->left==NULL)
{
if(r->MIS<r->parent->MIS)
r->parent->left = NULL;
else
r->parent->right = NULL;

}
return r;
}
void del_nonrec(node*r,int c )
{
node*curr = r;
while(curr->MIS!=c && curr!=NULL)
{
if(curr->MIS >c)
curr = curr->left;
else
curr = curr->right;
}
if(curr==NULL)
{
printf("Node not found !! ");
return ;
}
else if(curr->left!=NULL && curr->right==NULL)
{
if(curr->parent->MIS>curr->MIS)
{
curr->parent->left = curr->left;
free(curr);
}
else
{
curr->parent->right=curr->left;
}
}
else if(curr->left == NULL && curr->right!=NULL)
{
if(curr->parent->MIS > curr->MIS)
{
curr->parent->left = curr->right;
free(curr);
}
else
{
curr->parent->right=curr->right;
}
}
else if(curr->right==NULL && curr->left==NULL)
{
if(curr->MIS<curr->parent->MIS)
{
curr->parent->left = NULL;
free(curr);
}
else
{
curr->parent->right = NULL;
free(curr);
}

}
else if(curr->right!=NULL && curr->left!=NULL)
{
node*temp=ipre(curr);
curr->MIS = temp->MIS;
}
}
void search(node*r,int c )
{
if(r==NULL)
{printf("Node not found in the tree!!");
return ;}
else if(r->MIS==c)
{
printf("Node is found");
return ;
}
else if(r->MIS > c)
search(r->left,c);
else
search(r->right,c);

}
node* del_rec(node*r,int c)
{
if(r==NULL)
return NULL;
else if(r->left==NULL && r->right==NULL)
{
free(r);
return NULL;
}
else if(r->MIS >c)
r->left = del_rec(r->left,c);
else if(r->MIS<c)
r->right = del_rec(r->right,c);
else
{
node*temp = ipre(r);
r->MIS = temp->MIS ;
r->left = del_rec(r->left,temp->MIS);
}

}
void postorder(node *root)
{
node *ptr = root;
node *q;

if( ptr == NULL )


{
printf("Tree is empty\n");
return;
}
q = root;
while(1)
{
while(ptr->left!=NULL)
{
push(ptr);
ptr=ptr->left;
}

while( ptr->right==NULL || ptr->right==q )


{
printf(" Data :%d (Root : %s) ",ptr->MIS,ptr->name);
q = ptr;
if( stack_empty() )
return;
ptr = pop();
}
push(ptr);
ptr = ptr->right;
}
printf("\n");
}
void all_nodes(node*r ,int curr , int level)
{
if(r==NULL)
return ;
if (curr==level)
{
printf("Data :%d(Root : %s) ",r->MIS,r->name);
return;
}
all_nodes(r->left,curr+1,level);
all_nodes(r->right,curr+1,level);
}
void freeBST(node*r)
{
if(r==NULL)
return NULL;
freeBST(r->left);
freeBST(r->right);
free(r);
}
int main()
{
int c,d,s,l;
node*prev = NULL;
node*root;
initBST(&root);
root = insert(root,23,"Pradnya");
root = insert(root,10,"Rutuja");
root = insert(root,12,"Vaishanvi");
root = insert(root,45,"Parya");
root = insert(root,60,"Kalu");
root = insert(root,100,"chiku");
root = insert(root,18,"mammi");
root = insert(root,4,"Pappa");
root = insert(root,3,"Manu");
root = insert(root,5,"Renu");
root = insert(root,30,"BTS");
root = insert(root,11,"Jimin");
root = insert(root,1,"JK");
printf("In - order traversal is :");
inorder(root);
printf("\nNon-recursive deletion :\n");
printf("Enter the node to delete :");
scanf("%d",&c);
del_nonrec(root,c);
printf("\nAfter deleting the node the inorder traversal is :");
inorder(root);
printf("\nRecursive deletion :\n");
printf("Enter the node to delete :");
scanf("%d",&d);
del_rec(root,d);
printf("\nAfter deleting the node the inorder traversal is :\n");
inorder(root);
printf("\nEnter element to search:");
scanf("%d",&s);
search(root,s);
printf("Non-Recursive Postorder Traversal is : ");
postorder(root);
/*printf("\n Recursive Postorder Traversal is :");
postorder_rec(root);*/
printf("Enter the level at which you want to print the nodes :");
scanf("%d",&l);
all_nodes(root,0,l);
freeBST(root);

You might also like