Threaded Binary Search Tree
Write a C Program to Implement operations in Threaded Binary Search Tree. Here’s simple Program for Insertion, Deletion and Inorder Preorder Traversal in fully Threaded Binary Search Tree in C Programming Language.
What is Tree ?
In linear data structure, data is organized in sequential order and in non-linear data structure, data is organized in random order. Tree is a very popular data structure used in wide range of applications.
A tree data structure can be defined as follows…
- Tree is a non-linear data structure which organizes data in hierarchical structure and this is a recursive definition.
A tree data structure can also be defined as follows…
- Tree data structure is a collection of data (Node) which is organized in hierarchical structure and this is a recursive definition.
Every individual element is called as Node. Node in a tree data structure, stores the actual data of that particular element and link to next element in hierarchical structure.
Below is the source code for C Program to Implement operations in Threaded Binary Search Tree which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Implement operations in Threaded Binary Search Tree */ #include <stdio.h> #include <stdlib.h> typedef enum {false,true} boolean; struct node *in_succ(struct node *p); struct node *in_pred(struct node *p); struct node *insert(struct node *root, int ikey); struct node *del(struct node *root, int dkey); struct node *case_a(struct node *root, struct node *par,struct node *ptr); struct node *case_b(struct node *root,struct node *par,struct node *ptr); struct node *case_c(struct node *root, struct node *par,struct node *ptr); void inorder( struct node *root); void preorder( struct node *root); struct node { struct node *left; boolean lthread; int info; boolean rthread; struct node *right; }; int main( ) { int choice,num; struct node *root=NULL; while(1) { printf("\n"); printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Inorder Traversal\n"); printf("4.Preorder Traversal\n"); printf("5.Quit\n"); printf("\nEnter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("\nEnter the number to be inserted : "); scanf("%d",&num); root = insert(root,num); break; case 2: printf("\nEnter the number to be deleted : "); scanf("%d",&num); root = del(root,num); break; case 3: inorder(root); break; case 4: preorder(root); break; case 5: exit(1); default: printf("\nWrong choice\n"); }/*End of switch */ }/*End of while */ return 0; }/*End of main( )*/ struct node *insert(struct node *root, int ikey) { struct node *tmp,*par,*ptr; int found=0; ptr = root; par = NULL; while( ptr!=NULL ) { if( ikey == ptr->info) { found =1; break; } par = ptr; if(ikey < ptr->info) { if(ptr->lthread == false) ptr = ptr->left; else break; } else { if(ptr->rthread == false) ptr = ptr->right; else break; } } if(found) printf("\nDuplicate key"); else { tmp=(struct node *)malloc(sizeof(struct node)); tmp->info=ikey; tmp->lthread = true; tmp->rthread = true; if(par==NULL) { root=tmp; tmp->left=NULL; tmp->right=NULL; } else if( ikey < par->info ) { tmp->left=par->left; tmp->right=par; par->lthread=false; par->left=tmp; } else { tmp->left=par; tmp->right=par->right; par->rthread=false; par->right=tmp; } } return root; }/*End of insert( )*/ struct node *del(struct node *root, int dkey) { struct node *par,*ptr; int found=0; ptr = root; par = NULL; while( ptr!=NULL) { if( dkey == ptr->info) { found =1; break; } par = ptr; if(dkey < ptr->info) { if(ptr->lthread == false) ptr = ptr->left; else break; } else { if(ptr->rthread == false) ptr = ptr->right; else break; } } if(found==0) printf("\ndkey not present in tree"); else if(ptr->lthread==false && ptr->rthread==false)/*2 children*/ root = case_c(root,par,ptr); else if(ptr->lthread==false )/*only left child*/ root = case_b(root, par,ptr); else if(ptr->rthread==false)/*only right child*/ root = case_b(root, par,ptr); else /*no child*/ root = case_a(root,par,ptr); return root; }/*End of del( )*/ struct node *case_a(struct node *root, struct node *par,struct node *ptr ) { if(par==NULL) /*root node to be deleted*/ root=NULL; else if(ptr==par->left) { par->lthread=true; par->left=ptr->left; } else { par->rthread=true; par->right=ptr->right; } free(ptr); return root; }/*End of case_a( )*/ struct node *case_b(struct node *root,struct node *par,struct node *ptr) { struct node *child,*s,*p; /*Initialize child*/ if(ptr->lthread==false) /*node to be deleted has left child */ child=ptr->left; else /*node to be deleted has right child */ child=ptr->right; if(par==NULL ) /*node to be deleted is root node*/ root=child; else if( ptr==par->left) /*node is left child of its parent*/ par->left=child; else /*node is right child of its parent*/ par->right=child; s=in_succ(ptr); p=in_pred(ptr); if(ptr->lthread==false) /*if ptr has left subtree */ p->right=s; else { if(ptr->rthread==false) /*if ptr has right subtree*/ s->left=p; } free(ptr); return root; }/*End of case_b( )*/ struct node *case_c(struct node *root, struct node *par,struct node *ptr) { struct node *succ,*parsucc; /*Find inorder successor and its parent*/ parsucc = ptr; succ = ptr->right; while(succ->left!=NULL) { parsucc = succ; succ = succ->left; } ptr->info = succ->info; if(succ->lthread==true && succ->rthread==true) root = case_a(root, parsucc,succ); else root = case_b(root, parsucc,succ); return root; }/*End of case_c( )*/ struct node *in_succ(struct node *ptr) { if(ptr->rthread==true) return ptr->right; else { ptr=ptr->right; while(ptr->lthread==false) ptr=ptr->left; return ptr; } }/*End of in_succ( )*/ struct node *in_pred(struct node *ptr) { if(ptr->lthread==true) return ptr->left; else { ptr=ptr->left; while(ptr->rthread==false) ptr=ptr->right; return ptr; } }/*End of in_pred( )*/ void inorder( struct node *root) { struct node *ptr; if(root == NULL ) { printf("Tree is empty"); return; } ptr=root; /*Find the leftmost node */ while(ptr->lthread==false) ptr=ptr->left; while( ptr!=NULL ) { printf("%d ",ptr->info); ptr=in_succ(ptr); } }/*End of inorder( )*/ void preorder(struct node *root ) { struct node *ptr; if(root==NULL) { printf("Tree is empty"); return; } ptr=root; while(ptr!=NULL) { printf("%d ",ptr->info); if(ptr->lthread==false) ptr=ptr->left; else if(ptr->rthread==false) ptr=ptr->right; else { while(ptr!=NULL && ptr->rthread==true) ptr=ptr->right; if(ptr!=NULL) ptr=ptr->right; } } }/*End of preorder( )*/
OUTPUT : :
/* C Program to Implement operations in Threaded Binary Search Tree */ 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 5 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 3 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 4 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 2 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 7 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 6 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 1 Enter the number to be inserted : 8 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 3 2 3 4 5 6 7 8 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 4 5 3 2 4 7 6 8 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 2 Enter the number to be deleted : 5 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 3 3 4 2 6 7 8 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 4 2 3 4 7 6 8 1.Insert 2.Delete 3.Inorder Traversal 4.Preorder Traversal 5.Quit Enter your choice : 5 Process returned 1
If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.
Thanks for reading the post….