lecture07-RBTree

# Lecture07-RBTree - CS 6713 Advanced Analysis of Algorithms Lecture 07 Red-Black Trees(contd Sources Dr David Luebkes slides Internet 1 2 Red-Black

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

Sources: Dr. David Luebke’s slides Internet CS 6713 Advanced Analysis of Algorithms Lecture 07 Red-Black Trees (cont’d) 1

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

View Full Document
2
Red-Black Properties The red-black properties : 1. Every node is either red or black 2. Every leaf (NULL pointer) is black Note: this means every “real” node has 2 children 3. If a node is red, both children are black Note: can’t have 2 consecutive reds on a path 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black 3

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

View Full Document
Red-Black Trees black-height : # black nodes on path to leaf Label example with h and bh values 4
RB Trees: Worst-Case Time So we’ve proved that a red-black tree has O(lg n ) height Corollary: These operations take O(lg n ) time: Minimum(), Maximum() Successor(), Predecessor() Search() Insert() and Delete(): Will also take O(lg n ) time But will need special care since they modify tree 5

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

View Full Document
Red-Black Trees: Insertion Insertion: the basic idea Insert x into tree, color x red Only r-b property 3 might be violated (if p[ x ] red) If so, move violation up tree until a place is found where it can be fixed Total time will be O(lg n ) 1. Every node is either red or black 2. Every leaf (NULL pointer) is black 3. If a node is red, both children are black 4. Every path from node to descendent leaf contains the same number of black nodes 5. The root is always black 6
RB Insert: Case 1 if (y->color == RED) x->p->color = BLACK; y->color = BLACK; x->p->p->color = RED; x = x->p->p; Case 1: “uncle” is red In figures below, all ’s are equal-black-height subtrees C A D B C A D B x y new x Change colors of some nodes, preserving #4: all downward paths have equal b.h. The while loop now continues with x’s grandparent as the new x case 1 7

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

View Full Document
x RB Insert: Case 1 if (y->color == RED) x->p->color = BLACK; y->color = BLACK; x->p->p->color = RED; x = x->p->p; Case 1: “uncle” is red In figures below, all ’s are equal-black-height subtrees C A D C A D y new x Same action whether x is a left or a right child B x case 1 8
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/30/2011 for the course CS 6713 taught by Professor Su during the Spring '11 term at Arkansas State.

### Page1 / 39

Lecture07-RBTree - CS 6713 Advanced Analysis of Algorithms Lecture 07 Red-Black Trees(contd Sources Dr David Luebkes slides Internet 1 2 Red-Black

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

View Full Document
Ask a homework question - tutors are online