Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Red-Black Tree, Exercises of Data Representation and Algorithm Design

Section: Red Black Trees handout. Red-Black Tree. Binary search tree. •properties every node is red or black every leaf is a NULL node and black.

Typology: Exercises

2021/2022

Uploaded on 09/27/2022

attourney
attourney 🇬🇧

3.8

(11)

228 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CPS 100E - Program Design and Analysis I
Prof. Rodger
Section: Red BlackTrees (handout)
Red-BlackTree
Binary search tree
properties
{
every node is red or black
{
every leaf is a NULL node and black
{
the root is black
{
If node is red, then its children are black.
{
Every simple path from a node to a descendant leaf contains the same number of black nodes.
The gure below shows a red-black tree. Black nodes are the shaded nodes, and red nodes are the
nonshaded nodes. The rectangular nodes are external nodes (leaves) that only contain the value NULL.
Def:
The black height (bh) of a node
x
is the number of black nodes on any path from
x
(not including
x
)
to a leaf.
Example:
bh(19)=
bh(8)=
22
19
16
14
11
8
6
4
null
null null null null null
null null
null
Lemma:
A red-black tree with
n
internal nodes has height at most 2 log (
n
+1).
1
pf3
pf4
pf5

Partial preview of the text

Download Red-Black Tree and more Exercises Data Representation and Algorithm Design in PDF only on Docsity!

CPS 100E - Program Design and Analysis I Prof. Ro dger Section: Red Black Trees (handout)

Red-Black Tree

Binary search tree

 prop erties

{ every no de is red or black { every leaf is a NULL no de and black { the ro ot is black { If no de is red, then its children are black. { Every simple path from a no de to a descendant leaf contains the same numb er of black no des.

The gure b elow shows a red-black tree. Black no des are the shaded no des, and red no des are the nonshaded no des. The rectangular no des are external no des (leaves) that only contain the value NULL.

Def: The black height (bh) of a no de x is the numb er of black no des on any path from x (not including x) to a leaf.

Example:

 bh(19)=

 bh(8)=

null

null null null null null

null null

null

Lemma: A red-black tree with n internal no des has height at most 2 log (n + 1).

Rotations

Rotations change the p ointer structure while preserving the inorder prop erty.

rotate x

a

left

a

x

y

x

b y

c

d

e

b

c

d

e

Following is pseudo co de for a left rotation. This pro cedure assumes that x is b eing rotated down and to the left, and its right-child y is not NULL. The variables x, y , and r oot are p ointers to a no de. A no de contains p ointers to its parent (denoted by p), p ointers to its children (denoted left and right), and a color (red or black).

Left-Rotate(ro ot,x) y = x! right x! right = y! left if y! left 6 = NULL then y! left! p = x y! p = x! p if x! p == NULL then ro ot = y else if x == x! p! left then // x was left child x! p! left = y else // x was right child x! p! right = y y! left = x x! p = y

a

right rotate x

x

y

b

d y

x

b

d

c

e a

c e

Case 2 is when y is black and x is a right child. In this case, two rotations and some recoloring are required to x up the tree.

Case 3 is when y is black and x is a left child. In this case, one rotation and some recoloring are required to x up the tree.

Red-Black Tree Insert

x!p is parent of x, x!p!p is grandparent of x

RB-Insert(ro ot,x) Tree-Insert(ro ot,x) // binary search tree insert color x red while x 6 =ro ot && x! p! color==red do if x!p is a left child then

. let y (a right child) denote the sibling of x!p . if y is red then .. color x!p and y black .. color x!p!p red .. x = x!p!p . else (y is black) .. if x is a right child then ... x = x!p ... Left-rotate(ro ot,x) .. end if .. color x!p black and x!p!p red .. Right-rotate(ro ot,x!p!p) . end if else (case if x!p is a right child) . let y (a left child) denote the sibling of x!p . if y is red then .. color x!p and y black .. color x!p!p red .. x = x!p!p . else (y is black) .. if x is a left child then ... x = x!p ... Right-rotate(ro ot,x) .. end if .. color x!p black and x!p!p red .. Left-rotate(ro ot,x!p!p) . end if end if end while color ro ot black

B

A D

C

D

B

A

C new x

case 1

x

y

y

x

case 3

then

case 2

d b c

a

C

B

A (^) D

e

D

C

B

A d e

c

a b

x

y

case 3

d

a b c

C

B

A

D e

y

x a b

c (^) d e A

B

C

D

Tree-Insert(ro ot,z) y = NULL x = ro ot while x 6 = NULL do y = x if z!key < x!key then x = x!left else x = x!right z!p = y if y == NULL then ro ot = z else if z!key < y!key then y!left = z else y!right = z