Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Binary Search Trees Ric Glassey [email protected] Outline • Binary Search Trees – Aim: Demonstrate how a BST can maintain order and fast performance relative to its height – Properties – Operations • • • • Min/Max Search Insertion *Deletion – Summary of performance 2 PROPERTIES 3 Binary Search Tree • Ordered binary tree • Binary search tree property – Keys in left sub-tree of P are < k – Keys in right sub-tree of P are > k P k <k >k 4 Example Binary Search Tree 44 17 8 88 32 28 65 97 41 Any sub-‐tree of the BST should sa5sfy the binary-‐search-‐tree property 5 Applications of BST • Store a set of ordered keys • Fast operations and maintain order – Priority Queues – Ordered Maps/Dictionaries • e.g. Java’s TreeMap • n.b. if order is not important, regular hash tables still offer better average time complexity O(1) 6 MIN/MAX 7 Finding Min/Max in a BST • What is the fastest path to find the minimum and maximum values in a BST? 44 17 8 88 32 28 65 97 41 8 Finding Min/Max in a BST min(node) while node.left != null: node = node.left return node max(node) while node.right != null: node = node.right return node What is the expected 5me complexity of these opera5ons? And the worst case? 9 Pathological BSTs height of tree = number of nodes 44 insert(46) 46 insert(54) 54 insert(67) 67 insert(71) 71 insert(88) Order of inserts cannot be predicted in advance Pathological cases can emerge where some opera5ons will take O(n) Solu5ons include Randomised BSTs/Treaps and Balancing Procedures 88 10 SEARCH 11 Finding x within a BST • x = 41 44 17 8 88 32 28 65 97 41 How many opera5ons will it take? N? more/less? 12 Finding x within a BST • x = 41 x < 44 44 17 88 x > 17 8 32 65 97 x > 32 28 41 x = 41 Once more, the complexity was propor5onal to the height of the tree O(h) 13 BST Search Algorithms • Recursive approach: search(node, x) if node == null or x == node.key: return node if x < node.key: return search(node.left, x) else: return search(node.right, x) • Iterative option (see homework) – Why is this the better option? 14 BST Search Algorithms • Further operations use search(n,x) as a subroutine – insert(k, v) – remove(k) • As a consequence, all primary operations on a BST are considered fast, proportional to the height of the tree – An exception is in-order-traversal which should be expected to be O(n) as all nodes ‘must’ be visited 15 INSERTION 16 Insertion into a BST • Search for key • Two cases: – Key exists, update value – Key does not exist, extend leaf at end of failed search with new node (key, value) 17 Insertion into a BST: insert(68, value) 44 17 88 8 32 97 54 28 21 65 29 82 93 76 80 18 Insertion into a BST: Find position 44 68 > 44 17 88 8 32 68 < 88 68 > 65 54 28 21 65 29 82 97 93 68 < 82 76 68 < 76 ??? 80 19 Insertion into a BST: Extend Leaf 44 68 > 44 17 88 8 32 28 21 65 68 > 65 54 29 97 68 < 88 82 93 68 < 82 76 68 < 76 68 80 20 BST Insertion Algorithm • We can use search(x) as a subroutine to simplify insertion into a BST – n.b. homework demands iterative solution! insert(key, value) leaf = search(root, key) if key == leaf.key: leaf.value = value else if key < leaf.key: leaf.left = node(key, value) else: leaf.right = node(key, value) 21 *DELETION 22 *Deletion for a BST • Insertions always occur at a leaf (trivial) • Deletions can occur anywhere within a tree • Three cases to consider for remove(z): – Case 1: z has no children – Case 2: z has one child – Case 3: z has two children 23 *Case 1: z has no children • Simply remove from tree by setting z’s parent’s pointer to null z 24 *Case 2: z has one child R R LC Z LC R R RC Z RC 25 *Case 3: z has two children (i) R R Z LC X X LC Y Y X is promoted, linked to parent R of Z, and Z’s LC is linked as X’s leO child 26 *Case 3: z has two children (ii) R R R Z Y Z LC X LC LC Y X Y W X W W We transplant Y with X first, and relink W to X to maintain the BST-‐property Finally, we link Z’s LC to Y and Y to the root R Hint: subs5tute valid integers to show why this works 27 Summary of BST Performance Data Array Structure Linked List† Opera5on Hash Table Binary Search Tree average worst expected worst Search* O(1) O(n) O(1) O(n) O(h) O(n) Insert O(n) O(1) O(1) O(n) O(h) O(n) Delete O(n) O(1) O(1) O(n) O(h) O(n) * Index or Key based search † Assume doubly linked list 28 Readings • Algorithms and Data Structures *required* – Stefan Nilsson’s text on the Binary Search Tree – http://www.nada.kth.se/~snilsson/algoritmer/trad/ • Introduction to Algorithms, 3rd Edition – Chapter 12: Binary Search Trees – Full text available via KTH Library • http://kth-primo.hosted.exlibrisgroup.com/ KTH:KTH_SFX2560000000068328 • Data Structures and Algorithms in Java, 6th Edition – Goodrich et al. – Chapter 11: Search Trees – Full text available via KTH Library • http://kth-primo.hosted.exlibrisgroup.com/ KTH:KTH_SFX3710000000333147 29 Feedback! • This week, I will mostly be asking about: – Topics of today’s lecture – Analysis of algorithms – Tipjar • Survey will appear at this link – http://bit.ly/1EfJE91 – Also on the course web page after this lecture – n.b. I will switch survey service for P4 :-) 30