Balanced Trees Part One
Total Page:16
File Type:pdf, Size:1020Kb
Balanced Trees Part One Balanced Trees ● Balanced search trees are among the most useful and versatile data structures. ● Many programming languages ship with a balanced tree library. ● C++: std::map / std::set ● Java: TreeMap / TreeSet ● Many advanced data structures are layered on top of balanced trees. ● We’ll see several later in the quarter! Where We're Going ● B-Trees (Today) ● A simple type of balanced tree developed for block storage. ● Red/Black Trees (Today/Thursday) ● The canonical balanced binary search tree. ● Augmented Search Trees (Thursday) ● Adding extra information to balanced trees to supercharge the data structure. Outline for Today ● BST Review ● Refresher on basic BST concepts and runtimes. ● Overview of Red/Black Trees ● What we're building toward. ● B-Trees and 2-3-4 Trees ● Simple balanced trees, in depth. ● Intuiting Red/Black Trees ● A much better feel for red/black trees. A Quick BST Review Binary Search Trees ● A binary search tree is a binary tree with 9 the following properties: 5 13 ● Each node in the BST stores a key, and 1 6 10 14 optionally, some auxiliary information. 3 7 11 15 ● The key of every node in a BST is strictly greater than all keys 2 4 8 12 to its left and strictly smaller than all keys to its right. Binary Search Trees ● The height of a binary search tree is the 9 length of the longest path from the root to a 5 13 leaf, measured in the number of edges. 1 6 10 14 ● A tree with one node has height 0. 3 7 11 15 ● A tree with no nodes has height -1, by convention. 2 4 8 12 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Searching a BST 137 73 271 42 161 314 60 Inserting into a BST Inserting into a BST 137 73 271 42 161 314 60 Inserting into a BST 137 73 271 42 161 314 60 Inserting into a BST 137 73 271 42 161 314 60 Inserting into a BST 137 73 271 42 161 314 60 Inserting into a BST 137 73 271 42 161 314 60 Inserting into a BST 137 73 271 42 161 314 60 166 Inserting into a BST 137 73 271 42 161 314 60 166 Deleting from a BST Deleting from a BST DeleteDelete 60 60 from from this this tree, tree, then 73, and then 137. then 73, and then 137. 137 WorkWork this this out out with with a a pencil pencil and and paper,paper, and and we’ll we’ll reconvene reconvene as as a a groupgroup to to do do it it together. together. 73 271 42 161 314 60 166 Deleting from a BST 137 73 271 42 161 314 60 166 Deleting from a BST 137 73 271 42 161 314 60 166 Deleting from a BST 137 73 271 42 161 314 60 166 Deleting from a BST 137 73 271 42 161 314 60 166 Deleting from a BST 137 73 271 42 161 314 166 Deleting from a BST 137 73 271 42 161 314 CaseCase 0: 0: If If the the node node has has justjust no no children, children, just just remove it. 166 remove it. Deleting from a BST 137 73 271 42 161 314 166 Deleting from a BST 137 73 271 42 161 314 166 Deleting from a BST 137 271 42 161 314 166 Deleting from a BST 137 271 42 161 314 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 137 42 271 161 314 CaseCase 1: 1: If If the the node node has has justjust one one child, child, remove remove itit and and replace replace it it with with itsits child. child. 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 42 271 161 314 166 Deleting from a BST 42 271 161 314 166 Deleting from a BST 42 271 161 314 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 137 42 271 161 314 166 Deleting from a BST 161 42 271 161 314 166 Deleting from a BST 161 42 271 314 166 Deleting from a BST 161 42 271 166 314 Deleting from a BST 161 42 271 166 314 CaseCase 2: 2: If If the the node node has has two two children,children, find find its its inorder inorder successorsuccessor (which (which has has zero zero or or oneone child), child), replace replace the the node's node's key with its successor's key, key with its successor's key, thenthen delete delete its its successor. successor. Runtime Analysis ● The time complexity of all these operations is O(h), where h is the height of the tree. ● That’s the longest path we can take. ● In the best case, h = O(log n) and all operations take time O(log n). ● In the worst case, h = Θ(n) and some operations will take time Θ(n). ● Challenge: How do you efficiently keep the height of a tree low? A Glimpse of Red/Black Trees Red/Black Trees ● A red/black tree is a BST with the 110 following properties: ● Every node is either red or black. 107 166 ● The root is black. ● No red node has a red child. 106 161 261 ● Every root-null path in the tree passes through the same number of black nodes. 140 Red/Black Trees ● A red/black tree is a BST with the 53 following properties: ● Every node is either red or black. 31 58 ● The root is black. ● No red node has a red child. 26 41 59 97 ● Every root-null path in the tree passes through the same number of black nodes. Red/Black Trees ● A red/black tree is a BST with the 53 following properties: ● Every node is either red or black. 31 58 ● The root is black. ● No red node has a red child. 26 41 59 97 ● Every root-null path in the tree passes through the same number of black nodes. Red/Black Trees ● A red/black tree is a BST with the 5 following properties: ● Every node is either red or black. 2 7 ● The root is black. ● No red node has a red child. 1 4 8 ● Every root-null path in the tree passes through the same number of black nodes. Red/Black Trees ● A red/black tree is a BST with the 5 following properties: ● Every node is either red or black. 2 7 ● The root is black. ● No red node has a red child. 1 4 8 ● Every root-null path in the tree passes through the same number of black nodes. Red/Black Trees ● A red/black tree is a BST with the 5 following properties: ● Every node is either red or black. 2 7 ● The root is black. ● No red node has a red child. 1 4 8 ● Every root-null path in the tree passes through the same number of black nodes. Red/Black Trees ● Theorem: Any red/black tree with n nodes has height O(log n). ● We could prove this now, but there's a much simpler proof of this we'll see later on. ● Given a fixed red/black tree, lookups can be done in time O(log n). Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 13 Mutating Red/Black Trees 17 7 31 3 11 23 37 13 Mutating Red/Black Trees 17 7 31 3 11 23 37 13 Mutating Red/Black Trees 17 7 31 3 11 23 37 WhatWhat are are we we 13 supposedsupposed to to do do with with thisthis new new node? node? Mutating Red/Black Trees Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 31 3 11 23 37 Mutating Red/Black Trees 17 7 37 3 11 23 37 Mutating Red/Black Trees 17 7 37 3 11 23 Mutating Red/Black Trees 17 7 37 3 11 23 HowHow do do we we fix fix upup thethe black-heightblack-height property? property? Fixing Up Red/Black Trees ● The Good News: After doing an insertion or deletion, we can locally modify a red/black tree in time O(log n) to fix up the red/black properties. ● The Bad News: There are a lot of cases to consider and they're not trivial.