DSA Program 10
DSA Program 10
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *lchild;
struct node *rchild;
};
nodepointer getnode()
{
nodepointer x;
x=(nodepointer)malloc(sizeof(struct node));
return x;
}
}
}
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;
};
nodepointer getnode()
{
nodepointer x;
x=(nodepointer)malloc(sizeof(struct node));
return x;
}
}
}
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;
}