Binary Search Trees BST What is a Binary
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 1
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 A 2 1 8 4 3 7 5 B 9 C D 2
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: Data Structure Retrieval Insertion Deletion BST O(log n) FAST Sorted Array O(log n) FAST* O(n) SLOW Sorted Linked List O(n) SLOW *using binary search 3
Binary Search Tree Implementation • The Binary. Search. Tree class inherits the instance variables key, left, and right of the Binary. Tree class: public class Binary. Search. Tree extends Binary. Tree implements Searchable. Container { private Binary. Search. Tree get. Left. BST(){ return (Binary. Search. Tree) get. Left( ) ; } private Binary. Search. Tree get. Right. BST( ){ return (Binary. Search. Tree) get. Right( ) ; } //. . . } 4
Binary Search Tree Implementation (Cont’d) • The find method of the Binary. Search. Tree class: public Comparable find(Comparable comparable) { if(is. Empty()) return null; Comparable key = (Comparable) get. Key(); if(comparable. compare. To(key)==0) return key; else if (comparable. compare. To(key)<0) return get. Left. BST(). find(comparable); else return get. Right. BST(). find(comparable); } 5
Binary Search Tree Implementation (Cont’d) • The find. Min method of the Binary. Search. Tree 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 find. Min() { if(is. Empty()) return null; if(get. Left. BST(). is. Empty()) return (Comparable)get. Key(); else return get. Left. BST(). find. Min(); } 6
Binary Search Tree Implementation (Cont’d) • The find. Max method of the Binary. Search. Tree class: • By the BST ordering property, the maximum key is the key of the right-most node that has an empty right-subtree. 20 public Comparable find. Max() { if(is. Empty()) return null; if(get. Right. BST(). is. Empty()) return (Comparable)get. Key(); else return get. Right. BST(). find. Max(); } 10 4 15 9 7 30 25 40 32 35 7
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 attach. Key method: public void attach. Key(Object obj) { if(!is. Empty()) throw new Invalid. Operation. Exception(); else { key = obj; left = new Binary. Search. Tree(); right = new Binary. Search. Tree(); } } 8
Insertion in a BST public void insert(Comparable comparable){ if(is. Empty()) attach. Key(comparable); else { Comparable key = (Comparable) get. Key(); if(comparable. compare. To(key)==0) throw new Illegal. Argument. Exception("duplicate key"); else if (comparable. compare. To(key)<0) get. Left. BST(). insert(comparable); else get. Right. BST(). insert(comparable); } } 6 5 6 2 1 4 3 5 8 7 9 2 1 3 7 5 2 8 4 6 6 9 1 4 3 2 8 7 9 1 8 4 3 7 9 5 9
Deletion in a BST • There are three cases: 1. The node to be deleted is a leaf node. 2. The node to be deleted has one non-empty child. 3. 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 detach. Key method: // In Binary Tree class public Object detach. Key( ){ if(! is. Leaf( )) throw new Invalid. Operation. Exception( ) ; else { Object obj = key ; key = null ; left = null ; right = null ; return obj ; } } 10
Deleting a leaf node (cont’d) • Example: Delete 5 in the tree below: 7 7 Delete 5 2 15 4 3 8 6 40 9 1 15 4 3 8 6 40 9 5 11
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. // Let target be a reference to the node x. Binary. Search. Tree temp = target. Left. BST(); target. key = temp. key; target. left = temp. left; target. right = temp. right; temp = null; • Example: target 20 10 temp 5 3 target 35 22 8 6 Delete 10 5 40 25 20 3 35 8 6 22 40 25 12
Deleting a one-child node (cont’d) (b) The left subtree of the node x to be deleted is empty. // Let target be a reference to the node x. Binary. Search. Tree temp = target. Right. BST(); target. key = temp. key; target. left = temp. left; target. right = temp. right; temp = null; Example: 7 7 2 1 target 4 3 8 6 5 40 12 9 2 Delete 8 15 temp 14 1 15 target 4 3 12 6 9 40 14 5 13
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 8 2 15 4 3 8 6 5 40 9 1 15 4 3 9 40 6 5 14
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: Delete 7 7 6 2 15 4 3 8 6 40 9 1 15 4 3 8 5 40 9 5 15
Two-child deletion method#1 code // find the minimum key in the right subtree of the target node Comparable min = target. Right. BST(). find. Min(); // copy the minimum value to the target. key = min; // delete the one-child or leaf node having the min target. Right. BST(). withdraw(min); All the different cases for deleting a node are handled in the withdraw (Comparable key) method of Binary. Search. Tree class 16
- Slides: 16