170 likes | 186 Views
Binary Search Trees (BST). What is a Binary search tree? Why Binary search trees? Binary search tree implementation Insertion in a BST Deletion from a BST. 2. A. A. B. 6. 1. 4. C. 8. D. 7. 9. 3. 5. Binary Search Trees (Definition).
E N D
Binary Search Trees (BST) • What is a Binary search tree? • Why Binary search trees? • Binary search tree implementation • Insertion in a BST • Deletion from a BST
2 A A B 6 1 4 C 8 D 7 9 3 5 Binary Search Trees (Definition) • A binary search tree (BST) is a binary tree that is empty or that satisfies the BST ordering property: • The key of each node is greater than each key in the left subtree, if any, of the node. • The key of each node is less than each key in the right subtree, if any, of the node. • Thus, each key in a BST is unique. • Examples:
Why BST? • BSTs provide good logarithmic time performance in the best and average cases. • Average case complexities of using linear data structures compared to BSTs: *using binary search
Binary Search Tree Implementation • The BinarySearchTree class inherits the instance variables key, left, and right of the BinaryTree class: public class BinarySearchTree extends BinaryTree implements SearchableContainer { private BinarySearchTree getLeftBST(){ return (BinarySearchTree) getLeft( ) ; } private BinarySearchTree getRightBST( ){ return (BinarySearchTree) getRight( ) ; } // . . . }
Binary Search Tree Implementation (Cont’d) • The find method of the BinarySearchTree class: public Comparable find(Comparable comparable) { if(isEmpty()) return null; Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) return key; else if (comparable.compareTo(key)<0) return getLeftBST().find(comparable); else return getRightBST().find(comparable); }
Binary Search Tree Implementation (Cont’d) • The findMin method of the BinarySearchTree class: • By the BST ordering property, the minimum key is the key of the left-most node that has an empty left-subtree. public Comparable findMin() { if(isEmpty()) return null; if(getLeftBST().isEmpty()) return (Comparable)getKey(); else return getLeftBST().findMin(); }
20 32 9 30 35 7 10 40 25 4 15 Binary Search Tree Implementation (Cont’d) • The findMax method of the BinarySearchTree class: • By the BST ordering property, the maximum key is the key of the right-most node that has an empty right-subtree. public Comparable findMax() { if(isEmpty()) return null; if(getRightBST().isEmpty()) return (Comparable)getKey(); else return getRightBST().findMax(); }
Insertion in a BST • By the BST ordering property, a new node is always inserted as a leaf node. • The insert method, given in the next page, recursively finds an appropriate empty subtree to insert the new key. It then transforms this empty subtree into a leaf node by invoking the attachKey method: public void attachKey(Object obj) { if(!isEmpty()) throw new InvalidOperationException(); else { key = obj; left = new BinarySearchTree(); right = new BinarySearchTree(); } }
6 6 6 6 5 2 2 2 2 8 8 8 8 4 4 4 4 7 7 7 7 9 9 9 9 3 3 3 3 Insertion in a BST public void insert(Comparable comparable){ if(isEmpty()) attachKey(comparable); else { Comparable key = (Comparable) getKey(); if(comparable.compareTo(key)==0) throw new IllegalArgumentException("duplicate key"); else if (comparable.compareTo(key)<0) getLeftBST().insert(comparable); else getRightBST().insert(comparable); } } 5 5 1 1 1 1 5
Deletion in a BST • There are three cases: • The node to be deleted is a leaf node. • The node to be deleted has one non-empty child. • The node to be deleted has two non-empty children. CASE 1: DELETING A LEAF NODE Convert the leaf node into an empty tree by using the detachKey method: // In Binary Tree class public Object detachKey( ){ if(! isLeaf( )) throw new InvalidOperationException( ) ; else { Object obj = key ; key = null ; left = null ; right = null ; return obj ; } }
2 2 7 1 1 4 4 15 8 40 3 6 9 9 5 Deleting a leaf node (cont’d) • Example: Delete 5 in the tree below: 7 Delete 5 15 8 40 3 6
20 10 35 5 22 40 3 8 25 6 Deleting a one-child node • CASE 2: THE NODE TO BE DELETED HAS ONE NON-EMPTY CHILD (a) The right subtree of the node x to be deleted is empty. • Example: // Let target be a reference to the node x. BinarySearchTree temp = target.getLeftBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null; target target Delete 10 20 temp 5 35 3 8 22 40 6 25
2 2 7 7 target 15 target 15 1 1 4 4 12 40 8 40 temp 3 6 9 3 6 14 5 12 5 9 14 Deleting a one-child node (cont’d) // Let target be a reference to the node x. BinarySearchTree temp = target.getRightBST(); target.key = temp.key; target.left = temp.left; target.right = temp.right; temp = null; (b) The left subtree of the node x to be deleted is empty. Example: Delete 8
2 2 Delete 7 8 7 1 1 4 4 15 15 9 40 8 40 3 6 3 6 9 5 5 CASE 3: DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#1 Copy the minimum key in the right subtree of x to the node x, then delete the one-child or leaf-node with this minimum key. • Example:
Delete 7 7 6 2 2 15 15 1 1 4 4 8 40 8 40 3 6 3 5 9 9 5 DELETING A NODE THAT HAS TWO NON-EMPTY CHILDREN DELETION BY COPYING: METHOD#2 Copy the maximum key in the left subtree of x to the node x, then delete the one-child or leaf-node with this maximum key. • Example:
Two-child deletion method#1 code // find the minimum key in the right subtree of the target node Comparable min = target.getRightBST().findMin(); // copy the minimum value to the target target.key = min; // delete the one-child or leaf node having the min target.getRightBST().withdraw(min); All the different cases for deleting a node are handled in the withdraw (Comparable key) method of BinarySearchTree class