Download presentation
Presentation is loading. Please wait.
1
Computer Science 2-3-4 Red-Black CS 330: Algorithms 2-3-4 and Red-Black Trees Gene Itkis
2
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis2 2-3-4 trees 2-3-4 2-3-4 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
3
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 2-3-4 node Black2-3-4 Black depth = depth in 2-3-4 tree
4
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis4 2-3-4Red-Black 2-3-4 & Red-Black trees 2-3-4 2-3-4 trees Perfectly balanced height ≤ lg n Red-Black Red-Black trees 2-3-4 2-3-4 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 2-3-4 & Red-Black): O(lg n)
5
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis5 2-3-4 trees 2-3-4 2-3-4 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
6
Computer Science CS-330: Algorithms, Fall 2004Gene Itkis6 2-3-4 2-3-4 tree example52 7 8 6 9 5 225225 7 2 5 7 7 2 5 878878 2 5 6 6 7 8 2 5 2 5 8 6 7 5757 2 8 6 9
7
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 5 6+66+6 5 6 55 6 4 4 4 4 6 A CDB A CDB A 5 4 6+66+6 4 5 3 4 6 3 3 4 5 3 6 5 B A CB A C 6 DEDE
8
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 5 6+66+6 5 6 5 5 6 4 4 4 4 6 5 4 4 5 6+66+6 5 6 4 5 6 4 5 4 5 6 4 4 6 5+55+5 5 6 4 5 6 4 6 6
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.