9/14/2013

COMPSCI 105 Principles of Computer Science

Content
1. Binary Search Tree Definition 2. ADT Binary Search Tree 3. Operations on ADT

RESOURCES: Lecture Notes Textbook: - Section 11.3 – Binary Search Trees - Exercises

Binary Search Trees

Binary Search Tree
Retrieve (Search) Insert Delete

1. 2. 3.

2

COMPSCI 105 S2 C

1. Binary Search Tree Definition
 A Binary Search Tree is a Binary Tree, which for each node n

Binary Search Tree Example
Example: All binary search trees for the values 1,2 and 3 are

satisfies the following three properties:

– n’s value is greater than all values in its left subtree TL – n’s value is less than all values in its right subtree TR – Both TL and TR are Binary Search Trees
3 COMPSCI 105 S2 C

60

20

70

10

40

30

50

4

COMPSCI 105 S2 C

1

this. public Person(String id. supertype of KT } // end constructor public KT getKey() { return searchKey. private Address address. } // end toString …} 7 COMPSCI 105 S2 C Tree  retrieve(searchKey)  Insert a new item into a Binary Search Tree  insert(newItem)  Delete the item with a given search key from a Binary Search Tree  delete(searchKey) 8 COMPSCI 105 S2 C 2 . } // end constructor This initialises the key in the super public String toString() { Class with the id return getKey() + "#" + name. Operations of ADT Binary Search Tree  Retrieve the item with a given search key from a Binary Search public class Person extends KeyedItem<String> { private FullName name. address = addr. private String phoneNumber. Address addr) { super(id). String phone. 3. FullName name. Public KeyedItem(KT key) { “Wildcard” representing any searchKey = key.9/14/2013 2. } // end getKey } // end KeyedItem COMPSCI 105 S2 C Search Key Example  A Person class with the person’s ID number as the search key. phoneNumber = phone. ADT Binary Search Tree  A Binary Search Tree is a ADT Binary Search Tree (con’d)  A data item in a Binary Search Tree has a specifically designated search key Binary Tree which fulfills the Binary Search Tree properties  In order to guarantee that properties are fulfilled the access operations must be restricted  In order to find and compare elements we need a searchKey which is “comparable” The nodes of this tree have a searchKey attribute to compare and identify them 5 COMPSCI 105 S2 C 6 which is used to find items in the tree Public abstract class KeyedItem<KT extends Comparable<? super KT>> { private KT searchKey.name = name.

i. } // end if } // end if return treeItem.9/14/2013 3.getRight().getItem(). return null 11 COMPSCI 105 S2 C “Retrieve” algorithm protected KeyedItem retrieveItem(TreeNode<T> tNode.compareTo(nodeItem. searchKey).getKey()) < 0) { // search the left subtree treeItem = retrieveItem(tNode.getKey()) == 0) { // item is in the root (of the current subtree) treeItem = (KeyedItem)tNode. } else if (searchKey. KT searchKey) { T treeItem. } else { // search the right subtree treeItem = retrieveItem(tNode.getItem(). if (searchKey.  For a node n all items in its left subtree are smaller than n  For a node n all items in its right subtree are 20 70 3<8 3<5 3>2 50 10 40 larger than n  The subtrees are again Binary Search Trees  ALGORITHM  If (tree==null) return null (item not found) 30 Else compare searchKey with key of node If smaller search recursively in left subtree If larger search recursively in right subtree 9 COMPSCI 105 S2 C 10 COMPSCI 105 S2 C Found “3” “Retrieve” algorithm example Search for “6” 6<8 6>5 Right child of 5 is null => Item not found.compareTo(nodeItem. } else { T nodeItem = tNode. } // end retrieveItem 12 COMPSCI 105 S2 C 3 .getLeft().1 “Retrieve” algorithm  Use properties defining the Binary Search “Retrieve” algorithm example Search for “3” 60 Key. if (tNode == null) { treeItem = null.e. searchKey).

null).getItem().getLeft().9/14/2013 3.setRight(newSubtree). KT newItem){ TreeNode<T> newSubtree.getKey()) < 0) { // search the left subtree newSubtree = insertItem(tNode.getKey(). newItem).compareTo(nodeItem.getRight(). Else compare key of new item with key of node If smaller insert recursively in left subtree If larger insert recursively in right subtree 7 50 30 13 COMPSCI 105 S2 C 14 COMPSCI 105 S2 C Right subtree empty => insert new node “7” “Insert” algorithm example Insert “6” 6>5 6<7 7 6 15 COMPSCI 105 S2 C “Insert” algorithm protected TreeNode insertItem(TreeNode<T> tNode. return tNode. // search for the insertion position if (newItem. tNode. newItem).setLeft(newSubtree). null. insert as new leaf tNode = new TreeNode<T>(newItem. } // end if }COMPSCI 105 S2 C // end insertItem 6<8 Left subtree empty => insert new node “6” 16 4 . } // end if T nodeItem = tNode.2 “Insert” algorithm  Similar to the find algorithm  Note that algorithm also works if there are 60 “Insert” algorithm example Insert “7” 7<8 20 70 duplicate keys!! 10 7>5 40 ALGORITHM: If (node == null) make new node with the new item. if (tNode == null) { // position of insertion found. } else { // search the right subtree newSubtree = insertItem(tNode. return tNode. return tNode. tNode.

tNode. can be removed with case 1 or case 2(b)) 3.Case 2 (node has only one child) Replace N with N’s child (node that this can be a subtree) N has only a left child after delete N 20 COMPSCI 105 S2 C 19 5 . N has two children 6=6 => Found “6” is leaf => Delete 17 COMPSCI 105 S2 C Delete algorithm protected TreeNode deleteItem(TreeNode<T> tNode. KT searchKey){ TreeNode<T> newSubtree.Case 1 (node is leaf) Delete “6” 6>5 6<7 7 6 18 COMPSCI 105 S2 C N is a leaf => Delete N has only one child   6<8 2(a): N has a left child => Replace N with left child 2(b): N has a right child => Replace N with right child => Replace N with its successor in inorder.compareTo(nodeItem.getRight().getLeft().getKey()) == 0) { // item is in the root of some subtree tNode = deleteNode(tNode). if (searchKey.getKey()) < 0) { // search the left subtree newSubtree = deleteItem(tNode.setLeft(newSubtree). i. } // end deleteItem COMPSCI 105 S2 C “Delete” Algorithm .compareTo(nodeItem. i.3 “Delete” algorithm   Locate the node N containing item with the specified search key (like “retrieve” operation) Three cases for the node N containing the item to be deleted 1. (Note: Since the successor is the leftmost node. searchKey). } // delete the item else if (searchKey.getItem().9/14/2013 3. 2. } // end if } // end if return tNode.e. it does not have a left child. } else { T nodeItem = tNode. if (tNode == null) { throw new TreeException("TreeException: Item not found"). tNode. the leftmost node of the right subtree. searchKey).setRight(newSubtree). “Delete” Algorithm Example . } else { // search the right subtree newSubtree = deleteItem(tNode.e.

Case 2 (node has only one child) Delete “7” 7>5 7 6 “Delete” Algorithm Example .Case 3 (node has two children)  Replace N with a node M which has maintains the Binary Search Tree “Delete” Algorithm Example .Case 3 (node has two children) Delete “9” 9>7 “9” has two children => Find => leftmost element in Found 6 10 9 9=9 right subtree “11” is root of right subtree 11 Go left until left child is 8 “10”null.9/14/2013 “Delete” Algorithm Example . has no left child => 10 Replace “9” with “10” and delete node 7 property and is easy to remove – Rather than replacing the node N we just copy the key from M to N – Then we delete M – M is the inorder successor of N [= the smallest node greater than N = the leftmost node in the right subtree) 23 COMPSCI 105 S2 C 24 COMPSCI 105 S2 C 6 .Case 3 (node has two children)  Locate another node M that is easier to remove from the tree than node N  Copy the item that is in M to N. thus effectively deleting from the 7<8 7=7 => Found “7” has only left child 6 => Delete and replace with child tree the item originally in N  Remove the node M from the tree  NOTE: M must be chosen carefully so that Binary Search tree properties are maintained! WRONG SOLUTION!!!  22 COMPSCI 105 S2 C 21 COMPSCI 105 S2 C “Delete” Algorithm Example .

getLeft() == null) { // has only right child return tNode. } // end if COMPSCI 105 S2 C 25 } // end deleteNode Summary  The Binary Search Tree allows to use a binary search like algorithm to search for an item with a specific value operations  The shape of a Binary Search Tree determines the efficiency of its  The height of a binary search tree with n nodes can range from a minimum  The closer a Binary Search Tree is to a balanced tree. Case 2(a) } // end if no left child else if (tNode.setItem(replacementItem). return tNode. the more efficient of log2(n + 1) to a maximum of n 26 COMPSCI 105 S2 C 7 . if ( (tNode.setRight(deleteLeftmost(tNode. } // end if leaf else if (tNode.getRight() == null) { // has only left child return tNode.getLeft() == null) && Case 1 (tNode. tNode. tNode.getLeft().getRight(). Case 2(b) } // end if no right child else { // there are two children // retrieve and delete the inorder successor Case 3 replacementItem = findLeftmost(tNode.getRight()).9/14/2013 Delete a Node algorithm protected TreeNode deleteNode(TreeNode<T> tNode) { T replacementItem.getRight())).getRight() == null) ) { // is a leaf node return null.