Red Black Trees - Introduction to Algorithms - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Introduction to Algorithms which includes Expensive Operations, Sort Edges, Running Time, Upshot, Union, Makeset, Disjoint Set, Disjoint Set Union, Naïve Implementation etc. Key important points are: Red Black Trees Two, Binary Search Trees, Augmented, Operations Designed, Height, Color, Red Black Trees, Properties, Guarantee, Every Leaf

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Algorithms
Red-Black Trees
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Red Black Trees - Introduction to Algorithms - Lecture Slides and more Slides Computer Science in PDF only on Docsity!

Algorithms

Red-Black Trees

Review: Binary Search Trees

● Binary Search Trees (BSTs) are an important

data structure for dynamic sets

● In addition to satellite data, eleements have:

key : an identifying field inducing a total ordering ■ left : pointer to a left child (may be NULL) ■ right : pointer to a right child (may be NULL) ■ p : pointer to a parent node (NULL for root)

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);

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

Review: Sorting With BSTs

● Basic algorithm:

■ Insert elements of unsorted array from 1.. n ■ Do an inorder tree walk to print in sorted order

● Running time:

■ Best case: Ω( n lg n ) (it’s a comparison sort) ■ Worst case: O(n 2 ) ■ Average case: O( n lg n ) (it’s a quicksort!)

Review: More BST Operations

● Minimum:

■ Find leftmost node in tree

● Successor:

■ x has a right subtree: successor is minimum node in right subtree ■ x has no right subtree: successor is first ancestor of x whose left child is also ancestor of x ○ Intuition: As long as you move to the left up the tree, you’re visiting smaller nodes.

● Predecessor: similar to successor

Review: More BST Operations

● Delete:

■ x has no children: ○ Remove x ■ x has one child: ○ Splice out x ■ x has two children: ○ Swap x with successor ○ Perform case 1 or 2 to delete it

F B H

A D K

C Example: delete K or H or B

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

Red-Black Trees

● Put example on board and verify properties:

  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

● black-height: # black nodes on path to leaf

■ Label example with h and bh values

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 2bh( 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 2bh( 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)? ○ A: 0 ○ So…subtree contains 2 bh( x )^ - 1 = 2^0 - 1 = 0 internal nodes (TRUE)

RB Trees: Proving Height Bound

● Thus at the root of the red-black tree:

n ≥ 2 bh( root )^ - 1 (Why?) n ≥ 2 h /2^ - 1 (Why?) lg( n+1 ) ≥ h /2 (Why?) h ≤ 2 lg( n + 1) (Why?)

Thus h = O(lg n )

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