0% found this document useful (0 votes)
79 views29 pages

Data Structure and Algorithm (CS 302) : A. K. Siromoni

The document discusses deletion of nodes from a binary search tree implemented in C language. It explains three cases for node deletion - when the node is a leaf, has one subtree, or has two subtrees. For each case, it provides pseudocode to delete the node by adjusting pointers. It also covers creation of a right-threaded binary search tree and provides an inorder traversal algorithm to traverse the tree without recursion by following thread pointers.

Uploaded by

Abhigyan Prakash
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)
79 views29 pages

Data Structure and Algorithm (CS 302) : A. K. Siromoni

The document discusses deletion of nodes from a binary search tree implemented in C language. It explains three cases for node deletion - when the node is a leaf, has one subtree, or has two subtrees. For each case, it provides pseudocode to delete the node by adjusting pointers. It also covers creation of a right-threaded binary search tree and provides an inorder traversal algorithm to traverse the tree without recursion by following thread pointers.

Uploaded by

Abhigyan Prakash
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/ 29

Data Structure And Algorithm

( CS 302)

A. K. Siromoni

Department of Information Technology


Lecture 4

Lecture Objective 1 : Explain and implement the procedure of deletion of a


node from tree in C Language

Lecture Objective 2 : Illustrate creation of Threaded Binary Tree


and implement it C Language

Lecture Objective 3 : Implement inorder traversal Threaded Binary Tree


Last Lecture
Binary Search Tree : Creation and Traverse

Now we will try to Delete a node from BST

Three General Conditions :


1. May be leaf

2. May have one subtree

3. May have both subtrees


Deletion of a Node
1. May be leaf
• May be root
• If not root
 May be left child
 May be right child

2. May have one subtree


• Left Child with left subtree
• Left Child with right subtree
• Right Child with left subtree
• Right Child with right subtree

3. May have both subtrees


• Inorder Successor may not be the child
• Inorder Successor may be the child
• Inorder Predicessor may not be the child
• Inorder Predicessor may be the child
Deletion of a leaf

Just Delete the node and parent will


point to NULL instead of that node

Temp = Parent -> left / right ;


Parent - > left / right = NULL;
Delete temp ;
Deletion of a Node with one subtree

Temp = Parent -> left / right ;


Parent - > left / right = Temp -> left / right;
Delete temp ;
Deletion of a Node with both subtrees
Find highest value in left sub tree ( Predecessor )

Temp = left_max_node ( Temp = & 23 )


Data Value of node to be deleted = Data value of left_max_node / Temp
( 23 copied to root )
Right of Parent of left_max_node / Temp = left of left_max_node / Temp
( &17 -> to right = &23 -> left )
Delete temp ( & 23 deleted )
Deletion of a Node with subtrees
Find lowest value in right subtree (Successor )

Temp = right_min_node ( Temp = & 54 )


Data Value of matching node = Data value of right_min_node / Temp
( 54 copied to root )
Left of Parent of right_min_node / Temp = right of right_min_node / Temp
( &72 -> to left = &54 -> left )
Delete temp ( & 54 deleted )
Deletion of a Node with subtrees
Find lowest value in right subtree (Successor )

Temp = right_min_node ( Temp = & 10 )


Data Value of matching node = Data value of right_min_node / Temp
( 10 copied to root )
right of the matching node= right of right_min_node / Temp
( &10(new) -> to right = &10 (old) -> right )
Delete temp ( & 10 (old) deleted )
Deletion of a Node

struct tree * delnode(struct tree * root, int key){ struct tree *p, *q , *f;

/* if the key is equal to the value in root and root is the only node available */

if( key == root -> data && root -> left == NULL && root -> right ==NULL){
free(root);
return NULL; }

Data to be found is 17
Deletion of a Node
/* otherwise we have to find out the node where it is exactly matching and father of
that node */
p = root ;
q = NULL;
/* p will hold the matching node address, q will hold the father's address */

while( p != NULL && p -> data != key){


q = p;
p = key < p-> data ? p -> left : p -> right ; }
/* p is NULL means matching data is not available in tree */
if ( p == NULL){
printf ( "no data found ");
return root;}

If key = 10 , q = &20, p = &10


If key = 7 , q = &8 , p = NULL
Deletion of a Node
/ * p has a valid address means data is available in node p* /
/* case 1 : p is a leaf */
if( p-> left == NULL && p-> right == NULL){
if ( p == q -> left){
q -> left = NULL;
free(p) ; return root;}

If key = 38 , q = &40, p = &38

if ( p == q -> right){
q -> right = NULL;
free(p) ;
return root;} }

If key = 15 , q = &12, p = &15


Deletion of a Node
/* data is not in the root */

if ( p-> left == NULL) {


if ( p == q -> left ){ If key = 3 q = &5, p = &3
q -> left = p -> right ;
free(p); return root;}

if ( p == q -> right ){
q -> right = p -> right ;
free(p); return root;} }

if ( p-> right == NULL ) {


if ( p == q -> left ){
q -> left = p -> left ;
free(p); return root;}

if ( p == q -> right ){
q -> right = p -> left ;
free(p); return root;} } If key = 11 q = &10, p = &11

If key = 8 q = &10, p = &8 If key = 6 q = &2, p = &6


Deletion of a Node
/* case 2 : p has only one subtree*/
/* data found in root node */
if ( p == root ){

if (p -> left == NULL){


root = p -> right;
free(p);
return root;}

If key = 5 q = NULL, p = &5


&21 will be the new root

if (p -> right == NULL){


root = p -> left;
free(p);
return root;} }

If key = 5 q = NULL, p = &5


&3 will be the new root
Deletion of a Node
/* case 3 : p has both subtrees */
/* q holds address of lowest value of right subtree of p and f is the parent address of q*/
q = p - > right ; f = NULL;
while(((q -> left) ! = NULL){ f = q;
q = q -> left; }
p -> data = q -> data;
if (f==NULL) p -> right = q -> right
if( f ! = NULL ) f -> left = q -> right;
free (q);
return root; } }

If key = 10 q = &11, f = NULL If key = 2 q = &3, f = &4


Improving Efficiency of B S T
Threaded B S T
Threaded B S T
• Single Threaded: Inorder Traversal : Right Threaded BST
Preorder Traversal : Left Threaded BST

• Double Threaded: Where both left and right NULL pointers are made to
point to inorder predecessor and inorder successor
respectively. The predecessor threads are useful for
reverse inorder traversal and postorder traversal.

Advantages of Threaded BST :

• Can be traversed without using Stack or Recursion


• Revisit of the Nodes can be avoided

Disadvantages of Threaded BST :

• Have to use extra data members , so takes more memory


Creation of Right Threaded B S T
struct bthread { int data ;
struct bthread * left ;
struct bthread * right;
int thread ; /* Will keep a flag on whether it’s a right child or successor
0 if it has a right child, 1 if it does not have */ };

main(){ struct bthread * root, * node *p, *q;


root = createnode();
putdata(root);
while(getans()){
p = q = root;
node = createnode();
putdata(node);
while( q != NULL){
p=q;
((node - >data) < (p -> data)) ? q = q -> left : q = q -> right ;
((node - >data) < (p -> data)) ? setleft( p, node) : setright(p, node) ; }}
Creation of Right Threaded B S T
void putdata( struct bthread *n ){
scanf(“%d”,&(n->data));
n -> left = NULL ;
n -> right = NULL;
int thread = 1; }

void setleft ( struct bthread * parent , struct bthread * node ){


parent -> left = node ;
node -> right = parent ;
node -> thread =1 ; }
Creation of Right Threaded B S T
void setright ( struct bthread * parent , struct bthread * node ){
struct bthread * temp;
temp = parent - > right ;
parent -> right = node;
node -> right = temp ;
node -> thread = 1;
parent -> thread = 0; }
Inorder Traversal of Right Threaded B S T

void inorder_rbthread ( struct bthread * root ){


struct bthread *p, *q;
q = root;

do{ p= NULL; while(q != NULL){ p=q;


q=q->left;}
if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;
while(p->thread == 1 && q != NULL){
printf(“%d “, q->data);
p=q;
q=q->right;}
}
} while(p!=NULL);
}
Inorder Traversal of Right Threaded B S T
void inorder_rbthread ( struct bthread * root ){struct bthread *p, *q;

q = root;

do{ p= NULL; while(q != NULL){ p=q;


q=q->left;}

p = &(-4) q = NULL

if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;

-4 printed q = &2
Inorder Traversal of Right Threaded B S T
-4 printed q = &2
while(p->thread == 1 && q != NULL){
printf(“%d “, q->data);
p=q;
q=q->right;}
}
} while(p!=NULL);

p = &(-4) thread true q = &2


2 printed
p = &2 q = &3
&2 thread false
while ends
Inorder Traversal of Right Threaded B S T
p = &2 q = &3 &2 thread false while ends

do{ p= NULL; while(q != NULL){ p=q;


q=q->left;}

p = &3 q = NULL

if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;

p= &3 3 printed q = &5


Inorder Traversal of Right Threaded B S T
p= &3 q = &5
while(p->thread == 1 && q != NULL){
printf(“%d “, q->data);
p=q;
q=q->right;}
}
} while(p!=NULL);

p = &3 thread true q = &5


5 printed
p = &5 q = &18
&5 thread false
while ends
Inorder Traversal of Right Threaded B S T
p = &5 q = &18
do{ p= NULL; while(q != NULL){ p=q;
q=q->left;}

p = &18 q = NULL

if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;

p= &18 18 printed q = NULL


Inorder Traversal of Right Threaded B S T
p= &18 18 printed q = NULL

while(p->thread == 1 && q != NULL){


printf(“%d “, q->data);
p=q;
q=q->right;}
}
} while(p!=NULL);

& 18 thread false so does not enter


while loop

do{ p= NULL; while(q != NULL){ p=q;


q=q->left;}
if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;
p becomes NULL and q is also NULL program ends
Assignment 5
1. Write a program in C language to delete all the nodes of a BST
containing odd numbers.

2. Write a program to delete a node from right threaded BST

( Every Program should be properly documented )

• Covers LO1, LO2 and LO3


Self Learning
For Assignment 4

1. Data Structure Using C & C++ : Langsum, Augenstein , Tanenbaum ,


Second Edition, Prentice Hall of India

2. Data Structures : Seymour Lipschutz , Indian Adapted Edition


2006, Tata McGaw Hill Education Pvt Ltd

3. https://www.youtube.com/watch?v=wcIRPqTR3Kc
Binary Search Trees (BSTs) - Insert and Remove Explained
By Professor Cooleen Lewis

You might also like