0% found this document useful (0 votes)
44 views6 pages

NODE Insert (NODE) Void Inorder (NODE) Void Preorder (NODE) Void Postorder (NODE) NODE Search (NODE, Int) NODE Delete (NODE, Int)

This C program implements a menu-driven binary search tree (BST) that allows the user to: 1) Create a BST by inserting integers 2) Traverse the BST using inorder, preorder, and postorder 3) Search for an element in the BST 4) Delete an element from the BST 5) Exit the program The BST data structure and functions to implement these operations like insertion, searching, deletion, and traversal are defined.

Uploaded by

Shreevas Nandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views6 pages

NODE Insert (NODE) Void Inorder (NODE) Void Preorder (NODE) Void Postorder (NODE) NODE Search (NODE, Int) NODE Delete (NODE, Int)

This C program implements a menu-driven binary search tree (BST) that allows the user to: 1) Create a BST by inserting integers 2) Traverse the BST using inorder, preorder, and postorder 3) Search for an element in the BST 4) Delete an element from the BST 5) Exit the program The BST data structure and functions to implement these operations like insertion, searching, deletion, and traversal are defined.

Uploaded by

Shreevas Nandan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

10.

Design, Develop and Implement a menu driven Program in C for the


following operations on Binary Search Tree (BST) of Integers
a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2
b. Traverse the BST in Inorder, Preorder and Post Order
c. Search the BST for a given element (KEY) and report the
appropriate message
d. Delete an element from BST
e. Exit

#include <stdio.h>
#include <stdlib.h>

struct BST
{
int item;
struct BST *llink, *rlink;
};

typedef struct BST* NODE;

NODE insert(NODE);
void inorder(NODE);
void preorder(NODE);
void postorder(NODE);
NODE search(NODE, int);
NODE Delete(NODE, int);

void main()
{
int choice, key,n,i;
NODE root = NULL, tmp, parent;

while(1)
{
printf("\n1.Create");
printf("\n2.Traverse the Tree in Preorder, Inorder, Postorder");
printf("\n3.Search");
printf("\n4.Delete an element from the Tree");
printf("\n5.Exit");
printf("\nEnter your choice :");
scanf("%d", &choice);

switch (choice)
{
case 1: printf("\n enter the number 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 display : ");
inorder(root);
printf("\nThe Preorder display : ");
preorder(root);
printf("\nThe Postorder display : ");
postorder(root);
}
break;
case 3: printf("\nEnter Element to be searched :");
scanf("%d", &key);
tmp = search(root, key);
if(tmp == NULL)
printf("Element does not exists\n");
else
printf("\nThe element %d found", tmp->item);
break;
case 4: printf("\nEnter Element to be deleted :");
scanf("%d", &key);
root = Delete(root, key);
break;
default: exit(0);
}
}
}
/* This function is for creating a binary search tree */

NODE insert(NODE root)


{
NODE temp, cur, prev;
int item;
printf("\nEnter The Element ");
scanf("%d", &item);
temp = (NODE) malloc(sizeof(struct BST));
temp->llink = NULL;
temp->rlink = NULL;
temp->item = item;

if (root == NULL)
return temp;
prev = NULL;
cur = root;
while(cur != NULL)
{
prev = cur;
if (item < cur-> item)
cur = cur->llink;
else
cur = cur->rlink;
}
if (item < prev->item)
prev->llink = temp;
else
prev->rlink = temp;
return root;
}
/* This function displays the tree in inorder fashion */

void inorder(NODE root)


{
if (root != NULL)
{
inorder(root->llink);
printf("%d\t", root->item);
inorder(root->rlink);
}
}

/* This function displays the tree in preorder fashion */

void preorder(NODE root)


{
if (root != NULL)
{
printf("%d\t", root->item);
preorder(root->llink);
preorder(root->rlink);
}
}

/* This function displays the tree in postorder fashion */

void postorder(NODE root)


{
if (root != NULL)
{
postorder(root->llink);
postorder(root->rlink);
printf("%d\t", root->item);
}
}
NODE search(NODE root, int key)
{
NODE cur;
if(root == NULL)
return NULL;
cur = root;
while(cur != NULL)
{
if(key == cur->item)
return cur;
if(key<cur->item)
cur = cur->llink;
else
cur = cur->rlink;
}
return NULL;
}

NODE Delete(NODE root, int data)


{
NODE temp;
int min;

if (root == NULL)
{
return NULL;
}
if (data < root->item)
{ // data is in the left sub tree.
root->llink = Delete(root->llink, data);
}
else if (data > root->item)
{ // data is in the right sub tree.
root->rlink = Delete(root->rlink, data);
}
else
{
// 1: no children (leaf node)
if (root->llink == NULL && root->rlink == NULL)
{
free(root);
root = NULL;
}
// 2: one child (right)
else if (root->llink == NULL)
{
temp = root; / / save current node as a backup
root = root->rlink;
free(temp);
}
// 3: one child (left)
else if (root->rlink == NULL)
{
temp = root; // save current node as a backup
root = root->llink;
free(temp);
}
// 4: two children
else
{
min = FindMin(root->rlink); // find minimal value of right sub tree
root->item = min; // duplicate the node
root->rlink = Delete(root->rlink, min); // delete the duplicate node
}
}
return root; // parent node can update reference
}

int FindMin(NODE root)


{
if (root == NULL)
return -1;
if (root->llink != NULL)
{
return FindMin(root->llink); // left tree is smaller
}
return root->item;
}

You might also like