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

Dsa Lab12

1) The document contains C code to implement binary search tree operations like insertion, deletion, and traversal. 2) Functions are defined to create nodes, insert nodes, delete nodes based on different cases like leaf, single child, double children. 3) A preorder traversal is also implemented to print the tree. 4) The main function allows the user to choose between insertion, deletion and traversal and tests the BST implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Dsa Lab12

1) The document contains C code to implement binary search tree operations like insertion, deletion, and traversal. 2) Functions are defined to create nodes, insert nodes, delete nodes based on different cases like leaf, single child, double children. 3) A preorder traversal is also implemented to print the tree. 4) The main function allows the user to choose between insertion, deletion and traversal and tests the BST implementation.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

DSA LAB-12

Name: Simanjeet Kalia


Roll No.: 20051104
#include<stdio.h>
#include<stdlib.h>

struct node{
int info;
struct node *left;
struct node *right;
};

struct node* createNode(int x){


struct node *temp;
temp = (struct node*)malloc(sizeof(struct node));
temp->info = x;
temp->left = NULL;
temp->right = NULL;
return temp;
}

struct node* insertNode(struct node* root, int x){


if (root == NULL){
return createNode(x);
}
else if (x < root->info){
root->left = insertNode(root->left, x);
}
else if (x > root->info){
root->right = insertNode(root->right, x);
}
return root;
}

void preorderPrint(struct node *root){


if (root == NULL) return;
printf("%d ", root->info);
preorderPrint(root->left);
preorderPrint(root->right);
}

//deletion cases
//case a (node to be deleted is a leaf node)
struct node* case_a(struct node* root, struct node* par, struct node* ptr){
if (par == NULL){
root = NULL;
}
else if (ptr == par->left){
par->left = ptr->left;
}
else {
par->right = ptr->right;
}
free(ptr);
return root;
}

//case b (node to be deleted has one child)


struct node* case_b(struct node* root, struct node* par, struct node* ptr){
struct node* child;
if (ptr->left != NULL){
child = ptr->left;
}
else if (ptr->right != NULL){
child = ptr->right;
}

if (par == NULL){
root = child;
}
else if (ptr == par->left){
par->left = child;
}
else {
par->right = child;
}
free(ptr);
return root;
}

//case c (node to be deleted has 2 children)


struct node* case_c(struct node* root, struct node* par, struct node* ptr){
struct node *succ, *par_succ;
par_succ = ptr;
succ = ptr->right;

//finding the inorder successor


while (succ->left != NULL){
par_succ = succ;
succ = succ->left;
}
//copying the value of ios to ptr
ptr->info = succ->info;

//deleting the successor node


if (succ->left == NULL && succ->right == NULL){
root = case_a(root, par_succ, succ);
}
else {
root = case_b(root, par_succ, succ);
}
return root;
}

struct node* deleteNode(struct node* root, int dkey){


struct node *par, *ptr;
ptr = root;
par = NULL;
while (ptr != NULL){
if (dkey == ptr->info)
break;
//update parent
par = ptr;
if (dkey < ptr->info){
ptr = ptr->left;
}
else{
ptr = ptr->right;
}
}
if (ptr == NULL){
printf("dkey is not present\n");
}

//node as 2 child
else if (ptr->left != NULL && ptr->right != NULL){
root = case_c(root, par, ptr);
}
//node has 1 children
else if (ptr->left != NULL || ptr->right != NULL){
root = case_b(root, par, ptr);
}
//node has no children
else {
root = case_a(root, par, ptr);
}
return root;
}
int main(){
int data, ch, i, n;

struct node *root = NULL;

while (1)

printf("\n1.Insertion in Binary Search Tree");

printf("\n2.Delete Element in Binary Search Tree");

printf("\n3.Preorder\n4.Exit");

printf("\nEnter your choice: ");

scanf("%d", &ch);

switch (ch)

case 1:
printf("\nEnter N value: ");

scanf("%d", &n);

printf("\nEnter the values to create BST\n");

for (i = 0; i < n; i++)

scanf("%d", &data);

root = insertNode(root, data);


}

break;

case 2:
printf("\nEnter the element to delete: ");

scanf("%d", &data);
root = deleteNode(root, data);

break;

case 3:
printf("\nPreorder Traversal: \n");

preorderPrint(root);

break;

case 4:
exit(0);

default:
printf("\nWrong option");

break;
}
}

return 0;
}

You might also like