lecture06-RBTree

lecture06-RBTree - CS 6713 Advanced Analysis of Algorithms...

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

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

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

View Full Document
Review: Binary Search Trees BST property: key[left(x)] key[x] key[right(x)] More precisely, [all the keys in x’s left subtree] key[x] [all the keys in x’s right subtree] Example: F B H K D A

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

View Full Document
Review: Inorder Tree Walk An inorder walk prints the set in sorted order: TreeWalk(x) TreeWalk(left[x]); print(x); TreeWalk(right[x]); Easy to show by induction on the BST property Preorder tree walk : print root, then left, then right Postorder tree walk : print left, then right, then root
Review: BST Search TreeSearch(x, k) if (x = NULL or k = key[x]) return x; if (k < key[x]) return TreeSearch(left[x], k); else return TreeSearch(right[x], k);

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

View Full Document
Review: BST Search (Iterative) IterativeTreeSearch(x, k) while (x != NULL and k != key[x]) if (k < key[x]) x = left[x]; else x = right[x]; return x;
Review: BST Insert Adds an element x to the tree so that the binary search tree property continues to hold The basic algorithm Like the search procedure above Insert x in place of NULL Use a “trailing pointer” to keep track of where you came from (like inserting into singly linked list) Like search, takes time O( h ), h = tree height

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

View Full Document
Red-Black Tree
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

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
Height of Red-Black Trees What is the minimum black-height of a node with height h? A: a height- h node has black-height h /2 Theorem: A red-black tree with n internal nodes has height h 2 lg( n + 1) How to prove this? 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

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

View Full Document
RB Trees: Proving Height Bound Prove: n -node RB tree has height h 2 lg( n +1) Claim: A subtree rooted at a node x contains at least 2 bh( x ) - 1 internal nodes Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)?
RB Trees: Proving Height Bound Prove: n -node RB tree has height h 2 lg( n +1) Claim: A subtree rooted at a node x contains at least 2 bh( x ) - 1 internal nodes Proof by induction on height h Base step: x has height 0 (i.e., NULL leaf node) What is bh(x)?

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 03/30/2011 for the course CS 6713 taught by Professor Su during the Spring '11 term at Arkansas State.

Page1 / 46

lecture06-RBTree - CS 6713 Advanced Analysis of Algorithms...

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

View Full Document
Ask a homework question - tutors are online