redblack 12 - RED BLACK TREES A RED-BLACK TREE IS ANOTHER...

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

View Full Document Right Arrow Icon
RED BLACK TREES
Background image of page 1

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

View Full DocumentRight Arrow Icon
A RED-BLACK TREE IS ANOTHER KIND OF BALANCED BINARY SEARCH TREE.
Background image of page 2
A RED-BLACK TREE IS A BINARY SEARCH TREE IN WHICH THE ROOT ITEM IS COLORED BLACK, EVERY OTHER ITEM IS COLORED RED OR BLACK, AND 1. (RED RULE) A RED ITEM CANNOT HAVE ANY RED CHILDREN; 2. (PATH RULE) THE NUMBER OF BLACK ITEMS IS THE SAME IN ANY PATH FROM THE ROOT ITEM TO AN ITEM WITH NO CHILDREN OR WITH ONE CHILD .
Background image of page 3

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

View Full DocumentRight Arrow Icon
THE FOLLOWING ARE RED-BLACK TREES:
Background image of page 4
60 30 80 20 50 90
Background image of page 5

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

View Full DocumentRight Arrow Icon
60 30 80 20 50 90 40
Background image of page 6
WHAT ABOUT THIS? 60 30 80 20 50 90 40
Background image of page 7

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

View Full DocumentRight Arrow Icon
60 30 80 20 50 90 40 THIS IS NOT A RED-BLACK TREE: THE PATH FROM 60 TO 80 HAS ONLY ONE BLACK ELEMENT!
Background image of page 8
60 30 80 20 50 70 90 ADD 40?
Background image of page 9

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

View Full DocumentRight Arrow Icon
60 30 80 20 50 70 90 40 ADD 35?
Background image of page 10
60 30 80 20 50 70 90 THIS TREE VIOLATES THE PATH RULE. 40 35
Background image of page 11

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

View Full DocumentRight Arrow Icon
60 30 80 20 50 70 90 THIS TREE VIOLATES THE RED RULE. 40 35
Background image of page 12
ROTATION TO THE RESCUE!
Background image of page 13

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

View Full DocumentRight Arrow Icon
60 30 80 20 40 70 90 35 50
Background image of page 14
50 30 90 20 40 80 131 60 85 100 150 140 160 180 THIS RED-BLACK TREE IS NOT AN AVL TREE. WHY?
Background image of page 15

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

View Full DocumentRight Arrow Icon
CLAIM: THE HEIGHT OF ANY RED- BLACK TREE IS LOGARITHMIC IN n .
Background image of page 16
WHAT IS THE MINIMUM HEIGHT OF A RED-BLACK TREE? 75 40 90 20 60 80 100 10 30 50
Background image of page 17

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

View Full DocumentRight Arrow Icon
SUPPOSE A RED-BLACK TREE IS COMPLETE, WITH ALL BLACK ELEMENTS, EXCEPT FOR RED LEAVES AT THE LOWEST LEVEL. THEN THE HEIGHT OF THAT TREE IS, APPROXIMATELY, log 2 n .
Background image of page 18
WHAT IS THE MAXIMUM HEIGHT? 50 30 90 20 40 80 131 60 85 100 150 140 160 185
Background image of page 19

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

View Full DocumentRight Arrow Icon
SUPPOSE A RED-BLACK TREE HAS ALL BLACK ELEMENTS, EXCEPT THAT ONE PATH FROM THE ROOT TO A LEAF HAS AS MANY RED ELEMENTS AS POSSIBLE. THEN THE LENGTH OF THAT PATH, AND THE HEIGHT OF THE TREE, WILL BE MAXIMAL. THE LENGTH OF THAT PATH IS APPROXIMATELY, TWICE THE MINIMAL HEIGHT, SO THE MAXIMUM HEIGHT OF A RED-BLACK TREE WILL BE, ROUGHLY, 2log 2 ( n ).
Background image of page 20
AN ITEM’S KEY IS THAT PART OF THE ITEM USED IN COMPARISONS WITH OTHER ITEMS.
Background image of page 21

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

View Full DocumentRight Arrow Icon
THERE ARE FOUR ASSOCIATIVE CONTAINER CLASSES IN THE STANDARD TEMPLATE LIBRARY: set : ITEM = KEY; NO DUPLICATES ALLOWED multiset : ITEM = KEY; DUPLICATES ALLOWED map : KEY IS PART OF ITEM; NO DUPLICATES ALLOWED multimap : KEY IS PART OF ITEM;DUPLICATES ALLOWED
Background image of page 22
IN THE HEWLETT-PACKARD IMPLEMENTATION, AND OTHERS, ALL FOUR CLASSES ARE BASED ON RED- BLACK TREES. WE NOW OUTLINE HEWLETT- PACKARD’S rb_tree CLASS:
Background image of page 23

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

View Full DocumentRight Arrow Icon
Red Black Trees Data Structures and the STL - Collins 24 template < class Key, class Value, class KeyOfValue, class Compare> class rb_tree { Key : THE TYPE OF THE KEY: THE PART OF AN ITEM (MAY BE THE WHOLE ITEM) USED IN COMPARISONS WITH OTHER ITEMS Value : THE TYPE OF EACH ITEM KeyOfValue : A FUNCTION-CLASS TYPE: RETURNS THE KEY FROM THE VALUE Compare : A FUNCTION-CLASS TYPE FOR COMPARING KEYS
Background image of page 24
Red Black Trees Data Structures and the STL - Collins 25 FOR EXAMPLE, THE set CLASS STARTS AS FOLLOWS: template < class Key, class Compare = less<Key> > class set { typedef rb_tree<Key, Key, ident<Key, Key>, Compare> rep_type; rep_type t; // red-black tree representing set IN THE ident FUNCTION CLASS, operator ( ) HAS ONE PARAMETER AND RETURNS THAT PARAMETER!
Background image of page 25

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

View Full DocumentRight Arrow Icon
Data Structures and the STL - Collins 26 THE rb_tree
Background image of page 26
Image of page 27
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/17/2010 for the course CS 162 taught by Professor Fant during the Spring '08 term at Portland State.

Page1 / 87

redblack 12 - RED BLACK TREES A RED-BLACK TREE IS ANOTHER...

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

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