








Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!









Binary Search Tree Representation:
BST Basic Operations:
Insert Operation:
Search Operation:
Tree Node:
return; } else { find(item, &parent, &location); cout<<"\n\n\t\tNode is present in BST"; } }
void BST::insert(node *tree, node *newnode) { if (root == NULL) { root = new node; root->info = newnode->info; root->left = NULL; root->right = NULL; cout<<"Root Node is Added"<<endl; return; } if (tree->info == newnode->info) { cout<<"Element already in the tree"<<endl; return; } if (tree->info > newnode->info) { if (tree->left != NULL) { insert(tree->left, newnode); } else { tree->left = newnode; (tree->left)->left = NULL; (tree->left)->right = NULL; cout<<"Node Added To Left"<<endl; return; } } else { if (tree->right != NULL) { insert(tree->right, newnode); } else { tree->right = newnode; (tree->right)->left = NULL; (tree->right)->right = NULL; cout<<"Node Added To Right"<<endl; return; } }
void BST::case_a(node *par, node *loc ) { if (par == NULL) { root = NULL; } else { if (loc == par->left) par->left = NULL; else par->right = NULL; } }
void BST::case_b(node *par, node *loc) { node *child; if (loc->left != NULL) child = loc->left; else child = loc->right; if (par == NULL) {
public: void find(int, node **, node **); void search(); void insert(node *, node *); void del(int); void case_a(node *,node *); void case_b(node *,node *); void case_c(node *,node *); void display(node *, int); BST() { root = NULL; } }; int main () { int ch, num; BST bst; node *temp; while (1) { cout<<"\n\t >>>>>>>> Menu of Binary Search Tree (BST) <<<<<<<<"; cout<<"\n"; cout<<"\n\t-------------------------------------------------------------"; cout<<"\n\tENTER 1: Insert element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 2: Displaying BST"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 3: Search Element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 4: Delete element"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\tENTER 5: To Exit"; cout<<"\n\t\t-----------------------------------"; cout<<"\n\n\tPlease Enter Your Choice: "; cin>>ch; switch(ch){ case 1: system("cls"); temp = new node; cout<<"\n\n\tEnter the number to be inserted : "; cin>>temp->info; bst.insert(root, temp);Sleep(2000);system("cls"); break; case 2: system("cls"); cout<<"\n\n\t\tDisplay BST:\n"<<endl; bst.display(root,1); cout<<endl;Sleep(5000);system("cls"); break; case 3: system("cls"); bst.search(); Sleep(2000);system("cls"); break; case 4: system("cls"); if (root == NULL)
cout<<"\n\n\tTree is empty, nothing to delete"<<endl; continue; } cout<<"\n\n\tEnter the number to be deleted : "; cin>>num; bst.del(num); Sleep(2000);system("cls"); break; case 5: exit(0); default: cout<<"\nWrong choice!!"; } } return 0; } void BST::insert(node *tree, node *newnode) { if (root == NULL) { root = new node; root->info = newnode->info; root->left = NULL; root->right = NULL; cout<<"\n\tRoot Node is Added"<<endl; return; } if (tree->info == newnode->info) { cout<<"\n\tElement already in the tree"<<endl; return; } if (tree->info > newnode->info) { if (tree->left != NULL) { insert(tree->left, newnode); } else { tree->left = newnode; (tree->left)->left = NULL; (tree->left)->right = NULL; cout<<"\n\tNode Added To Left"<<endl; return; } } else { if (tree->right != NULL) { insert(tree->right, newnode); } else { tree->right = newnode; (tree->right)->left = NULL; (tree->right)->right = NULL; cout<<"\n\tNode Added To Right"<<endl; return;
void BST::search() { int item; node ptr, ptrsave,loc,**par; node *parent, *location; if (root == NULL) { cout<<"\n\n\t\tTree empty"<<endl; return; } find(item, &parent, &location); cout<<"\n\n\t\tEnter Element to be searched: "; cin>>item; find(item, &parent, &location); if (location == NULL) { cout<<"\n\n\t\tItem not present in tree"<<endl; return; } else { find(item, &parent, &location); cout<<"\n\n\t\tNode is present in BST"; } } void BST::del(int item) { node *parent, *location; if (root == NULL) { cout<<"\n\n\t\tTree empty"<<endl; return; } find(item, &parent, &location); if (location == NULL) { cout<<"\n\n\t\tItem not present in tree"<<endl; return; } if (location->left == NULL && location->right == NULL) case_a(parent, location); if (location->left != NULL && location->right == NULL) case_b(parent, location); if (location->left == NULL && location->right != NULL) case_b(parent, location); if (location->left != NULL && location->right != NULL) case_c(parent, location); free(location); } void BST::case_a(node *par, node *loc ) { if (par == NULL) { root = NULL; } else {
if (loc == par->left) par->left = NULL; else par->right = NULL; } } void BST::case_b(node *par, node *loc) { node *child; if (loc->left != NULL) child = loc->left; else child = loc->right; if (par == NULL) { root = child; } else { if (loc == par->left) par->left = child; else par->right = child; } } void BST::case_c(node *par, node *loc) { node *ptr, *ptrsave, *suc, *parsuc; ptrsave = loc; ptr = loc->right; while (ptr->left != NULL) { ptrsave = ptr; ptr = ptr->left; } suc = ptr; parsuc = ptrsave; if (suc->left == NULL && suc->right == NULL) case_a(parsuc, suc); else case_b(parsuc, suc); if (par == NULL) { root = suc; } else { if (loc == par->left) par->left = suc; else par->right = suc; } suc->left = loc->left; suc->right = loc->right; }
Option#2 is used to Display element of BST as shown in fig Option#3 is used to find binary node in a BST as shown in figs
Option#4 is used to delete element of BST as shown in figs
Option#5 is used to Exit from BST program