11 Red Black Trees

11 Red Black Trees - Search Trees, part 2 15-211...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Search Trees, part 2 15-211 Fundamental Data Structures and Algorithms Margaret Reid-Miler 18 February 2010 Reading for today: Sections 19.5 and 19.8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 Plan Today 2-3-4 trees Red-black trees Index trees HW3 is out You should do the theory questions first As seam carving is slow, give yourself plenty of time for testing
Background image of page 2
3 Last time … Dealing with imbalance Balanced trees Maintain structural invariants that guarantee O(lg n ) height AVL trees today 2-3-4 and Red-black trees Thursday Self-adjusting trees Not necessarily O(lg n ) height, but can show amortized efficiency
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
4 Last time … A bad structural invariant Complete trees Consider insert(7): 42 25 12 9 17 37 29 46 50 71 47 27 44 37 17 9 7 12 29 27 44 47 71 46 25 42 50
Background image of page 4
5 Last time …. AVL trees For a tree T , let L be the root’s left subtree and R be the root’s right subtree. T is an AVL tree if |height( L ) – height( R )| · 1 L is an AVL tree, and R is an AVL tree a.k.a. height-balanced trees L R
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
6 AVL insert At lowest node where imbalance occurred, which subtree caused the imbalance? i.e., to which subtree did the insert occur? Four cases: Left outer Left inner Right inner Right outer
Background image of page 6
7 Case 1: left outer subtree The insertion was to the left subtree of the left child of X: “single rotate right” X y A B C X y A B C Δ ( h) > 1 (to be continued…)
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
8 Case 1: left outer subtree The insertion was to the left subtree of the left child of X: “single rotate right” X y A B C X y A B C 2
Background image of page 8
9 Case 2: left inner subtree The insertion was to the right subtree of the left child of X: “double rotate right” X y A C D Z B X y A C D Z B (to be continued…) 2
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
10 Case 2: left inner subtree The insertion was to the right subtree of the left child of X: “double rotate right” X y A C D Z B X y A C D Z B 2
Background image of page 10
11 Case 3: right inner subtree The insertion was to the left subtree of the right child of X: “double rotate left” X y A C D Z B X y A C D Z B symmetric with Case 2!
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
12 Case 4: right outer subtree The insertion was to the right subtree of the right child of X: “single rotate left” X y A B C 2 X y A B C symmetric with Case 1!
Background image of page 12
AVL delete Is similar, except one subtree is too short instead of too long insert never requires more than one rebalancing operation delete might require O(lg n) rebalances as we walk up the tree. As we correct an imbalance we may reduce
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 55

11 Red Black Trees - Search Trees, part 2 15-211...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online