Binary Search Tree (Insertion, Deletion, Display) Menu card

Write a Modular C Programming code for Binary Search Tree (Insertion, Deletion, Display) Menu card System DSA

What is Binary Search Tree?

Binary Search Tree is a node-based binary tree data structure which has the following properties:

• The left subtree of a node contains only nodes with keys lesser than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• The left and right subtree each must also be a binary search tree.

CODE:

```#include<stdio.h>

struct node {
int key;
struct node *left, *right;
};

struct node* newNode(int item)
{
struct node* temp
= (struct node*)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct node* root)
{
if (root != NULL) {
inorder(root->left);
printf("%d->", root->key);
inorder(root->right);
}
}

struct node* insert(struct node* node, int key)
{

if (node == NULL)
return newNode(key);

if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);

return node;
}

struct node* minValueNode(struct node* node)
{
struct node* current = node;

while (current && current->left != NULL)
current = current->left;

return current;
}

struct node* deleteNode(struct node* root, int key)
{
if (root == NULL)
return root;

if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);

else {
if (root->left == NULL) {
struct node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL) {
struct node* temp = root->left;
free(root);
return temp;
}
struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);
}
return root;
}

int main()
{
int key,no,del,ch;
struct node* root = NULL;

while(1)
{
printf("\n\n+++++++++++++++++++++++++++++++++++++++++++++\n");
printf("1) Insert Data\n");
printf("2) Delete Data\n");
printf("3) Exit\n");
printf("+++++++++++++++++++++++++++++++++++++++++++++\n");

scanf("%d",&ch);
switch(ch)
{

case 1: printf("Enter the DATA of node: ");
scanf("%d",&key);
root = insert(root, key);
printf("\nLatest Inorder traversal of the given tree \n");
inorder(root);
break;
case 2: printf("Enter the node data to DELETE: ");
scanf("%d",&del);
root = deleteNode(root, del);
printf("\nLatest Inorder traversal of the given tree \n");
inorder(root);
break;
case 3: exit(0);break;
default: printf("Invalid choice\n");
}
}
}

```

OUTPUT

```+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the DATA of node: 12

Latest Inorder traversal of the given tree
12->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the DATA of node: 45

Latest Inorder traversal of the given tree
12->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the DATA of node: 1

Latest Inorder traversal of the given tree
1->12->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the DATA of node: 3

Latest Inorder traversal of the given tree
1->3->12->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the DATA of node: 10

Latest Inorder traversal of the given tree
1->3->10->12->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the node data to DELETE: 10

Latest Inorder traversal of the given tree
1->3->12->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++
Enter the node data to DELETE: 12

Latest Inorder traversal of the given tree
1->3->45->

+++++++++++++++++++++++++++++++++++++++++++++
1) Insert Data
2) Delete Data
3) Exit
+++++++++++++++++++++++++++++++++++++++++++++

Process returned 0 (0x0)   execution time : 50.367 s
Press any key to continue.```

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University