0% found this document useful (0 votes)
2 views

DSA Program 10

The document outlines a C program for a menu-driven Binary Search Tree (BST) implementation that supports operations such as insertion, traversal (inorder, preorder, postorder), and searching for an element. It includes code for both a BST without duplicate nodes and one that allows duplicates, detailing the structure and functions necessary for each operation. The program continuously prompts the user for actions until they choose to exit.

Uploaded by

sagarsriram535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

DSA Program 10

The document outlines a C program for a menu-driven Binary Search Tree (BST) implementation that supports operations such as insertion, traversal (inorder, preorder, postorder), and searching for an element. It includes code for both a BST without duplicate nodes and one that allows duplicates, detailing the structure and functions necessary for each operation. The program continuously prompts the user for actions until they choose to exit.

Uploaded by

sagarsriram535
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Laboratory Program – 10

Develop a Program in C for the following:


a) Design, Develop and Implement a menu driven Program in C for the following
operations on Binary Search Tree (BST) of Integers.
b) Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2.
c) Traverse the BST in Inorder, Preorder and Postorder..
d) Search the BST for a given element (KEY) and report the appropriate message.
e) Exit

C Program: Without Duplicate Node

#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *lchild;
struct node *rchild;
};

typedef struct node *nodepointer;

nodepointer getnode()
{
nodepointer x;
x=(nodepointer)malloc(sizeof(struct node));
return x;
}

nodepointer insert(int item, nodepointer root)


{
nodepointer temp,cur,prev;
temp=getnode();
temp->info=item;
temp->lchild=temp->rchild=NULL;
if(root==NULL)
{
root=temp;
return root;
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(temp->info < cur->info)
cur=cur->lchild;
else if(temp->info > cur->info)
cur=cur->rchild;
else
{
printf("Duplicate Node\n");
return root;
}
}
if(temp->info > prev->info)
prev->rchild=temp;
else
prev->lchild=temp;
return root;

}
}

void preorder(nodepointer root)


{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
}

void inorder(nodepointer root)


{
if(root!=NULL)
{
inorder(root->lchild);
printf("%d\t",root->info);
inorder(root->rchild);
}
}

void postorder(nodepointer root)


{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d\t",root->info);
}
}

void traversal(nodepointer root)


{
if(root==NULL)
{
printf("The tree is empty\n");
return;
}
printf("\nThe preorder traversal is\n");
preorder(root);
printf("\nThe inorder traversal is\n");
inorder(root);
printf("\nThe postorder traversal is\n");
postorder(root);
}

void search(nodepointer root)


{
int key;
nodepointer cur;
printf("Enter the key element to be searched\n");
scanf("%d",&key);
if(root==NULL)
{
printf("The tree is empty\n");
return;
}
cur=root;
while(cur!=NULL)
{
if(key==cur->info)
{
printf("The key element %d is found in tree\n",key);
return;
}
if(key<cur->info)
cur=cur->lchild;
else
cur=cur->rchild;
}
printf("The key element %d is not found in the tree\n",key);
}
int main()
{
int ch,item;
nodepointer root=NULL;
while(1)
{
printf("\nBinary Search Tree Operations\n1.Insert\t
2.Traversal\t3.Search\t4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2: traversal(root);
break;
case 3: search(root);
break;
case 4: exit(0);
default:printf("enter a valid choice\n");
}
}
return 0;
}

OUTPUTS:
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
10
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
7
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
11
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
14
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
15
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
12
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
6
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
8
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
2
The preorder traversal is
10 7 6 8 11 14 12 15
The inorder traversal is
6 7 8 10 11 12 14 15
The postorder traversal is
6 8 7 12 15 14 11 10
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
3
Enter the key element to be searched
15
The key element 15 is found in tree
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
3
Enter the key element to be searched
5
The key element 5 is not found in tree
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
3
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
5
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
1
Enter the item to be inserted
9
Binary Search Tree Operations
1. Insert 2. Traversal 3. Search 4. Exit
Enter your choice
2
The preorder traversal is
10 7 6 3 5 8 9 11 14 12 15
The inorder traversal is
3 5 6 7 8 9 10 11 12 14 15
The postorder traversal is
5 3 6 9 8 7 12 15 14 11 10
C Program: With Duplicate Node
#include<stdio.h>
#include<stdlib.h>

struct node
{
int info;
struct node *lchild;
struct node *rchild;
};

typedef struct node *nodepointer;

nodepointer getnode()
{
nodepointer x;
x=(nodepointer)malloc(sizeof(struct node));
return x;
}

nodepointer insert(int item, nodepointer root)


{
nodepointer temp,cur,prev;
temp=getnode();
temp->info=item;
temp->lchild=temp->rchild=NULL;
if(root==NULL)
{
root=temp;
return root;
}
else
{
prev=NULL;
cur=root;
while(cur!=NULL)
{
prev=cur;
if(temp->info < cur->info)
cur=cur->lchild;
else if(temp->info >= cur->info)
cur=cur->rchild;
}
if(temp->info >= prev->info)
prev->rchild=temp;
else
prev->lchild=temp;
return root;

}
}

void preorder(nodepointer root)


{
if(root!=NULL)
{
printf("%d\t",root->info);
preorder(root->lchild);
preorder(root->rchild);
}
}

void inorder(nodepointer root)


{
if(root!=NULL)
{
inorder(root->lchild);
printf("%d\t",root->info);
inorder(root->rchild);
}
}

void postorder(nodepointer root)


{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
printf("%d\t",root->info);
}
}
void traversal(nodepointer root)
{
if(root==NULL)
{
printf("The tree is empty\n");
return;
}
printf("\nThe preorder traversal is\n");
preorder(root);
printf("\nThe inorder traversal is\n");
inorder(root);
printf("\nThe postorder traversal is\n");
postorder(root);
}

void search(nodepointer root)


{
int key;
nodepointer cur;
printf("Enter the key element to be searched\n");
scanf("%d",&key);
if(root==NULL)
{
printf("The tree is empty\n");
return;
}
cur=root;
while(cur!=NULL)
{
if(key==cur->info)
{
printf("The key element %d is found in tree\n",key);
return;
}
if(key<cur->info)
cur=cur->lchild;
else
cur=cur->rchild;
}
printf("The key element %d is not found in the tree\n",key);
}

int main()
{
int ch,item;
nodepointer root=NULL;
while(1)
{
printf("\nBinary Search Tree Operations\n1.Insert\t
2.Traversal\t3.Search\t4.Exit\n");
printf("Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the item to be inserted\n");
scanf("%d",&item);
root=insert(item,root);
break;
case 2: traversal(root);
break;
case 3: search(root);
break;
case 4: exit(0);
default:printf("enter a valid choice\n");
}
}
return 0;
}

You might also like