Data Structures Lakshmish Ramaswamy Searching in BST BST
Data Structures Lakshmish Ramaswamy
Searching in BST • BST lends itself to efficient searching • Find node matching a given object – Equivalently, object matching a key • Observation – If given object > object at current node, given object cannot be in left subtree – If given object < object at current node, given object cannot be in left subtree • Safely eliminate one subtree • Recursive and iterative implementations
Recursive Implementation Method in Binary. Node Class public Binary. Node find(Object o){ if(o. compare. To(element) < 0){ if(left == null) return(null); else return(left. find(o)); } if(o. compare. To(element) > 0){ if(right == null) return(null); else return(right. find(o)); } return(this); } Method in Binary. Search. Tree Class public Binary. Node find(Object o){ if(root == null) return(null); root. find(o); }
Iterative Implementation Method in Binary. Search. Tree Class public Binary. Node find(Object o){ if(root == null) return(null); Binary. Node t = root; while(t != null){ if(o. compare. To(t. element) < 0) t = t. left; else if(o. compare. To(t. element) > 0) t = t. right; else return(t); } return(null); }
Finding the Minimum Element Method in Binary. Search. Tree class public Binary. Node find. Min(){ if(root == null) return(null); Binary. Node t = root; while(t. left != null) t = t. left; return(t); }
Finding the Maximum Element Method in Binary. Search. Tree class public Binary. Node find. Max(){ if(root == null) return(null); Binary. Node t = root; while(t. right != null) t = t. right; return(t); }
Inserting into BST • • • Should maintain the ordering property New element is always inserted as a leaf Tree traversal to find appropriate position 1. Start from root 2. Repeatedly choose left or right subtree based on key comparisons 3. Repeat 2 until reaching null 4. Insert element at the position as a new leaf
Insertion Method public void insert (Object o){ Binary. Node new. Node = new Binary. Node(o); if (root == null){ root = new. Node; return; } Binary. Node c = root; Binary. Node p; boolean left; while(c != null) { if (o. compare. To(c. element) <= 0){ p = c; c = c. left; left = true; } else { p = c; c = c. right; left = false; } }
Insertion Method (Contd. ) if (left){ p. left = new. Node; else p. right = new. Node; return; }
Insertion Example 7 10 25 6 15 4 9 7 12 19 25
Removal from BST • Remove a given element from BST, if it exists and return the node containing it • Search for given element in BST (find method); Return null if not found
Removal Logic • If element is at a leaf, set parent’s left/right pointer to null • If not a leaf, remove node by replacing it with node containing immediate higher/lower element – Immediate higher (lower) element is in leftmost (rightmost) node of right (left) subtree • Some extra maintenance operations might be needed
Removal Example 12 10 9 15 5 10 15 17 4 8 7 12 9 19 17 25
Remove Procedure Remove node containing o from tree and return the node • Search for node c containing element matching o. Let p be parent of c • If not found return null • If c is at leaf, remove c by setting child pointer of p to null • If c. left != null, find rightmost node of c’s left subtree. Let it be c 1. Let p 1 be c 1’s parent • Set p 1’s right pointer to c 1’s left child
Remove Procedure (Contd. ) • Replace c by c 1 – Set c 1. left = c. left; c 1. right = c. right – Set c 1 as the child of p • Perform analogous operations on the right subtree in case c. left == null • If c is root, set root to c 1 • Return c
- Slides: 15