WAP Write the following menu driven program for the binary search tree
----------------------------------------
Binary Search Tree Menu
----------------------------------------
1. Create
2. In-Order Traversal
3. Pre-Order Traversal
4. Post-Order traversal
5. Search
6. Find Smallest Element
7. Find Largest Element
8. Deletion of Tree
9. Quit
#include <stdio.h>
#include <stdlib.h>
struct node {
int k;
struct node *l, *r;
};
struct node* newNode(int x)
{
struct node* tmp= (struct node*)malloc(sizeof(struct node));
tmp->k = x;
tmp->l = tmp->r = NULL;
return tmp;
}
void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->l);
printf("%d \n", root->k);
inorder(root->r);
}
}
struct node* insert(struct node* node, int k)
{
if (node == NULL)
return newNode(k);
if (k < node->k)
node->l = insert(node->l, k);
else if (k > node->k)
node->r = insert(node->r, k);
return node;
}
int main()
{
struct node* root = NULL;
root = insert(root, 40);
insert(root, 10);
insert(root, 20);
insert(root, 30);
insert(root, 50);
insert(root, 60);
insert(root, 70);
inorder(root);
return 0;
}
Comments
Leave a comment