# CSE Lecture 17 – Balanced trees

## Presentation on theme: "CSE Lecture 17 – Balanced trees"— Presentation transcript:

CSE 30331 Lecture 17 – Balanced trees
Unbalanced binary search trees Balanced search trees 2-3-4 trees Red-black trees

Unbalanced Binary Search Tree and balanced alternatives

Two Binary Search Tree Examples
Insertion sequence for tree (a): 5, 15, 20, 3, 9, 7, 12, 17, 6, 75, 100, 18, 25, 35, 40 Depth = 6 Depth = 4 Average comparisons per search = 4.0 Average comparisons per search = 3.47 5 9 3 15 5 20 9 20 3 7 15 75 7 12 17 75 6 12 17 35 100 6 18 25 100 18 25 40 (a) unbalanced 35 (b) Same data, but more balanced 40

What is a 2-3-4 Tree? Each node has 1, 2 or 3 values
Each node has 2, 3 or 4 children The tree is ALWAYS perfectly balanced Depth of all subtrees of a node are equal

2-3-4 Tree Example

2-3-4 Tree (node types) A 2-node (value < A) (value > A) A B C
(A < value < B) (value > C) (B < value < C) A B 3-node A < B (value < A) (A < value < B) (value > B)

2-3-4 Tree Insertion Splits 4-nodes top-down
Inserting 7 requires splitting of the 4-node before adding 7 as a child (leaf or part of existing leaf) After splitting, 7 is added to a leaf following normal search tree order 9 3 6 2 10 7 8 4 2 4 10 8

2-3-4 Tree Insertion Details
4-nodes must be split on the way down Split involves “promotion” of middle value into parent node New value is always added to existing 2-node or 3-node LEAF 2-3-4 tree is always perfectly balanced Implementation is wasteful of space Each node must have space for 3 values, 1 parent pointer and 4 child pointers, even if not always used There is always an equivalent red-black binary tree

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 4, first split the root (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 Insert 8 To insert 10, first split the root (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 8 10 Insert 25 Insert 35 2 8 10 15 35 Insert 55 Split 4-node (15, 25, 35) To insert 55, first split the leaf (a 4-node) and then add value to leaf

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 11, again requires top-down split of each 4-node encountered, so root is split and new value is inserted in leaf 2 15 25 4 12 Insert 11 2 15 25 4 12 Split 4-node (4, 12, 25)

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 To insert 9, split the leaf (a 4-node) and insert new value 2 15 25 12 11 Insert 9 2 15 25 12 11 8 Split 4-node (8, 10, 11)

Building 2-3-4 Tree by Series of Insertions
Insertion Sequence: 2, 15, 12, 4, 8, 10, 25, 35, 55, 11, 9, 5, 7 2 15 25 12 11 Insert 5 2 15 25 12 11 9 2 15 25 12 11 9 5 Split 4-node (5, 8, 9) And finally insert 7 in a leaf

Inefficiencies 2-3-4 trees require space for up to ...
3 values in every node Pointers to 4 children from every node Even if they are not used in all cases So, we can use a Red Black tree instead A (colored) form of binary search tree Always relatively balanced Nodes have no more storage required than normal binary search tree

Red Black Tree Properties
The root is always BLACK A RED parent never has a RED child Every path from ROOT to an EMPTY subtree has the same black height (# of BLACK nodes) Black Height of tree is O(log2n)

Red Black Tree Node Types
BLACK parent with two RED children is the equivalent of a 4-node A 4-node split is accomplished by flipping the colors to RED parent & two BLACK children, with possible rotation if colors conflict BLACK parent with single RED child is equivalent of a 3-node New nodes are always inserted a RED leaves

Red-Black subtree equivalents of 2-3-4 tree nodes

Converting a 2-3-4 Tree to Red-Black Tree (top-down)
Original First, convert the root (note: shaded nodes are RED)

Converting a 2-3-4 Tree to Red-Black Tree (top-down)
Next, convert left subtree Then convert the right subtree and we’re done

Building red black tree by repeated insertions
Insert root and color it BLACK Each successive insertion point is found in normal search tree order Any 4-nodes (black parent with two red children) is split going down into tree Requires color changes and possible rotations of subtrees New value is added as RED leaf If result is RED child of RED parent, then rotations are required to correct

Situations in the Splitting of a 4-Node (A X B)

Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

Left child of a Black parent P: Split requires only a Color Flip

Example split prior to inserting 55: flip colors (40,50,60) & insert leaf

Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

RED parent, 4-node oriented left-left from G (grandparent)

Required actions to split a 4-node (A X B) with parent P & grandparent G
4-node is either child of black parent Flip subtree colors ONLY (X red, A & B black) 4-node is child of red parent (left-left) Flip subtree colors, single right rotation about P & recolor 4-node is child of red parent (right-right) Flip subtree colors, single left rotation about P & recolor 4-node is child of red parent (left-right) Flip subtree colors, then double rotation (first left, then right) about X & then color X black, G & P red 4-node is child of red parent (right-left) Flip subtree colors, then double rotation (first right, then left) about X & then color X black, G & P red

Red Parent : Oriented Left-Right From G

Red Parent : Oriented Left-Right From G
B A X G P C D Red-black tree after color flip B A X G P C D Red-black tree after first (left) rotation (about X)

Red Parent : Oriented Left-Right From G
B A X G P C D Red-black tree after first (left) rotation (about X) After second (right) rotation about X and coloring X black, G & P red B A G X P C D

Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

Building A Red-Black Tree Insertions: 2, 15, 12, 4, 8, 10, 25, 35

Example on blackboard Insert: 2, 12, 15, 7, 5, 8, 10

Deleting nodes from a Red-Black Tree
If node has 2 children Swap value with that of inorder successor and Delete the successor node (it has at most 1 child) If node has 1 or 0 children If it is RED Delete it and replace with its BLACK child If it is BLACK and its child is RED Delete it, replace with its RED child and recolor it BLACK If it is BLACK and its child is BLACK Yikes!!!!

Deleting nodes from a Red-Black Tree
If node has 1 or 0 children If it is BLACK and its child is BLACK Delete the node and replace with its child BUT ... Now we have a situation. We have removed a BLACK node from some paths and thus the BLACK height may differ in this part of the tree from others If the node deleted was the root, we are done All paths are shorter by 1 black node If not the root, then there are 5 other cases These involve bottom-up rebalancing of the tree using recoloring and various rotations See

Representing a Red-Black Tree
The rbNode class from Ford & Topp

Summary 2-3-4 tree Each node has either …
1 value and 2 children 2 values and 3 children 3 values and 4 children Construction of trees is complex, so we build an equivalent binary tree known as a red-black tree