CO4301 – Advanced Games Development Week 5 Walkthrough of Red-Black Tree Insertion Gareth Bellaby
Insert 40 Root, black 40
Insert 20 Parent is black No change 40 20
Insert 45 Parent is black No change 40 20 45
Insert 50 Parent is red Uncle is red 40 20 45 50
Colour uncle and parent black Colour node and grandparent red Propagate change in rest of tree 40 20 45 50
Insert 43 Parent black No change 40 20 45 43 50
Insert 41 Parent red Uncle red 40 20 45 43 50 41
Colour uncle and parent black Colour node and grandparent red Propagate change in rest of tree (no further changes) 40 20 45 43 50 41
Insert 42 Parent red Uncle black N is a right child Left rotation on P 40 20 45 G 43 50 U 41 P N is a right child Left rotation on P N 42
Effect of left rotation on P 40 20 45 43 50 42 Effect of left rotation on P 41
Relabel N and P Uncle is black N is left child Right rotation on G G U 40 20 45 G 43 50 U P 42 N is left child Right rotation on G 41 N
Effect of right rotation on G 40 20 45 42 50 P 41 N 43 G Effect of right rotation on G U
40 20 45 42 50 P 41 43 G Swap colours of P and G
The tree is still height balanced. 40 20 45 42 50 Examine the tree The tree is still height balanced. The number of black nodes is identical for every path to a leaf. 41 43 The values are sort order