COMP3600/6466 Algorithms 2018 Lecture 12 1 Binary search trees Reading: Cormen et al, Sections 12.1 to 12.3 Binary search trees are data structures based on binary trees that support operations on dynamic sets. Each element contains a key. There is a (total) order on the set of keys. Typical operations include: Search Insert Delete Minimum Maximum Predecessor Successor 1
COMP3600/6466 Algorithms 2018 Lecture 12 2 Binary search trees 2 The keys in a binary search tree are always stored in such a way as to satisfy the binary-search-tree property : Let x be a node in a binary search tree. If y is a node in the left subtree of x, then y.key x.key. If y is a node in the right subtree of x, then y.key x.key. In addition to a key and satellite data, each node x in a binary tree has three attributes that are pointers: parent pointer x.p left child pointer x.left right child pointer x.right 2
Binary search trees 3 Cormen et al, p.287 Two binary search trees each containing the keys 2, 5, 5, 6, 7, 8 The worst-case running time for most binary-search-tree operations is proportional to the height of the tree. So the tree on the left is preferable to the tree on the right. We want O(lg n) performance, where n is the number of nodes. COMP3600/6466 Algorithms 2018 Lecture 12 3 3
COMP3600/6466 Algorithms 2018 Lecture 12 4 Traversing binary search trees There are three natural ways to traverse a binary search tree: Inorder tree walk: visit the left subtree, then the root, then the right subtree Preorder tree walk : visit the root, then the left subtree, then the right subtree Postorder tree walk: visit the left subtree, then the right subtree, then the root. 4
COMP3600/6466 Algorithms 2018 Lecture 12 5 Traversing binary search trees 2 Cormen et al, p.288 For Preorder-Tree-Walk, the print statement comes first. For Postorder-Tree-Walk, the print statement comes last. Theorem: If x is the root of an n-node subtree, then the call Inorder-Tree-Walk(x) takes Θ(n) time. The proof is by finding a recurrence and solving it by the substitution method. (See p.288) 5
COMP3600/6466 Algorithms 2018 Lecture 12 6 Traversing binary search trees 3 Cormen et al, p.290 Inorder traversal : 2, 3, 4, 6, 7, 9, 13, 15, 17, 18, 20 Preorder traversal: 15, 6, 3, 2, 4, 7, 13, 9, 18, 17, 20 Postorder traversal: 2, 4, 3, 9, 13, 7, 6, 17, 20, 18, 15 6
COMP3600/6466 Algorithms 2018 Lecture 12 7 Searching binary search trees Given a pointer to the root of the tree and a key k, Tree-Search returns a pointer to a node with key k if one exists; otherwise, it returns NIL. Cormen et al, p.290 The running time of Tree-Search is O(h), where h is the height of the tree. 7
COMP3600/6466 Algorithms 2018 Lecture 12 8 Searching binary search trees 2 Searching for the node with key 13 traverses the path having nodes with keys 15, 6, 7, and 13. 8
COMP3600/6466 Algorithms 2018 Lecture 12 9 Finding the minimum and maximum Here are the procedures for finding the nodes with the minimum and maximum keys in a binary search tree. Cormen et al, p.291 The running time of Tree-Minimum and Tree-Maximum are both O(h), where h is the height of the tree. Exercise: Use induction to prove that these procedures are correct. 9
COMP3600/6466 Algorithms 2018 Lecture 12 10 Finding the minimum and maximum Finding the minimum traverses the path having nodes with keys 15, 6, 3, and 2. Finding the maximum traverses the path having nodes with keys 15, 18, and 20. 10
COMP3600/6466 Algorithms 2018 Lecture 12 11 Finding the successor and predecessor Let s concentrate on successors. If all keys are distinct, the successor of a node x is the node with the smallest key greater than x.key. Exercise: Consider a binary tree T whose keys are distinct. Show that if the right subtree of a node x in T is empty and x has a successor y, then y is the lowest ancestor of x whose left child is also an ancestor of x. (Recall that every node is its own ancestor.) 11
COMP3600/6466 Algorithms 2018 Lecture 12 12 Finding the successor Cormen et al, p.292 The running time of Tree-Successor O(h), where h is the height of the tree. Exercise: Write the procedure Tree-Predecessor. 12
COMP3600/6466 Algorithms 2018 Lecture 12 13 Insertion and deletion The operations of insertion and deletion cause the dynamic set represented by a binary search tree to change. The data structure must be modified to reflect this change, but in such a way that the binary-search-tree property continues to hold. Inserting a node with a new key into a binary search tree is straightforward. However, deleting a node from a binary search tree is rather more complicated. In the Tree-Insert procedure on the next slide, suppose a node with a key κ is to be inserted into a binary search tree. Then the procedure has a node z as an argument, where z.key = κ, z.left = NIL, and z.right = NIL. 13
Insertion Cormen et al, p.295 Cormen et al, p.294 The running time of Tree-Insert is O(h), where h is the height 14 of the tree. COMP3600/6466 Algorithms 2018 Lecture 12 14
Deletion Cormen et al, Second edition In case (c), any external pointers to the node with key 6 become stale. 15 COMP3600/6466 Algorithms 2018 Lecture 12 15
COMP3600/6466 Algorithms 2018 Lecture 12 16 Deletion 2 The procedure for deleting a given node z from a binary search tree T takes as arguments pointers to T and z. There are four cases to consider. If z has no left child, then we replace z by its right child, which may or may not be NIL. If z has just one child, which is its left child, then we replace z by its left child. Otherwise, z has both a left and a right child. We find z s successor y, which lies in z s right subtree and has no left child. If y is z s right child, then we replace z by y, leaving y s right child alone. Otherwise, y lies within z s right subtree, but is not z s right child. In this case, we first replace y by its own right child, and then we replace z by y. 16
Deletion 3 Cormen et al, p.297 17 COMP3600/6466 Algorithms 2018 Lecture 12 17
COMP3600/6466 Algorithms 2018 Lecture 12 18 Deletion 4 For the deletion operation we use the procedure Transplant. It replaces the subtree rooted at u with the subtree rooted at v. Cormen et al, p.296 18
Deletion 5 Cormen et al, p.298 The running time of Tree-Delete is O(h), where h is the height of the tree. 19 COMP3600/6466 Algorithms 2018 Lecture 12 19
COMP3600/6466 Algorithms 2018 Lecture 12 20 Randomly built binary search trees Define a randomly built binary search tree on n keys as one that arises from inserting the keys in random order into an initially empty tree, where each of the n! permutations of the input keys is equally likely. Theorem: The average height of a randomly built binary search tree on n distinct keys is O(lg n). For the proof, see p.300-303. However, generally we cannot rely on randomness to ensure that the height is logarithmic in the number of nodes. We need to consider a class of balanced trees that have this property by their definition and the way insertion and deletion operations are carried out. 20