Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 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.

Similar presentations


Presentation on theme: "1 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."— Presentation transcript:

1 1 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 2 Binary Search Trees (Definition) A binary search tree (BST) is a binary tree that is empty or that satisfies the BST ordering property: 1.The key of each node is greater than each key in the left subtree, if any, of the node. 2.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: 6 8 2 41 79 53 AA B C D

3 3 Why BST DeletionInsertionRetrievalData Structure O(log n) FAST O(log n) FAST O(log n) FAST BST O(n) SLOW O(n) SLOW O(log n) FAST* Sorted Array O(n) SLOW O(n) SLOW O(n) SLOW Sorted Linked List 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

4 4 Binary Search Tree Implementation Since search trees are designed to support efficient searching they implement the SearchableContainer interface. The class hierarchy of BSTs is MyComparable Container Tree AbstractObject AbstractContainer AbstractTree GeneralTree BinarySearchTree SearchableContainerSearchTree

5 5 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 }

6 6 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 10return getRightBST( ).find(comparable) ; 11 }

7 7 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 }

8 8 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. 20 30 10 154 25 40 35 32 7 9 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 }

9 9 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 }

10 10 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 } 1 6 8 2 4 79 3 56 8 2 4 79 3 5 6 8 2 4 79 3 5 6 8 2 4 79 3 5 11 1

11 11 Deletion in BST There are three cases: 1.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 15 2 41 840 63 9 5 Delete 6 7 15 2 41 840 53 9

12 12 Deletion in BST (Contd.) 2.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 15 2 41 840 63 9 5 Delete 8 7 15 2 41 940 63 5 Note: Deletion a leaf node is a special case of any of case1 & case2.

13 13 Deletion by copying 3.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: 7 15 2 41 840 63 9 5 Delete 7 8 15 2 41 940 63 5

14 14 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: 7 15 2 41 840 63 9 5 Delete 7 6 15 2 41 940 53

15 15 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: 7 15 2 41 840 63 9 5 The right subtree of node7 Delete 7 2 41 63 15 8 40 9 5 is attached as the right subtrees of the rightmost node in the left subtree of node7

16 16 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: 7 15 2 41 840 63 9 5 Delete 7 The left subtree of node7 is attached as the left subtrees of the leftmost node in the right subtree of node7 15 840 9 2 41 63 5

17 17 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") ; 4MyComparable 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 }


Download ppt "1 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."

Similar presentations


Ads by Google