



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
1 / 6
This page cannot be seen from the preview
Don't miss anything!
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.
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
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
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