CSS 343 Data Structures, Algorithms, and Discrete Math II Balanced Search Trees Yusuf Pisan
Height Height of a tree impacts how long it takes to find an item Balanced tree O(log n) vs Degenerate tree O(n) What can we do? Reduce height - have 3, 4 or more children for each node Keep tree balanced 2-3 Tree Up to 3 children: left, middle, right Up to 2 data items: small (S) and large (L) S is greater than each item in left subtree S is smaller than each item in middle subtree L is larger than each item in middle subtree All leaves at same level 2-node: single data, 2 children 3-node: 2 data, 3 children 2
2-3 Insertion 1. Insert into the appropriate node 2. If node has 3 data value (overfull), the node splits and the middle data gets sent to the parent. 3. If the parent node is overfull, the parent splits and the middle data gets set to the parent's parent Inserting 10, 20, 30, 40, 50, 60 below. Exercise: Insert prime numbers 2 to 19 3
2-3 Inorder Traversal if at a leaf node visit data item(s) else if node has 1 item inorder left subtree visit data item inorder right subtree else // must have 2 data items inorder left subtree visit small data item inorder middle subtree visit large data item inorder right subtree 4
Delete 70 1. Swap 70 with inorder successor, 80 2. Delete the 70 from the node 3. If the node still has another data item, done 4. If the node does not have a data item, delete node 5. 3-node with 2 values must have 3 children not 2, so move smaller value from parent to left child 2-3 Deletion https://people.ok.ubc.ca/ylucet/ds/algorithms. html uses inorder predecessor 5
Delete 100 5. 6. All leaves not at same level, so a. Check the siblings of the now-empty leaf and distribute values is sibling has 2 b. If no sibling has two items, merge the leaf with an adjacent sibling by moving an item down from the parent into the sibling and deleting the empty leaf. c. If parent became empty, apply merging (6) step recursively 2-3 Deletion 6
Delete 80 5. 6. All leaves not at same level, so a. Check the siblings of the now-empty leaf and distribute values is sibling has 2 b. If no sibling has two items, move an item down from the parent into the sibling and deleting the empty leaf. c. If parent became empty, apply merging (6) step recursively 2-3 Deletion 7
Deleting 70 8
Best-First Search Generalizing breadth-first search to find a specific vertex Instead of queue us priorityqueue Cost function f(n) is the priority UPDATED Add initial vertex to PQ OPEN while PQ is not empty w = top of queue if w is targetvertex, return add w to CLOSED list for each vertex u adjacent to w if u is not in CLOSED calculate f(u), push u to PQ OPEN with cost f(u) A node is OPEN, if we will still look at paths from that node A node is CLOSED, when it has been fully explored f(n) == depth(n) or all edges are of length 1, then we have breadth-first search EXplore nodes at distance 1, and then distance 2, and then distance 3,. if f(n) is sum of edge costs from start to n, we have Dijkstra CLOSED is equivalent to vertexset OPEN keeps track of shortest paths so far 9
2-3-4 Tree 10
2-3-4 Tree 4-nodes, nodes with 3 data items and 4 children: left, middleleft, middleright, right Up to 3 data items: S, M and L Insertion moves extra item to parent BUT to avoid the return path it preemptively splits 4-nodes (with 3 values) on the way down middle value gets pushed up to parent Preemptive split 11
2-3 vs 2-3-4 tree Both are balanced Reduction in height leads to larger number of comparisons, so not much gained 2-3-4 tree has single pass insertion and removal, more efficient than 2-3 tree How about 2-3-4-5 -20 tree? Too many comparisons BUT worth it if implemented for external storage where moving from node-to-node is more expensive than comparisons 2-3-4 tree good for dictionaries, databases, file systems 12
AVL (Adelson-Velskii and Landis) AVL Trees Binary search tree Balanced - height close to the minimum Rotations restore the balance Common to keep track of the height of each node 13
Left Rotate: (Right-Right, right child s right child is heavy) Right Rotate: (left-left, left child s left child is heavy) 14
Insert: 3, 2, 1, 4, 5, 6, 7 into a fresh AVL tree. Show each step of the process, e.g., for each insertion: heights, rotation node, and tree before and after each rotation https://people.ok.ubc.ca/ylucet/ds/avltree.html 15
Insert: 3, 2, 1, 4, 5, 6, 7 into a fresh AVL tree. Insert 5, unbalances 3, left-rotate on 4. Insert 6, unbalances 2, left-rotate on 4 Insert 7, unbalances 5, left-rotate on 6 16
Red-Black Trees 2-3-4 trees require more storage than a BST Convert 3-nodes and 4-nodes to binary nodes All the child pointers in the original 2-3-4 tree be black, and use red child pointers to link the 2-nodes that result when you split 3-nodes and 4-nodes. 17
Red-Black Trees Traversal: Like BST, ignore link color Root is black Every red node has a black parent Any children of red node are black (red node cannot have red children) Every path from root to leaf contains same number of black nodes Carrano show 2-3-4 insertion equivalence to red-black tree, but might be easier to think about it separetely 18
Red-Black Trees - Insertion Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent 1. Color the grandparent red 2. Color the parent black uncle sibling of parent node. 3. Color the uncle black Every node is red on insertion Empty child is considered black 4. Point X to grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children 1. Color the grandparent red 2. Color parent black 3. Rotate 3a. If both left children, rotate right on parent 3b. If both right children, rotate left on parent 4. Point X to parent Case 3: Parent is red, uncle is black. X and its parent are opposite type of children 1. Need double rotation. 2. Rotate 2a. If parent is left, child is right, rotate left on X 3. 2b. If parent is right, child is left, rotate right on X 4. Set X to new child (which used to be parent) and proceed to Case 2 19
Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 10, Case-0, root is black Add 20, parent is black Add 30, Case-2, parent (20) is red, uncle (empty) is black, Color the grandparent (10) red. Color parent (20) black. Both right children, rotate-left on parent (20). Point X to parent (20) Case-0, X (20) is root, make it black 20
Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 40, Case-1 parent (30) and uncle (10) are red, push blackness down. Color the parent (30) black, Color the uncle (10) black, point X to grandparent (20) Case-0, X (20) is root, make it black 21
Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. uncle sibling of parent node. Every node is red on insertion Empty child is considered black Add 3, Case-2 parent (5) is red, uncle (empty) is black. Color the grandparent (10) red. Color parent (5) black. Both left children, rotate-right on parent (5). Point X to parent (5), nothing to be done 22
Case 0: X is the root. Make X black Case 1: Both parent and uncle are red - push blackness down from grandparent Case 2: Parent is red, uncle is black. X and its parent are both left or both right children Case 3: Parent is red, uncle is black. Add 12, Case-3 parent (15) is red, uncle (empty) is black. X and its parent are opposite type of children. Need double rotation. Parent (15) is right, X (12) is left child, so rotate right on X (11). Set X to new child (15). Go to Case-2 Parent (12) is red, uncle (empty) is black. Color the grandparent (10) red. Color parent (12) black. Both right children, rotate-left on parent (12). Point X to parent (10) 23
Review 2-3 Trees 2-3-4 Trees AVL Trees Red-Black Trees (equivalent to 2-3-4) 24