0% found this document useful (0 votes)
0 views6 pages

Binary Search Tree Traversals

The document contains a C program that implements a Binary Search Tree (BST) with functionalities for creating the tree, inserting nodes, deleting nodes, and performing tree traversals (inorder, preorder, and postorder). It defines a structure for tree nodes and includes functions for searching, deleting, and finding the smallest and largest elements in the tree. The main function orchestrates the creation of the tree and demonstrates the various operations on it.

Uploaded by

prohacker5804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views6 pages

Binary Search Tree Traversals

The document contains a C program that implements a Binary Search Tree (BST) with functionalities for creating the tree, inserting nodes, deleting nodes, and performing tree traversals (inorder, preorder, and postorder). It defines a structure for tree nodes and includes functions for searching, deleting, and finding the smallest and largest elements in the tree. The main function orchestrates the creation of the tree and demonstrates the various operations on it.

Uploaded by

prohacker5804
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

/*Binary Search Tree Traversals*/

#include<stdio.h>
#include<malloc.h>
void create();
void inorder();
void preorder();
void postorder();
void insert();
void delete();
void delete1();
void search1();
int smallest();
int largest();
int flag=1;
struct node
{
int info;
struct node *lptr,*rptr;
};

struct node *root='\0',*new='\0',*t='\0',*p='\0',*t1,*t2;


int i,n,x;

main()
{
create();
printf("\npreorder\n");
preorder(root);
printf("\nInorder\n");
inorder(root);
printf("\npostorder\n");
postorder(root);
insert();
printf("\nAfter Insert\n");
printf("\npreorder\n");
preorder(root);
printf("\nInorder\n");
inorder(root);
printf("\npostorder\n");
postorder(root);
delete();
printf("\nAfter Delete\n");
printf("\npreorder\n");
preorder(root);
printf("\nInorder\n");
inorder(root);
printf("\npostorder\n");
postorder(root);
}

/* To check for the deleted node */


void delete()
{
int x;
if (root == NULL)
{
printf("No elements in a tree to delete");
return;
}
printf("Enter the data to be deleted : ");
scanf("%d",&x);
t1 = root;
t2 = root;
search1(root,x);
}

/* Search for the appropriate position to delete the node */


void search1(struct node *t, int data)
{
if ((data>t->info))
{
t1 = t;
search1(t->rptr, data);
}
else if ((data<t->info))
{
t1 = t;
search1(t->lptr, data);
}
else if ((data==t->info))
{
delete1(t);
}
}

/* To find the smallest element in the right sub tree */


int smallest(struct node *t)
{
t2 = t;
if (t->lptr != NULL)
{
t2 = t;
return(smallest(t->lptr));
}
else
return (t->info);
}

/* To find the largest element in the left sub tree */


int largest(struct node *t)
{
if (t->rptr != NULL)
{
t2 = t;
return(largest(t->rptr));
}
else
return(t->info);
}

/* To delete a node */
void delete1(struct node *t)
{
int k;

/* To delete leaf node */


if ((t->lptr == NULL)&&(t->rptr == NULL))
{
if (t1->lptr == t)
{
t1->lptr = NULL;
}
else
{
t1->rptr = NULL;
}
t = NULL;
free(t);
return;
}

/* To delete node having one left hand child */


else if ((t->rptr == NULL))
{
if (t1 == t)
{
root = t->lptr;
t1 = root;
}
else if (t1->lptr == t)
{
t1->lptr = t->lptr;

}
else
{
t1->rptr = t->lptr;
}
t = NULL;
free(t);
return;
}
/* To delete node having right hand child */
else if (t->lptr == NULL)
{
if (t1 == t)
{
root = t->rptr;
t1 = root;
}
else if (t1->rptr == t)
t1->rptr = t->rptr;
else
t1->lptr = t->rptr;
t == NULL;
free(t);
return;
}

/* To delete node having two child */


else if ((t->lptr != NULL)&&(t->rptr != NULL))
{
t2 = root;
if (t->rptr != NULL)
{
k = smallest(t->rptr);
flag = 1;
}
else
{
k =largest(t->lptr);
flag = 2;
}
search1(root, k);
t->info = k;
}
}

void insert()
{
new=(struct node*)malloc(sizeof(struct node));
printf("\nEnter insert element\n");
scanf("%d",&x);
new->info=x;
new->lptr='\0';
new->rptr='\0';
if(root==NULL)
root=new;
else
{
t=root;
while(t!='\0')
{
p=t;
if(x>t->info)
t=t->rptr;
else
t=t->lptr;
}
if(x>p->info)
p->rptr=new;
else
p->lptr=new;
}
}

void create()
{
printf("\nEnter How Many Nodes:\n");
scanf("%d",&n);
for(i=1;i<=n;i++)
{
new=(struct node*)malloc(sizeof(struct node));
scanf("%d",&x);
new->info=x;
new->lptr='\0';
new->rptr='\0';
if(root==NULL)
root=new;
else
{
t=root;
while(t!='\0')
{
p=t;
if(x>t->info)
t=t->rptr;
else
t=t->lptr;
}
if(x>p->info)
p->rptr=new;
else
p->lptr=new;
}
}
}

void inorder(struct node *t)


{
if(t!='\0')
{
inorder(t->lptr);
printf("%3d",t->info);
inorder(t->rptr);
}
}

void preorder(struct node *t)


{
if(t!='\0')
{
printf("%3d",t->info);
preorder(t->lptr);
preorder(t->rptr);
}
}
void postorder(struct node *t)
{
if(t!='\0')
{
postorder(t->lptr);
postorder(t->rptr);
printf("%3d",t->info);
}
}

You might also like