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

BST_INSERT

Uploaded by

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

BST_INSERT

Uploaded by

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

#include <stdio.

h>
#include <stdlib.h>

// structure of a node
struct node
{
int data;
struct node *left;
struct node *right;
};

// globally initialized root pointer


struct node *root = NULL;
int data;
// creates a new node
struct node *create_node(int data)
{
struct node *new_node = (struct node *)malloc(sizeof(struct node));
new_node->data = data;
new_node->left = NULL;
new_node->right = NULL;

return new_node;
}

// inserts the data in the BST


void insert(int data)
{
struct node *new_node = create_node(data);

if (new_node != NULL)
{
// if the root is empty then make a new node as the root node
if (root == NULL)
{
root = new_node;
printf("\n Node having data %d is inserted\n", data);
return;
}

struct node *temp = root;


struct node *prev = NULL;

// traverse through the BST to get the correct position for insertion
while (temp != NULL)
{
prev = temp;
if (data > temp->data)
{
temp = temp->right;
}
else
{
temp = temp->left;
}
}

// found the last node where the new node should insert
if (data > prev->data)
{
prev->right = new_node;
}
else
{
prev->left = new_node;
}

printf("\n Node having data %d is inserted\n", data);


}
}

// finds the node with the smallest value in BST


struct node *smallest_node(struct node *root)
{
struct node *curr = root;
while (curr != NULL && curr->left != NULL)
{
curr = curr->left;
}
return curr;
}

// deletes the given key node from the BST


struct node *delete (struct node *root, int key)
{
if (key < root->data)
root->left = delete (root->left, key);

else if (key > root->data)


root->right = delete (root->right, key);

else
{
//leaf node
if(root->left == NULL && root->right == NULL)
{
root=NULL;
free(root);
}
//right child
else if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
//left child
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
struct node *temp = smallest_node(root->right);
root->data = temp->data;
root->right = delete (root->right, temp->data);
}
return root;
}
// search the given key node in BST
int search(int key)
{
struct node *temp = root;
while (temp != NULL)
{
if (key == temp->data)
return 1;
else if (key > temp->data)
temp = temp->right;
else
temp = temp->left;

}
return 0;
}

// inorder traversal of the BST


void inorder(struct node *root)
{
if (root == NULL)
return;

inorder(root->left);
printf("%d ", root->data);
inorder(root->right);
}

int main()
{
int choice;
struct node* result = NULL;

while (1)
{
printf("\n\n------- Binary Search Tree ------\n");
printf("\n1. Insert");
printf("\n2. Delete");
printf("\n3. Search");

printf("\n4. Display ");

printf("\n5. Exit");

printf("\n\nEnter Your Choice: ");


scanf("%d", &choice);
printf("\n");

switch(choice)
{
case 1:
printf("\nEnter Data to be inserted : ");
scanf("%d", &data);
insert(data);
break;

case 2:
if (root == NULL)
printf("\n Tree is empty\n");
else{

printf("\nEnter Data to be deleted : ");


scanf("%d", &data);
root = delete(root, data);
}
break;

case 3:
printf("\nEnter Data to be searched : ");
scanf("%d", &data);
if (search(data) == 1)
printf("\nData is found!\n");
else
printf("\nData is not found!\n");
break;

case 4:
inorder(root);
break;

case 5:
printf("\n\nThank you!!\n");
break;

default:
printf("\n\tInvalid Choice\n");
break;
}
}

return 0;
}

You might also like