11 Red Black Trees

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

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

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

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

View Full Document
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
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

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

View Full Document
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
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

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

View Full Document
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
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…)

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

View Full Document
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
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

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

View Full Document
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
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!

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

View Full Document
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!
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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/10/2011.

Page1 / 55

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

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

View Full Document
Ask a homework question - tutors are online