16IT201/Data structure/Unit II/Binary Search Tree Recap Binary Search Tree Node Declaration and Routine Declaration To make a Empty Tree Insert a element in a tree 16IT201/Data structure/Unit II/Binary Search Tree
Binary Search Tree-Find, FindMin, FindMax and Delete 16IT201/Data structure/Unit II/Binary Search Tree
Routine for Find Operation Int Find(int X, SearchTree T) { If(T==NULL) return NULL; If(X<T->Element) return Find(X,T->left); else If(X>T->Element) return Find(X,T->Right) Return T;//returns the position of the search element 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Find Min Returns the position of the smallest element in the tree To perform Find Min start the root and go left as long ad there is a left child. The stopping point is the smallest element int FindMin (SearchTree T) { If(T==NULL) return NULL; else if(T->left==NULL) return T; else return FindMin (T->left); } 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Non Recursive Routine Int FindMin(SearchTree T) { If(T!=NULL) While(T->left!=NULL) T=T->left Return T } 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Find Max Returns the position of the largest element in the tree Start at root and go right as long as there is a right child The stopping point is the largest element 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Routine for FindMax int FindMax(SearchTree T) { If(T==NULL) return NULL; else if(T->Right==NULL) return T; else FindMax(T->Right); } int Find Max(SearchTree T) { If(T!=NULL) While(T->Right!=NULL) T=T->Right return T; } 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Check Point ----- is the top node in a tree. ------ is the terminal node in the tree. -------- traverses the tree in Root,left,right order. -------- traverses the tree in Left,Right,Root order. All the child nodes of a parent node are referred as Neighbors b)Sibilings c)internal nodes d)leaf nodes 6. Which node in a binary tree does not have a parent node? 7. In an Expression tree,the internal odes contain ---- while the leaf nodes contain -------. 8) The Expression (a+b)*(a-b) is stored in an expression tree.What will be preorder sequence. 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Deletion Operation To delete an element consider the following three possibilities CASE 1->Node to be deleted is a leaf node(ie) No children CASE 2 ->Node with one child CASE 3-> Node with two children 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Deletion Operation CASE 1->If the node is a leaf node ,it can be deleted immediately CASE 2:Node with one child If the node has one child, it can be deleted by adjusting in parent pointer that points to its child node CASE 3: To replace the data of the node to be deleted with its smallest data of the right subtree and recursively delete that node 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Deletion 12 5 15 3 7 13 15 9 1 8 11 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Deletion Operation SearchTree Delete(int X,SearchTree T) { Int Tmpcell; If(T==NULL) Error(“Element not found”); else If(X<T->Element T->Left=Delete(X,T->Left); Else If(X>T->Element) T->Right=Delete(X,T->Right); 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Deletion Operation If(T->Left&&T->Right) Tmpcell=FindMin(T->Right); T->Element=Tmpcell->Element; T->Right=Delete(T->Element;T->Right); } Else//one or zero children { Tmpcell=T; If(T->Left==NULL) T=T->Right; Else if(T->Right==NULL) T=T->Left; Free(Tmpcell); } Return T; 16IT201/Data structure/Unit II/Binary Search Tree
16IT201/Data structure/Unit II/Binary Search Tree Summarization 16IT201/Data structure/Unit II/Binary Search Tree