170 likes | 314 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
MyComparable AbstractObject Container AbstractContainer Tree AbstractTree GeneralTree BinarySearchTree SearchableContainer SearchTree Binary Search Tree Implementation • Since search trees are designed to support efficient searching they implement the SearchableContainer interface. • The class hierarchy of BSTs is
Binary Search Tree Implementation (Contd.) • The SearchTree interface is defined as: 1 public interface SearchTree extends Tree, SearchableContainer{ 2 public abstract MyComparable findMin( ) ; 3 public abstract MyComparable findMax( ) ; 4 } • The BinarySearchTree class inherits the instance variables key, left, and right of the BinaryTree class: 1 public class BinarySearchTree extends BinaryTree implements SearchTree{ 2 private BinarySearchTree getLeftBST( ) 3 { return (BinarySearchTree) getLeft( ) ; } 4 5 private BinarySearchTree getRightBST( ) 6 { return (BinarySearchTree) getRight( ) ; } 7 // . . . 8 }
Binary Search Tree Implementation (Contd.) • The find method of the BinarySearchTree class: 1 public MyComparable find(MyComparable comparable)\{ 2 if(isEmpty( )) 3 return null ; 4 MyComparable key = (MyComparable) getKey( ) ; 5 if(comparable.isEQ(key)) 6 return key ; 7 else if(comparable.isLT(key)) 8 return getLeftBST( ).find(comparable) ; 9 else • return getRightBST( ).find(comparable) ; • }
Binary Search Tree Implementation (Contd.) • 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. 1 public MyComparable findMin( ){ 2 if(isEmpty( )) 3 return null ; 4 if(getLeftBST( ).isEmpty( )) 5 return (MyComparable) getKey( ) ; 6 else 7 return getLeftBST( ).findMin( ) ; 8 }
20 32 9 30 35 7 10 40 25 4 15 Binary Search Tree Implementation (Contd.) • 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. 1 public MyComparable findMax( ) { 2 if(isEmpty( )) 3 return null ; 4 if(getRightBST( ).isEmpty( )) 5 return (MyComparable) getKey( ) ; 6 else 7 return getRightBST( ).findMax( ) ; 8 }
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: 1 public void attachKey(Object obj) 2 { 3 if(! isEmpty( )) 4 throw new InvalidOperationException( ) ; 5 else 6 { 7 key = obj ; 8 left = new BinarySearchTree( ) ; 9 right = new BinarySearchTree( ) ; 10 } 11 }
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 1 public void insert (MyComparable comparable) { 2 if(isEmpty( )) 3 attachKey(comparable) ; 4 else 5 { 6 MyComparable key = (MyComparable) getKey( ) ; 7 if(comparable.isEQ(key)) 8 throw new IllegalArgumentException("duplicate key" ) ; 9 else if(comparable.isLT(key)) 10 getLeftBST( ).insert(comparable) ; 11 else 12 getRightBST( ).insert(comparable) ; 13 } 14 } 5 5 1 1 1 1 5
2 2 7 1 1 4 4 15 8 40 3 6 9 9 5 Deletion in BST • There are three cases: • The right subtree of the node x to be deleted is empty. Make the reference pointing to x point to the left child of x, if any. After this, delete x. • Example: 7 Delete 6 15 8 40 3 5
2 2 7 1 1 4 4 15 8 40 3 6 5 5 9 Deletion in BST (Contd.) • The left subtree of the node x to be deleted is empty. • Make the reference pointing to x point to the right child of x, if any. After this, delete x. • Example: 7 Delete 8 15 9 40 3 6 • Note: Deletion a leaf node is a special case of any of case1 & case2.
2 2 7 1 1 4 4 15 8 40 3 6 9 5 5 Deletion by copying • Both subtrees of the node x to be deleted are not empty. • Four deletion methods are possible: (a) Copy the minimum key in the right subtree of x to the node x, then delete the node with this minimum key. • Example: Delete 7 8 15 9 40 3 6
2 2 7 1 1 4 4 15 8 40 3 6 9 5 Deletion by copying (Contd.) (b) Copy the maximum key in the left subtree of x to the node x, then delete the node with this maximum key. • Example: Delete 7 6 15 9 40 3 5
2 2 7 3 6 1 1 4 4 15 15 40 8 40 8 9 3 6 9 5 5 Deletion by Merging (c) Make the right subtree of x to be the right subtree of the rightmost node in the left subtree of x. After this, delete x. • Example: Delete 7 The right subtree of node7 is attached as the right subtrees of the rightmost node in the left subtree of node7
2 2 15 7 1 1 4 4 8 40 15 8 40 3 6 3 6 9 5 5 9 Deletion by Merging (Contd.) (c) Make the left subtree of x to be the left subtree of the leftmost node in the right subtree of x. After this, delete x. • Example: Delete 7 is attached as the left subtrees of the leftmost node in the right subtree of node7 The left subtree of node7
Deletion by Copying CODE • Here is the deletion by Copying Code: 1 public void withdraw(MyComparable comparable){ 2 if(isEmpty( )) 3 throw new IllegalArgumentException("object notfound") ; 4 MyComparable key = (MyComparable) getKey( ) ; 5 if(comparable.isLT(key)) 6 getLeftBST( ).withdraw(comparable) ; 7 else if(comparable.isGT(key)) 8 getRightBST( ).withdraw(comparable) ; 9 else{ // the key is found 10 if(isLeaf( )) detachKey( ) ; 11 else if( ! getLeftBST( ).isEmpty( )){ 12 MyComparable max = getLeftBST( ).findMax( ) ; 13 super.key = max ; 14 getLeftBST( ).withdraw(max) ; } 15 else { // the right subtree is not empty 16 MyComparable min = getRightBST( ).findMin( ) ; 17 super.key = min ; 18 getRightBST( ).withdraw(min) ; } 19 } 20 }