Data Structure and Algorithm (CS 302) : A. K. Siromoni
Data Structure and Algorithm (CS 302) : A. K. Siromoni
( CS 302)
A. K. Siromoni
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 */
if ( p == q -> right){
q -> right = NULL;
free(p) ;
return root;} }
if ( p == q -> right ){
q -> right = p -> right ;
free(p); return root;} }
if ( p == q -> right ){
q -> right = p -> left ;
free(p); return root;} } If key = 11 q = &10, p = &11
• 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.
q = root;
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 = &3 q = NULL
if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;
p = &18 q = NULL
if(p!=NULL){
printf(“%d “, p-> data);
q=p->right;
3. https://www.youtube.com/watch?v=wcIRPqTR3Kc
Binary Search Trees (BSTs) - Insert and Remove Explained
By Professor Cooleen Lewis