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

Prog 15

The document provides a C program for constructing a Binary Search Tree (BST) using dynamic memory allocation. It includes functions for inserting nodes, searching for keys, and displaying the tree using various traversal methods (inorder, preorder, postorder). The program prevents duplicate entries and presents a menu for user interaction to manage the BST.

Uploaded by

akpanaapr0206
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)
1 views6 pages

Prog 15

The document provides a C program for constructing a Binary Search Tree (BST) using dynamic memory allocation. It includes functions for inserting nodes, searching for keys, and displaying the tree using various traversal methods (inorder, preorder, postorder). The program prevents duplicate entries and presents a menu for user interaction to manage the BST.

Uploaded by

akpanaapr0206
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

15. Using dynamic memory allocation, construct a Binary Search Tree of integers.

Write C functions to do the following:


Given a KEY, Perform a search in Binary search tree. If it is found display Key
found else insert the Key in the Binary search tree.
While constructing the Binary search tree do not add any duplicate.
Display the tree using all the traversal methods.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int info;
struct node * left;
struct node * right;
};
struct node *root=NULL;
void insert(int);
void create(int);
void display(struct node *,int);
void inorder(struct node *);
void preorder(struct node *);
void postorder(struct node *);
struct node * search(struct node *,int);
void main()
{
int item,ch,i,n;
clrscr();
while(1)
{
printf("Binary Search Tree\n");
printf("1.Create\n");
printf("2.Insert\n");
printf("3.Dispaly\n");
printf("4.Inorder Traversal\n");
printf("5.Preorder Traversal\n");
printf("6.Postorder Traversal\n");
printf("7.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:printf("Enter how many elements\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter value of the node\n");
scanf("%d",&item);
create(item);
}
break;
case 2:printf("Enter an item to insert\n");
scanf("%d",&item);
if(search(root,item)!=NULL)
printf("Item found and no duplicate value alowed\n");
else
create(item);
break;
case 3:printf("Binary Search Tree\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
display(root,1);
break;
case 4:printf("Inorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
inorder(root);
break;
case 5:printf("Preorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
preorder(root);
break;
case 6:printf("Postorder Traversal\n");
if(root==NULL)
printf("Empty Binary Search Tree\n");
else
postorder(root);
break;
case 7:exit(0);
}
}
}

void create(int item)


{
struct node *newnode,*currptr,*ptr;
newnode=(struct node *)malloc(sizeof(struct node));
newnode->info=item;
newnode->left=NULL;
newnode->right=NULL;
if(root==NULL)
root=newnode;
else
{
currptr=root;
while(currptr!=NULL)
{
ptr=currptr;
currptr=(item>currptr->info)?currptr->right:currptr->left;
}
if(item<ptr->info)
ptr->left=newnode;
else
ptr->right=newnode;
}
}
void display(struct node *ptr,int level)
{
int i;
if(ptr!=NULL)
{
display(ptr->right,level+1);
for(i=0;i<level;i++)
printf(" ");
printf("%4d\n\n",ptr->info);
display(ptr->left,level+1);
}
}

void inorder(struct node *ptr)


{
if(ptr!=NULL)
{
inorder(ptr->left);
printf("%d\t",ptr->info);
inorder(ptr->right);
}
}

void preorder(struct node *ptr)


{
if(ptr!=NULL)
{
printf("%d\t",ptr->info);
preorder(ptr->left);
preorder(ptr->right);
}
}

void postorder(struct node *ptr)


{
if(ptr!=NULL)
{
postorder(ptr->left);
postorder(ptr->right);
printf("%d\t",ptr->info);
}
}

struct node * search(struct node *ptr,int item)


{
if(ptr==NULL)
return NULL;
else if(item<ptr->info)
search(ptr->left,item);
else if(item>ptr->info)
search(ptr->right,item);
else
return ptr;
}

Algorithm
[[Here, NODE contains information field info. The left field of a node points to the address of
the left child of the node. The right field of a node points to the address of the right child of
the node. Root is the address of the root node of the tree .When root=null, tree is empty.]

Step1: Start
Step2: Display menu and accept user choice
Step3: If choice=1
a. Accept the ‘item’
b. Call the function search(root,item) to check whether the item already
exists.
c. If the item is found, print “Duplicates not allowed”
Else, Call the function create(item) to insert a new element to the tree
End if
Step5: If choice=2
Call the function inorder(root) to print the elements in the tree using inorder
traversal.
End if
Step6: If choice=3
Call the function preorder(root) to print the elements in the tree using preorder
traversal.
End if
Step7: If choice=4
Call the function postorder(root) to print the elements in the tree using
postorder traversal.
End if

Step8: If choice=5
Exit the program
End if
Step97: Stop

Algorithm for create(item)

Step1: [Obtain a node from available storage.]


newnode=malloc(sizeof(struct node))
Step2:[Initialize fields of the node]
Accept item from the user.
newnode->info=item
newnode->left=null
newnode->right=null
Step3: [Is the list empty?]
If root=null then
root=newnode
[End If]
Step 4:[If the list is not empty,add the new node at the proper position]
currptr=root
while(currptr!=null)
ptr=currptr
if(item>currptr->info)
currptr=currptr->right
else
currptr=currptr->left
[Endif]
[Endwhile]
If(item<ptr->info)
ptr->left=newnode
Else
ptr->right=newnode
[Endif]
Step5: Return

Algorithm for inorder(root)

Step 1: [Traverse the left sub tree of the root in inorder recursively]
If root->left!=NULL then
Call inorder(root->left)
Step 2: [Process the root node]
Write root->info
Step 3: [Traverse the right sub tree of the root in inorder recursively]
If root->right!=NULL then
Call inorder(root->right)
Step 4: Return

Algorithm for preorder(root)


Step 1: [Process the root node]
Write root->info
Step 2: [Traverse the left sub tree of the root in preorder recursively]
If root->left!=NULL then
Call preorder(root->left)
Step 3: [Traverse the right sub tree of the root in preorder recursively]
If root->right!=NULL then
Call preorder(root->right)
Step 4: Return

Algorithm for postorder(root)

Step 1: [Traverse the left sub tree of the root in postorder recursively]
If root->left!=NULL then
Call postorder(root->left)
Step 2: [Traverse the right sub tree of the root in postorder recursively]
If root->right!=NULL then
Call postorder(root->right)
Step 3: [Process the root node]
Write root->info

Step 4: Return

Algorithm for search(root,item)

Step1: [Set the terminating condition for recusive function]


If root=NULL
Return NULL
Step 2: [If item is less than root->info, search the left sub tree of the root recursively]
If (item<root->info) then
Call search(root->left, item)
[If item is greater than root->info, search the right sub tree of the root recursively]
Else If root->right!=NULL then
Call search(root->right,item)
else
Return root
[end if]

You might also like