Binary Search Tree Program in C Using Recursion
Binary Search Tree Program in C Using Recursion
#include <stdio.h>
struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};
int main()
{
struct tnode *root = NULL;
int choice, item, n, i;
do
{
printf("\n\nBinary Search Tree Operations\n");
printf("\n1. Creation of BST");
printf("\n2. Traverse in Inorder");
printf("\n3. Traverse in Preorder");
printf("\n4. Traverse in Postorder");
printf("\n5. Exit\n");
printf("\nEnter Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
root = NULL;
printf("\n\nBST for How Many Nodes ? ");
scanf("%d",&n);
for(i = 1; i <= n; i++)
{
printf("\nEnter data for node %d : ", i);
scanf("%d",&item);
root = CreateBST(root,item);
}
printf("\nBST with %d nodes is ready to Use!!\n", n);
break;
case 2:
printf("\nBST Traversal in INORDER \n");
Inorder(root);
break;
case 3:
printf("\nBST Traversal in PREORDER \n");
Preorder(root);
break;
case 4:
printf("\nBST Traversal in POSTORDER \n");
Postorder(root);
break;
case 5:
printf("\n\n Terminating \n\n");
break;
default:
printf("\n\nInvalid Option !!! Try Again !! \n\n");
break;
}
} while(choice != 5);
return 0;
}
return(root);
}
}
www.cprogrammingnotes.com/question/bst-recursion.html 2/2