Computer Science Red-Black CS 330: Algorithms and Red-Black Trees Gene Itkis
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis trees nodes: Red-black Red-black implementation: x y z >y<z>y<z >z>z >x<y>x<y <x<x >y>y >x<y>x<y <x<x xyxy >x>x<x<x x >x>x<x<x x >y>y >x<y>x<y <x<x x y y x or >y<z>y<z >z>z >x<y>x<y <x<x y z x
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis3 Red-Black trees Properties: Every node is either red or black Root: black Leaf (nil): black Children of red node are black Any root-leaf path has same # of black nodes Black depth of node v = “# of black nodes on the path from root to v same for all leaves Black2-3-4 Black node = “root” of a node Black2-3-4 Black depth = depth in tree
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis Red-Black & Red-Black trees trees Perfectly balanced height ≤ lg n Red-Black Red-Black trees node = Red-Black =Red-Black subtree of height ≤ 2subtree Red-Blackheight ≤ 2 lg n Red-Black tree height ≤ 2 lg n Search (both 2-3-4Red-Black: O(lg n) Search (both & Red-Black): O(lg n)
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis trees nodes: Red-black Red-black implementation: x y z >y<z>y<z >z>z >x<y>x<y <x<x >y>y >x<y>x<y <x<x xyxy >x>x<x<x x >x>x<x<x x >y>y >x<y>x<y <x<x x y y x or >y<z>y<z >z>z >x<y>x<y <x<x y z x
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis tree example
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis7 B Zoom-in: one node Simple inserts: Case 0 No fixing needed …even inside tree More complex: Case 1: Split (3,4,5) 4 will try to join higher 6 can now join as case 0 …same inside tree A CDB A CDB A B A CB A C 6 DEDE
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis8 Zoom-in: one node Case 0: Case 3: Rotate 4-5… To get case 0 above Case 2: Rotate 6-5 To get case 3 above