BST Lab 10
BST Lab 10
h>
#include<stdlib.h>
struct bst
{ int item;
struct bst *lptr, *rptr;
};
typedef struct bst node;
if (root == NULL)
return new1;
while(cur != NULL)
{ prev = cur;
cur = new1->item < cur->item ? cur->lptr : cur->rptr;
}
int main()
{ int choice, key,n,i;
node *root = NULL, *temp;
while(1)
{ printf("\n 1.Create");
printf("\n 2.Traverse the Tree in Pre, In and Post");
printf("\n 3.Search");
printf("\n 4.Exit");
printf("\nEnter your choice :"); scanf("%d",&choice);
switch (choice)
{ case 1: printf("\n enter no. of nodes");
scanf("%d",&n);
for(i=0;i<n;i++)
root = insert(root); break;
case 2: if (root == NULL)
printf("Tree Is Not Created");
else
{ printf("\nThe Inorder Traversal : ");
inorder(root);
printf("\nThe Preorder Traversal: ");
preorder(root);
printf("\nThe Postorder Traversal : ");
postorder(root);
} break;
case 3: printf("\nEnter Element to be searched :");
scanf("%d",&key);
temp = search(root,key);
if(temp == NULL)
printf("Element does not exists\n");
else
printf("\nThe element %d found",temp->item);
break;
default: exit(0);
} } }