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

Unit 4 Program

This program implements functions to create, insert, search, and traverse nodes in a binary search tree (BST). The main function allows the user to choose between creating and inserting nodes, searching for a node, or displaying the tree using preorder, inorder, or postorder traversal. Node structures contain data and left/right child pointers. Functions are included to create and insert nodes, search by value, and traverse the tree using the three ordering methods.

Uploaded by

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

Unit 4 Program

This program implements functions to create, insert, search, and traverse nodes in a binary search tree (BST). The main function allows the user to choose between creating and inserting nodes, searching for a node, or displaying the tree using preorder, inorder, or postorder traversal. Node structures contain data and left/right child pointers. Functions are included to create and insert nodes, search by value, and traverse the tree using the three ordering methods.

Uploaded by

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

/*program for Binary Search Tree*/

#include<stdio.h>
#include<conio.h>
typedef struct bst
{
int info;
struct bst *left;
struct bst *right;
}node;
node *create();
void insert(node *,node *);
void search(node *,int);
void del(node *);
void preorder(node *);
void inorder(node *);
void postorder(node *);
void main()
{
int c=1,k=1,choice,item;
node *root=NULL,*temp;
clrscr();
while(c==1)
{
printf("\n1. Create \n2. Search \n3. Delete \n4. Diplay");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
do
{
temp=create();
if(root==NULL)
root=temp;
else
insert(root,temp);
printf("\nContinue 1 for yes:");
scanf("%d",&k);
}while(k==1);
break;

case 2:
printf("\nEnter item to search:");
scanf("%d",&item);
search(root,item);
break;

case 4:
printf("\nPreorder Traversal :");
preorder(root);
printf("\nInorder Traversal :");
inorder(root);
printf("\nPostorder Traversal :");
postorder(root);
break;
}
printf("\nContinue next operation 1 for yes:");
scanf("%d",&c);
}
}
//create function
node *create()
{
node *temp;
printf("\nEnter data:");
temp=(node *)malloc(sizeof(node));
scanf("%d",&temp->info);
temp->left=temp->right=NULL;
return temp;
}

//insert function
void insert(node *root,node *temp)
{
if(temp->info<root->info)
{
if(root->left!=NULL)
insert(root->left,temp);
else
root->left=temp;
}
if(temp->info>root->info)
{
if(root->right!=NULL)
insert(root->right,temp);
else
root->right=temp;
}
}
/*search function*/
void search(node *root,int item)
{
if(root==NULL)
printf("\nSearch unsuccessful");
else
{
if(root->info==item)
{
printf("\nSearch successful:");
}
else
{
if(item<root->info)
search(root->left,item);
else
if(item>root->info)
search(root->right,item);
}
}

}
/*preorder traversal*/
void preorder(node *root)
{
if(root!=NULL) 200
{ 0
printf(" %d ",root->info); 150 H 350
preorder(root->left);
preorder(root->right);
} 400 E 450 600 M 700
}

200 C 250 0 F 0 0 L 0 0 S 0

0 A 0 0 D 0

preorder(200)
preorder(200->Left) i.e. preorder(150) ---preorder(200->Right)--Pending In Stack-
preorder(150->Left) i.e. preorder(400) ---preorder(150->Right)--Pending In Stack-
preorder(400->Left) i.e. preorder(200) ---preorder(400->Right)--Pending In Stack-
preorder(200->Left) i.e. preorder(NULL)---preorder(200->Right)--Pending In
Stack-

After NULL is passed it would return. And the next element pending in stack
would be executed.
/*inorder Traversal*/
void inorder(node *root)
{
if(root!=NULL)
{
inorder(root->left);
printf(" %d ",root->info);
inorder(root->right);
}
}

/*postorder traversal*/
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->left);
postorder(root->right);
printf(" %d ",root->info);
}
}

You might also like