Documente Academic
Documente Profesional
Documente Cultură
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
1. 2. 3.
COMPSCI 105 S2 C
ns value is greater than all values in its left subtree TL ns 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
COMPSCI 105 S2 C
9/14/2013
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
COMPSCI 105 S2 C
key.
public class Person extends KeyedItem<String> { private FullName name; private String phoneNumber; private Address address; public Person(String id, FullName name, String phone, Address addr) { super(id); this.name = name; phoneNumber = phone; address = addr; } // end constructor This initialises the key in the super public String toString() { Class with the id return getKey() + "#" + name; } // end toString }
7 COMPSCI 105 S2 C
Tree
retrieve(searchKey)
Delete the item with a given search key from a Binary Search Tree
delete(searchKey)
COMPSCI 105 S2 C
9/14/2013
Key, i.e.
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
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
protected KeyedItem retrieveItem(TreeNode<T> tNode, KT searchKey) { T treeItem; if (tNode == null) { treeItem = null; } else { T nodeItem = tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey()) == 0) { // item is in the root (of the current subtree) treeItem = (KeyedItem)tNode.getItem(); } else if (searchKey.compareTo(nodeItem.getKey()) < 0) { // search the left subtree treeItem = retrieveItem(tNode.getLeft(), searchKey); } else { // search the right subtree treeItem = retrieveItem(tNode.getRight(), searchKey); } // end if } // end if return treeItem; } // end retrieveItem
12 COMPSCI 105 S2 C
9/14/2013
duplicate keys!!
10
7>5
40
ALGORITHM: If (node == null) make new node with the new item. 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
Insert algorithm
protected TreeNode insertItem(TreeNode<T> tNode, KT newItem){ TreeNode<T> newSubtree; if (tNode == null) { // position of insertion found; insert as new leaf tNode = new TreeNode<T>(newItem, null, null); return tNode; } // end if T nodeItem = tNode.getItem(); // search for the insertion position if (newItem.getKey().compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = insertItem(tNode.getLeft(), newItem); tNode.setLeft(newSubtree); return tNode; } else { // search the right subtree newSubtree = insertItem(tNode.getRight(), newItem); tNode.setRight(newSubtree); return tNode; } // end if }COMPSCI 105 S2 C // end insertItem
6<8
16
9/14/2013
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. 2.
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, i.e. the leftmost node of the right subtree. (Note: Since the successor is the leftmost node, it does not have a left child, i.e. can be removed with case 1 or case 2(b))
3.
17
COMPSCI 105 S2 C
Delete algorithm
protected TreeNode deleteItem(TreeNode<T> tNode, KT searchKey){ TreeNode<T> newSubtree; if (tNode == null) { throw new TreeException("TreeException: Item not found"); } else { T nodeItem = tNode.getItem(); if (searchKey.compareTo(nodeItem.getKey()) == 0) { // item is in the root of some subtree tNode = deleteNode(tNode); } // delete the item else if (searchKey.compareTo(nodeItem.getKey()) < 0) { // search the left subtree newSubtree = deleteItem(tNode.getLeft(), searchKey); tNode.setLeft(newSubtree); } else { // search the right subtree newSubtree = deleteItem(tNode.getRight(), searchKey); tNode.setRight(newSubtree); } // end if } // end if return tNode; } // end deleteItem COMPSCI 105 S2 C
after delete N
20 COMPSCI 105 S2 C
19
9/14/2013
node N
7<8 7=7 => Found 7 has only left child 6 => Delete and replace with child
Remove the node M from the tree NOTE: M must be chosen carefully
21
COMPSCI 105 S2 C
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
9/14/2013
Summary
The Binary Search Tree allows to use a binary search like algorithm to
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, the more efficient
of log2(n + 1) to a maximum of n
26
COMPSCI 105 S2 C