lecture06-RBTree

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

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

View Full Document Right Arrow Icon
Sources: Dr. David Luebke’s slides CS 6713 Advanced Analysis of Algorithms Lecture 06 Red-Black Trees (cont’d)
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 4
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);
Background image of page 5

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

View Full DocumentRight Arrow Icon
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;
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
Red-Black Tree
Background image of page 8
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
Background image of page 9

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

View Full DocumentRight Arrow Icon
Red-Black Trees black-height : # black nodes on path to leaf Label example with h and bh values
Background image of page 10
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
Background image of page 11

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

View Full DocumentRight Arrow Icon
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)?
Background image of page 12
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)?
Background image of page 13

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

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

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 Right Arrow Icon
Ask a homework question - tutors are online