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

Binary Search Tree Program in C Using Recursion

This C program implements binary search tree operations like creation, traversal, and deletion using recursion. It allows the user to input the number of nodes to create a binary search tree, and then insert values into the tree using recursion. The user can then choose to traverse the tree using inorder, preorder, or postorder recursion and print out the node values. Functions are defined to create new nodes, insert values into the tree, and perform the different traversal types recursively on the root node.

Uploaded by

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

Binary Search Tree Program in C Using Recursion

This C program implements binary search tree operations like creation, traversal, and deletion using recursion. It allows the user to input the number of nodes to create a binary search tree, and then insert values into the tree using recursion. The user can then choose to traverse the tree using inorder, preorder, or postorder recursion and print out the node values. Functions are defined to create new nodes, insert values into the tree, and perform the different traversal types recursively on the root node.

Uploaded by

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

5/4/2019 Binary Search Tree Program in C using Recursion

#include <stdio.h>

struct tnode
{
int data;
struct tnode *right;
struct tnode *left;
};

struct tnode *CreateBST(struct tnode *, int);


void Inorder(struct tnode *);
void Preorder(struct tnode *);
void Postorder(struct tnode *);

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;
}

struct tnode *CreateBST(struct tnode *root, int item)


{
if(root == NULL)
{
www.cprogrammingnotes.com/question/bst-recursion.html 1/2
5/4/2019 Binary Search Tree Program in C using Recursion

root = (struct tnode *)malloc(sizeof(struct tnode));


root->left = root->right = NULL;
root->data = item;
return root;
}
else
{
if(item < root->data )
root->left = CreateBST(root->left,item);
else if(item > root->data )
root->right = CreateBST(root->right,item);
else
printf(" Duplicate Element !! Not Allowed !!!");

return(root);
}
}

void Inorder(struct tnode *root)


{
if( root != NULL)
{
Inorder(root->left);
printf(" %d ",root->data);
Inorder(root->right);
}
}

void Preorder(struct tnode *root)


{
if( root != NULL)
{
printf(" %d ",root->data);
Preorder(root->left);
Preorder(root->right);
}
}

void Postorder(struct tnode *root)


{
if( root != NULL)
{
Postorder(root->left);
Postorder(root->right);
printf(" %d ",root->data);
}
}

www.cprogrammingnotes.com/question/bst-recursion.html 2/2

You might also like