You are on page 1of 7

ID: PREERN210017705

    Page No:


           
S.No: 16 Exp. Name: Binary Search Tree, Insertion, and Deletion in BST Date: 2022-11-10

Aim:
Aim: Binary Search Tree, Insertion, and Deletion in BST

Theory: A binary search tree (BST) is a binary tree where each node has a Comparable key (and an
associated value) and satisfies the restriction that the key in any node is larger than the keys in all nodes in
that node's left subtree and smaller than the keys in all nodes in that node's right subtree..

Algorithm
Create a new BST node and assign values to it.
insert(node, key)

1. If root == NULL, return the new node to the calling function.


2. if root=>data < key. call the insert function with root=>right and assign the return value in root=>right. ...

Finally, return the original root pointer to the calling function


Source Code:

BinarySearch.c

#include<stdio.h>

#include<malloc.h>

struct nodetype{

int info;

struct nodetype *lchild;

struct nodetype *rchild;

}*root, *dnode, *pardnode;;;

int k=0, m=0;

void btreeins(int);

void btsrch(struct nodetype *, int);

void display(struct nodetype *, int);

void inorder(struct nodetype *);

void postorder(struct nodetype *);

void preorder(struct nodetype *);

void btsrchn(int);

void delcaseA(struct nodetype*, struct nodetype *);

void delcaseB();

void btreedel(int);

int height(struct nodetype *, int);

void main(){

int n=0, ch, x, dn;

//root = NULL;

while(1){

printf("1.Create tree / Insertion\n2.Display tree\n3.Height of tree\n4.Search the d


esired value\n5.Inorder traversing of tree is");

    ITS Engineering College      2021-2025-CSE_3_D1

printf("\n6.Postorder traversing of tree is\n7.Preorder traversing of tree is\n8.De


letion\n9.Exit\nEnter choice for tree operation: ");

scanf("%d", &ch);

switch(ch){

case 1:

while(n!=-1){

printf("Enter node type(-1) to terminate: ");

scanf("%d", &n);

if(n!=-1){

btreeins(n);

n=0;

break;

case 2:

ID: PREERN210017705
    Page No:
           
display(root,0);

break;

case 3:

printf("Height is %d as follows", height(root,0));

break;

case 4:

printf("Enter node to be search: ");

scanf("%d", &x);

btsrch(root,x);

break;

case 5:

inorder(root);

break;

case 6:

postorder(root);

break;

case 7:

preorder(root);

break;

case 8:

printf("Enter the node to be delete: ");

scanf("%d", &dn);

btreedel(dn);

printf("Binary Search Tree after Deletion of node %d is as follows", dn);

display(root,1);

break;

case 9:

exit(0);

void btreeins(int x){

struct nodetype *p, *parent;

int found = 0;

p = root;

while((p!=NULL) && (!found)){

parent = p;

if(x == p->info){

found = 1;

else if(x<p->info){

p=p->lchild;

    ITS Engineering College      2021-2025-CSE_3_D1

else{

p = p->rchild;

if(!found){

p = (struct nodetype *)malloc(sizeof(struct nodetype));

p->info=x;

p->lchild = NULL;

p->rchild=NULL;

if(root!=NULL){

if(x<parent->info){

parent -> lchild = p;

else{

parent -> rchild = p;

ID: PREERN210017705
    Page No:
           
}

else{

root = p;

void btsrch(struct nodetype *p, int x){

while((p!=NULL) && (p->info!=x)){

if(p->info > x){

p=p->lchild;

else{

p=p->rchild;

if(p=NULL){

printf("Search unsucessful");

else{

printf("Search successful at node address %d ",x);

int height(struct nodetype *btreeh, int level){

if(btreeh){

height(btreeh->lchild, level+1);

height(btreeh->rchild, level+1);

if(level>m){

m = level;

return (m-1);

void inorder(struct nodetype *bt){

if(bt){

inorder(bt->lchild);

printf(" %d ", bt->info);

inorder(bt->rchild);

void preorder(struct nodetype *bt){

if(bt){

printf(" %d ",bt->info);

preorder(bt->lchild);

preorder(bt->rchild);

    ITS Engineering College      2021-2025-CSE_3_D1

void postorder(struct nodetype *bt){

if(bt){

postorder(bt->lchild);

postorder(bt->rchild);

printf(" %d ", bt->info);

void btreedel(int x){

btsrchn(x);

if(dnode == NULL){

printf("not found");

return;

if((dnode->rchild != NULL) && (dnode->lchild!=NULL)){

ID: PREERN210017705
    Page No:
           
delcaseB();

else{

delcaseA(dnode,pardnode);

free(dnode);

void btsrchn(int x){

dnode = root;

while((dnode!=NULL) && (dnode->info!=x)){

pardnode = dnode;

if(dnode->info>x){

dnode = dnode->lchild;

else{

dnode = dnode->rchild;

void delcaseA(struct nodetype *dnode, struct nodetype *pardnode){

struct nodetype *child;

if((dnode->lchild == NULL) && (dnode->rchild==NULL)){

child = NULL;

else if((dnode->lchild)!=NULL){

child = dnode->lchild;

else{

child = dnode->rchild;

if(pardnode != NULL){

if(dnode == pardnode -> lchild){

pardnode->lchild = child;

else{

pardnode -> rchild = child;

else{

root = child;

void delcaseB(){

struct nodetype *p, *q, *succ, *parsucc;

p = dnode->rchild;

q=dnode;

while((p->lchild)!=NULL){

    ITS Engineering College      2021-2025-CSE_3_D1

q=p;

p=p->lchild;

succ = p;

parsucc = q;

delcaseA(succ,parsucc);

if(pardnode!=NULL){

if(dnode == pardnode ->lchild){

pardnode -> lchild = succ;

else{

pardnode -> rchild = succ;

else{

root = succ;

ID: PREERN210017705
    Page No:
           
}

succ ->lchild = dnode->lchild;

succ->rchild = dnode ->rchild;

void display(struct nodetype *start, int t){

int i=1;

struct nodetype *ptr = start;

if(ptr){

display(ptr->rchild,t+1);

for(i=0; i<=t; i++){

printf(" ");

printf("%d<", ptr->info);

display(ptr->lchild, t+1);

Execution Results - All test cases have succeeded!

Test Case - 1

User Output
1.Create tree / Insertion 1
2.Display tree 1
3.Height of tree 1
4.Search the desired value 1
5.Inorder traversing of tree is 1
6.Postorder traversing of tree is 1
7.Preorder traversing of tree is 1
8.Deletion 1
9.Exit 1
Enter choice for tree operation: 1
Enter node type(-1) to terminate: 2
Enter node type(-1) to terminate: 6
Enter node type(-1) to terminate: 9
Enter node type(-1) to terminate: 13
Enter node type(-1) to terminate: -1
1.Create tree / Insertion 2
2.Display tree 2
3.Height of tree 2
4.Search the desired value 2
5.Inorder traversing of tree is 2
6.Postorder traversing of tree is 2
7.Preorder traversing of tree is 2
    ITS Engineering College      2021-2025-CSE_3_D1

8.Deletion 2
9.Exit 2
Enter choice for tree operation: 2
13< 9< 6< 2<1.Create tree / Insertion 3
2.Display tree 3
3.Height of tree 3
4.Search the desired value 3
5.Inorder traversing of tree is 3
6.Postorder traversing of tree is 3
7.Preorder traversing of tree is 3
8.Deletion 3
9.Exit 3
Enter choice for tree operation: 3
Height is 3 as follows1.Create tree / Insertion 4
ID: PREERN210017705
    Page No:
           
Test Case - 1
2.Display tree 4
3.Height of tree 4
4.Search the desired value 4
5.Inorder traversing of tree is 4
6.Postorder traversing of tree is 4
7.Preorder traversing of tree is 4
8.Deletion 4
9.Exit 4
Enter choice for tree operation: 4
Enter node to be search: 13
Search successful at node address 13 1.Create tree / Insertion 5
2.Display tree 5
3.Height of tree 5
4.Search the desired value 5
5.Inorder traversing of tree is 5
6.Postorder traversing of tree is 5
7.Preorder traversing of tree is 5
8.Deletion 5
9.Exit 5
Enter choice for tree operation: 5
2 6 9 13 1.Create tree / Insertion 6
2.Display tree 6
3.Height of tree 6
4.Search the desired value 6
5.Inorder traversing of tree is 6
6.Postorder traversing of tree is 6
7.Preorder traversing of tree is 6
8.Deletion 6
9.Exit 6
Enter choice for tree operation: 6
13 9 6 2 1.Create tree / Insertion 7
2.Display tree 7
3.Height of tree 7
4.Search the desired value 7
5.Inorder traversing of tree is 7
6.Postorder traversing of tree is 7
7.Preorder traversing of tree is 7
8.Deletion 7
9.Exit 7
Enter choice for tree operation: 7
2 6 9 13 1.Create tree / Insertion 8
2.Display tree 8
3.Height of tree 8
4.Search the desired value 8
5.Inorder traversing of tree is 8
6.Postorder traversing of tree is 8
    ITS Engineering College      2021-2025-CSE_3_D1

7.Preorder traversing of tree is 8


8.Deletion 8
9.Exit 8
Enter choice for tree operation: 8
Enter the node to be delete: 13
Binary Search Tree after Deletion of node 13 is as follows 9< 6< 2<1.Create tree / Insertion 9
2.Display tree 9
3.Height of tree 9
4.Search the desired value 9
5.Inorder traversing of tree is 9
6.Postorder traversing of tree is 9
7.Preorder traversing of tree is 9
8.Deletion 9
9.Exit 9
Enter choice for tree operation: 9
Test Case - 1

ID: PREERN210017705
    Page No:
           

    ITS Engineering College      2021-2025-CSE_3_D1

You might also like