C Program to Construct a Binary Search Tree and perform deletion and inorder traversal

Here is source code of the C Program to construct a binary search tree and perform deletion, inorder traversal on it. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*

* C Program to Construct a Binary Search Tree and perform deletion, inorder traversal on it

*/

#include <stdio.h>

#include <stdlib.h>

struct btnode

{

int value;

struct btnode *l;

struct btnode *r;

}*root = NULL,*temp = NULL,*t2,*t1;

void delete1();

void insert();

void delete();

void inorder(struct btnode *t);

void create();

void search(struct btnode *t);

void preorder(struct btnode *t);

void postorder(struct btnode *t);

void search1(struct btnode *t,int data);

int smallest(struct btnode *t);

int largest(struct btnode *t);

int flag =1;

void main()

{

int ch;

printf("\nOPERATIONS ---");

printf("\n1 - Insert an element into tree\n");

printf("2 - Delete an element from the tree\n");

printf("3 - Inorder Traversal\n");

printf("4 - Preorder Traversal\n");

printf("5 - Postorder Traversal\n");

printf("6 - Exit\n");

while(1)

{

printf("\nEnter your choice : ");

scanf("%d",&ch);

switch(ch)

{

case1:

insert();

break;

case2:

delete();

break;

case3:

inorder(root);

break;

case4:

preorder(root);

break;

case5:

postorder(root);

break;

case6:

exit(0);

default:

printf("Wrong choice, Please enter correct choice ");

break;

}

}

}

/* To insert a node in the tree */

void insert()

{

create();

if(root == NULL)

root = temp;

else

search(root);

}

/* To create a node */

void create()

{

int data;

printf("Enter data of node to be inserted : ");

scanf("%d",&data);

temp =(struct btnode *)malloc(1*sizeof(struct btnode));

temp->value = data;

temp->l = temp->r = NULL;

}

/* Function to search the appropriate position to insert the new node */