lecture_09

# lecture_09 - Introduction to Algorithms 6.046J/18.401J...

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

Introduction to Algorithms 6.046J/18.401J Lecture 9 Prof. Piotr Indyk

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

View Full Document
Today or how to avoid this Balanced search trees, even in the worst case 1 2 3 4 5 6 © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.2
Balanced search trees Balanced search tree: A search-tree data structure for which a height of O (lg n ) is guaranteed when implementing a dynamic set of n items. AVL trees Examples: 2-3 trees 2-3-4 trees B-trees Red-black trees © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.3

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

View Full Document
Red-black trees BSTs with an extra one-bit color field in each node. Red-black properties: 1. Every node is either red or black. 2. The root and leaves ( NIL ’s) are black. 3. If a node is red, then its parent is black. 4. All simple paths from any node x to a descendant leaf have the same number of black nodes. © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.4
Example of a red-black tree 8 8 11 11 10 10 18 18 26 26 22 22 3 3 7 7 NIL NIL NIL NIL NIL NIL NIL NIL NIL © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.5

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

View Full Document
Use of red-black trees What properties would we like to prove about red-black trees ? – They always have O(log n) height –There is an O(log n) –time insertion procedure which preserves the red-black properties Is it true that, after we add a new element to a tree (as in the previous lecture), we can always recolor the tree to keep it red-black ? © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.6
Example of a red-black tree 8 8 11 11 10 10 18 18 26 26 22 22 3 3 7 7 7.5 7.5 © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.7

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

View Full Document
Use of red-black trees What properties would we like to prove about red- black trees ? –They always have O(log n) height – There is an O(log n) –time insertion procedure which preserves the red-black properties Is it true that, after we add a new element to a tree (as in the previous lecture), we can always recolor the tree to keep it red-black ? NO After insertions, sometimes we need to juggle nodes around © Piotr Indyk and Charles E. Leiserson Introduction to Algorithms October 13, 2004 L9.8
Rotations A A B B α α β β γ γ R IGHT -R OTATE (B) B B A A γ γ β β α α L EFT -R OTATE (A) Rotations maintain the inorder ordering of keys: a ∈α , b ∈β , c ∈γ a A b B c .

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 note was uploaded on 04/10/2008 for the course CSE 6.046J/18. taught by Professor Piotrindykandcharlese.leiserson during the Fall '04 term at MIT.

### Page1 / 40

lecture_09 - Introduction to Algorithms 6.046J/18.401J...

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

View Full Document
Ask a homework question - tutors are online