Red Black Trees Two - 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, Binary Search Trees, Important, Data Structure, Dynamic Sets, Addition to Satellite Data, Eleements, Identifying, Null For Root, Property

Typology: Slides

2012/2013

Uploaded on 03/23/2013

dhruv
dhruv 🇮🇳

4.3

(12)

194 documents

1 / 32

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
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20

Partial preview of the text

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

Algorithms

Red-Black Trees

Review: Red-Black Trees

● Red-black trees :

■ Binary search trees augmented with node color

■ Operations designed to guarantee that the height

h = O(lg n )

● We described the properties of red-black trees

● We proved that these guarantee h = O(lg n )

● Next: describe operations on red-black trees

Review: Black-Height

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

● What is the minimum black-height of a node

with height h?

● A: a height- h node has black-height ≥ h /

● Theorem: A red-black tree with n internal

nodes has height h ≤ 2 lg( n + 1)

■ Proved by (what else?) induction

Review: Proving Height Bound

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

n ≥ 2 bh( root )^ - 1

n ≥ 2 h /2^ - 1

lg( n+1 ) ≥ h /

h ≤ 2 lg( n + 1)

Thus h = O(lg n )

Red-Black Trees: An Example

● Color this tree: 7

5 9

1212

Red-black 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

● Insert 8

■ Where does it go?

Red-Black Trees:

The Problem With Insertion

  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

● Insert 8

■ Where does it go?

■ What color

should it be?

Red-Black Trees:

The Problem With Insertion

  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

Red-Black Trees:

The Problem With Insertion

● Insert 11

■ Where does it go?

  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

Red-Black Trees:

The Problem With Insertion

● Insert 11

■ Where does it go?

■ What color?

○ Can’t be red! (#3)

  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

Red-Black Trees:

The Problem With Insertion

● Insert 11

■ Where does it go?

■ What color?

○ Can’t be red! (#3) ○ Can’t be black! (#4)

  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

Red-Black Trees:

The Problem With Insertion

● Insert 10

■ Where does it go?

  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

Red-Black Trees:

The Problem With Insertion

● Insert 10

■ Where does it go?

■ What color?

  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

RB Trees: Rotation

● Our basic operation for changing tree structure

is called rotation :

● Does rotation preserve inorder key ordering?

● What would the code for rightRotate()

actually do?

y

x C

A B

x

A y

B C

rightRotate(y)

leftRotate(x)

rightRotate(y)

RB Trees: Rotation

● Answer: A lot of pointer manipulation

■ x keeps its left child

■ y keeps its right child

■ x ’s right child becomes y ’s left child

■ x ’s and y ’s parents change

● What is the running time?

y x C A B

x A y B C