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!
13 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.

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.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern