1 of 59

CS61B, 2022

Lecture 18: Balanced Search Trees

  • Tree Rotation
  • Left Leaning Red-Black Trees
  • Maintaining Correspondence Through Rotation

2 of 59

The Bad News

2-3 trees (and 2-3-4 trees) are a real pain to implement, and suffer from performance problems. Issues include:

  • Maintaining different node types.
  • Interconversion of nodes between 2-nodes and 3-nodes.
  • Walking up the tree to split nodes.

“Beautiful algorithms are, unfortunately, not always the most useful.” - Knuth

public void put(Key key, Value val) {

Node x = root;

while (x.getTheCorrectChildKey(key) != null) {

x = x.getTheCorrectChildKey();

if (x.is4Node()) { x.split(); }

}

if (x.is2Node()) { x.make3Node(key, val); }

if (x.is3Node()) { x.make4Node(key, val); }

}

fantasy 2-3 code via Kevin Wayne

3 of 59

BST Structure and

Tree Rotation

4 of 59

BSTs

Suppose we have a BST with the numbers 1, 2, 3. Five possible BSTs.

  • The specific BST you get is based on the insertion order.
  • More generally, for N items, there are Catalan(N) different BSTs.

Given any BST, it is possible to move to a different configuration using “rotation”.

1

2

3

1

3

2

3

2

1

3

1

2

3

2

1

5 of 59

Tree Rotation Definition

rotateLeft(G): Let x be the right child of G. Make G the new left child of x.

  • Preserves search tree property. No change to semantics of tree.

G

C

P

k

r

A

B

j

l

G

C

P

k

r

A

B

j

l

6 of 59

Tree Rotation Definition

rotateLeft(G): Let x be the right child of G. Make G the new left child of x.

  • Can think of as temporarily merging G and P, then sending G down and left.
  • Preserves search tree property. No change to semantics of tree.

G

C

P

k

r

A

B

j

l

C

k

r

A

B

j

l

G P

C

k

r

A

B

j

l

G

P

For this example rotateLeft(G) increased height of tree!

7 of 59

Your Turn

rotateRight(P): Let x be the left child of P. Make P the new right child of x.

  • Can think of as temporarily merging G and P, then sending P down and right.

C

k

r

A

B

j

l

G

P

8 of 59

Your Turn

rotateRight(P): Let x be the left child of P. Make P the new right child of x.

  • Can think of as temporarily merging G and P, then sending P down and right.

C

k

r

A

B

j

l

G

P

9 of 59

Your Turn

rotateRight(P): Let x be the left child of P. Make P the new right child of x.

  • Can think of as temporarily merging G and P, then sending P down and right.
  • Note: k was G’s right child. Now it is P’s left child.

C

k

r

A

B

j

l

G

P

G

C

P

k

r

A

B

j

l

For this example rotateRight(P) decreased height of tree!

10 of 59

Balancing BSTs with Rotation

11 of 59

BSTs

Give a sequence of rotation operations that balances the tree on the left.

1

3

2

3

2

1

12 of 59

BSTs

Give a sequence of rotation operations that balances the tree on the left.

  • rotateRight(3)
  • rotateLeft(1)

1

3

2

3

2

1

1

2

3

There are other correct answers as well!

13 of 59

Rotation for Balance

Rotation:

  • Can shorten (or lengthen) a tree.
  • Preserves search tree property.

D

B

rotateRight(D)

rotateLeft(B)

>D

> B and < D

< B

D

B

>D

> B and < D

< B

14 of 59

Rotation for Balance

Rotation:

  • Can shorten (or lengthen) a tree.
  • Preserves search tree property.

Can use rotation to balance a BST: Demo

  • Rotation allows balancing of a BST in O(N) moves.

D

B

rotateRight(D)

rotateLeft(B)

>D

> B and < D

< B

D

B

>D

> B and < D

< B

15 of 59

Rotation: An Alternate Approach to Balance

Rotation:

  • Can shorten (or lengthen) a tree.
  • Preserves search tree property.

Paying O(n) to occasionally balance a tree is not ideal. In this lecture, we’ll see a better way to achieve balance through rotation. But first...

D

B

rotateRight(D)

rotateLeft(B)

>D

> B and < D

< B

D

B

>D

> B and < D

< B

16 of 59

Red-Black Trees

17 of 59

Search Trees

There are many types of search trees:

  • Binary search trees: Can balance using rotation, but we have no algorithm for doing so (yet).
  • 2-3 trees: Balanced by construction, i.e. no rotations required.

Let’s try something clever, but strange.

Our goal: Build a BST that is structurally identical to a 2-3 tree.

  • Since 2-3 trees are balanced, so will our special BSTs.

18 of 59

Representing a 2-3 Tree as a BST

A 2-3 tree with only 2-nodes is trivial.

  • BST is exactly the same!

What do we do about 3-nodes?

e

b

g

o

n

p

m

d f

b

g

o

n

p

m

e

e

b

g

o

n

p

m

????

19 of 59

Representing a 2-3 Tree as a BST: Dealing with 3-Nodes

Possibility 1: Create dummy “glue” nodes.

Result is inelegant. Wasted link. Code will be ugly.

d f

b

g

o

n

p

m

e

o

n

p

m

b

g

e

d

f

d f

d

f

20 of 59

Representing a 2-3 Tree as a BST: Dealing with 3-Nodes

Possibility 2: Create “glue” links with the smaller item off to the left.

Idea is commonly used in practice (e.g. java.util.TreeSet).

d f

b

g

o

n

p

m

e

o

n

p

m

d

f

d f

e

g

b

d

f

For convenience, we’ll mark glue links as “red”.

21 of 59

Left-Leaning Red Black Binary Search Tree (LLRB)

A BST with left glue links that represents a 2-3 tree is often called a “Left Leaning Red Black Binary Search Tree” or LLRB.

  • LLRBs are normal BSTs!
  • There is a 1-1 correspondence between an LLRB and an equivalent 2-3 tree.
  • The red is just a convenient fiction. Red links don’t “do” anything special.

22 of 59

LLRB Properties

23 of 59

Left-Leaning Red Black Binary Search Tree (LLRB)

Draw the LLRB corresponding to the 2-3 tree shown below.

x y

a s

v

u w

24 of 59

Left-Leaning Red Black Binary Search Tree (LLRB)

Draw the LLRB corresponding to the 2-3 tree shown below.

x y

a s

v

u w

s

v

u

w

x

y

a

25 of 59

Left-Leaning Red Black Binary Search Tree (LLRB)

Draw the LLRB corresponding to the 2-3 tree shown below.

Searching an LLRB tree for a key is easy.

  • Treat it exactly like any BST.

s

v

u

w

x

y

a

x y

a s

v

u w

26 of 59

LLRB Problem #1: yellkey.com/defense

How many of these are valid LLRBs, i.e. have a 1-1 correspondence with a valid 2-3 tree?

G

B

X

A

C

G

B

X

A

C

G

B

X

A

C

G

C

X

A

B

27 of 59

LLRB Problem #1: yellkey.com/person

How many of these are valid LLRBs, i.e. have a 1-1 correspondence with a valid 2-3 tree?

G

C

X

A

B

G

B

X

A

C

G

B

X

A

C

G

B

X

A

C

G

A B C

X

G

A B

X

C

G

B

X

A

C

B G

X

A

C

Equivalent 2-3

Invalid, has 4 node.

Invalid, not balanced.

Invalid, not balanced.

28 of 59

LLRB Problem #2: yellkey.com/life

How tall is the corresponding LLRB for the 2-3 tree below? (3 - nodes in pink)

D E

P

B

G

J

N

Q R

V W

A

C

F

H

I

K

M

O

S U

T

L

29 of 59

LLRB Problem #2: yellkey.com/leave

How tall is the corresponding LLRB for the 2-3 tree below? (3 - nodes in pink)

  • Each 3-node becomes two nodes in the LLRB.
  • Total height is 3 (black) + 2 (red) = 5.
  • More generally, an LLRB has no more than ~2x the height of its 2-3 tree.

L

P

U

S

R

Q

D E

P

B

G

J

N

Q R

V W

A

C

F

H

I

K

M

O

S U

T

L

Dark line shows longest path (3 links).

3 black links

2 red links

30 of 59

LLRB Balance

Because 2-3 trees have logarithmic height, and the corresponding LLRB has height that is never more than ~2 times the 2-3 tree height, LLRBs also have logarithmic height!

L

P

U

S

R

Q

D E

P

B

G

J

N

Q R

V W

A

C

F

H

I

K

M

O

S U

T

L

Dark line shows longest path (3 links).

3 black links

2 red links

31 of 59

Note on Hidden Slides

A somewhat more formal look at heights of LLRBs follows in the hidden slides after this one.

  • This is covered in the web videos, but honestly, I don’t think the argument is necessary.
  • These slides include two invariants you might find interesting.

32 of 59

LLRB Height

Suppose we have a 2-3 tree of height H.

  • What is the maximum height of the corresponding LLRB?

D E

P

B

G

J

N

Q R

V W

A

C

F

H

I

K

M

O

S U

T

L

33 of 59

LLRB Height

Suppose we have a 2-3 tree of height H.

  • What is the maximum height of the corresponding LLRB?
    • Total height is H (black) + H + 1 (red) = 2H + 1.

D E

P

B

G

J

N

Q R

V W

A

C

F

H

I

K

M

O

S U

T

L

Worst case would be if these were both 3 nodes.

34 of 59

Left-Leaning Red Black Binary Search Tree (LLRB) Properties

Some handy LLRB properties:

  • No node has two red links [otherwise it’d be analogous to a 4 node, which are disallowed in 2-3 trees].
  • Every path from root to null has same number of black links [because 2-3 trees have the same number of links to every leaf]. LLRBs are therefore balanced.

35 of 59

Extra Slide: Root-to-Leaf vs. Root-to-Null

A version of this lecture from many years ago had a subtle error in its definition of “perfect black balance”. Specifically, it stated:

  • The number of black links to any leaf must be the same.

In fact, the correct invariant is:

  • The number of black links to any null link must be the same.

An example of a red-black tree which satisfies the erroneous invariant, but has no corresponding 2-3 tree:

G

B

X

A

Invalid LLRB!

B G

X

A

2-3 tree missing child.

36 of 59

Left-Leaning Red Black Binary Search Tree (LLRB) Properties

Some handy LLRB properties:

  • No node has two red links [otherwise it’d be analogous to a 4 node, which are disallowed in 2-3 trees].
  • Every path from root to null has same number of black links [because 2-3 trees have the same number of links to every leaf]. LLRBs are therefore balanced.

G

C

X

A

B

G

B

X

A

C

G

B

X

A

C

G

B

X

A

C

Invalid, B has two red links.

Invalid, not

black balanced.

Invalid, not

black balanced.

Valid

37 of 59

LLRB Construction

One last important question: Where do LLRBs come from?

  • Would not make sense to build a 2-3 tree, then convert. Even more complex.
  • Instead, it turns out we implement an LLRB insert as follows:
    • Insert as usual into a BST.
    • Use zero or more rotations to maintain the 1-1 mapping.

38 of 59

Maintaining 1-1 Correspondence Through Rotations

39 of 59

The 1-1 Mapping

There exists a 1-1 mapping between:

  • 2-3 Tree
  • LLRB�

Implementation of an LLRB is based on maintaining this 1-1 correspondence.

  • When performing LLRB operations, pretend like you’re a 2-3 tree.
  • Preservation of the correspondence will involve tree rotations.�

40 of 59

Design Task #1: Insertion Color

Should we use a red or black link when inserting?�

S

S

E

S

E

S

E S

LLRB World

World 2-3

add(E)

add(E)

add(E)

41 of 59

Design Task #1: Insertion Color

Design Task #1: Insertion Color

Design Task #1: Insertion Color

Should we use a red or black link when inserting?

  • Use red! In 2-3 trees new values are ALWAYS added to a leaf node (at first).

S

S

E

S

E

S

E S

LLRB World

World 2-3

add(E)

add(E)

add(E)

42 of 59

Design Task #2: Insertion on the Right

Suppose we have leaf E, and insert S with a red link. What is the problem below, and what do we do about it?

B

A

E

B

A

E S

B

A

E

B

A

E

S

LLRB World

World 2-3

add(S)

add(S)

43 of 59

Design Task #2: Insertion on the Right

Suppose we have leaf E, and insert S with a red link. What is the problem below, and what do we do about it: Right links aren’t allowed, so rotateLeft(E).

B

A

E

B

A

E S

B

A

E

B

A

E

S

B

A

S

E

LLRB World

World 2-3

add(S)

rotateLeft(E)

add(S)

44 of 59

New Rule: Representation of Temporary 4-Nodes

We will represent temporary 4-nodes as BST nodes with two red links.

  • This state is only temporary (more soon), so temporary violation of “left leaning” is OK.

B

A

E S

B

A

E S Z

LLRB World

World 2-3

B

A

S

E

B

A

S

E

Z

add(Z)

add(Z)

Represents temporary 4 nodes. Temporarily violates “no red right links”.

Temporarily violates “no 4 nodes”.

45 of 59

Design Task #3: Double Insertion on the Left

Suppose we have the LLRB below and insert E. We end up with the wrong representation for our temporary 4 node. What should we do so that the temporary 4 node has 2 red children (one left, one right) as expected?

LLRB World

World 2-3

B

A

S Z

B

A

E S Z

B

A

Z

S

B

A

Z

S

E

add(E)

add(E)

46 of 59

Design Task #3: Double Insertion on the Left

Suppose we have the LLRB below and insert E. We end up with the wrong representation for our temporary 4 node. What should we do?

  • Rotate Z right.

LLRB World

World 2-3

B

A

S Z

B

A

E S Z

B

A

Z

S

B

A

Z

S

E

B

A

S

E

Z

rotateRight(Z)

add(E)

add(E)

47 of 59

Design Task #4: Splitting Temporary 4-Nodes

Suppose we have the LLRB below which includes a temporary 4 node. What should we do next?

  • Try to figure this one out! It’s a particularly interesting puzzle.

LLRB World

World 2-3

G

A B C

X

split(A/B/C)

B G

X

A

C

G

B

X

A

C

Hint: Ask yourself “What Would 2-3 Tree Do?” WW23TD?

48 of 59

Design Task #4: Splitting Temporary 4-Nodes

Suppose we have the LLRB below which includes a temporary 4 node. What should we do next?

  • Flip the colors of all edges touching B.
    • Note: This doesn’t change the BST structure/shape.

LLRB World

World 2-3

G

A B C

X

split(A/B/C)

B G

X

A

C

G

B

X

A

C

flip(B)

G

B

X

A

C

BST, the magic was inside of you all along.

49 of 59

… and That’s It!

Congratulations, you just invented the red-black BST.

  • When inserting: Use a red link.
  • If there is a right leaning “3-node”, we have a Left Leaning Violation.
    • Rotate left the appropriate node to fix.
  • If there are two consecutive left links, we have an Incorrect 4 Node Violation.
    • Rotate right the appropriate node to fix.
  • If there are any nodes with two red children, we have a Temporary 4 Node.
    • Color flip the node to emulate the split operation.

One last detail: Cascading operations.

  • It is possible that a rotation or flip operation will cause an additional violation that needs fixing.

50 of 59

Cascading Balance Example

Inserting Z gives us a temporary 4 node.

  • Color flip yields an invalid tree. Why? What’s next?

B

A

E S

B

A

E S Z

B

A

S

E

LLRB World

World 2-3

B

A

S

E

Z

B

A

S

E

Z

B S

A

E

Z

add(Z)

flip(S)

add(Z)

split(E/S/Z)

51 of 59

Cascading Balance Example

Inserting Z gives us a temporary 4 node.

  • Color flip yields an invalid tree. Why? What’s next?
  • We have a right leaning 3-node (B-S). We can fix with rotateLeft(b).

LLRB World

World 2-3

B

A

S

E

Z

B S

A

E

Z

B

A

S

E

Z

rotateLeft(B)

52 of 59

Strongly Recommended Optional Exercise

(see web video)

53 of 59

Insertion of 7 through 1

To get an intuitive understanding of why all this works, try inserting the 7, 6, 5, 4, 3, 2, 1, into an initially empty LLRB.

  • You should end up with a perfectly balanced BST!

To check your work, see this demo.

  • Or see this video walkthrough of solution.

2

1

3

6

5

7

4

54 of 59

LLRB Runtime and Implementation

55 of 59

LLRB Runtime

The runtime analysis for LLRBs is simple if you trust the 2-3 tree runtime.

  • LLRB tree has height O(log N).
  • Contains is trivially O(log N).
  • Insert is O(log N).
    • O(log N) to add the new node.
    • O(log N) rotation and color flip operations per insert.

We will not discuss LLRB delete.

  • Not too terrible really, but it’s just not interesting enough to cover. See optional textbook if you’re curious (though they gloss over it, too).

56 of 59

LLRB Implementation

Amazingly, turning a BST into an LLRB requires only 3 clever lines of code.

  • Does not include helper methods (which do not require cleverness).

private Node put(Node h, Key key, Value val) {

if (h == null) { return new Node(key, val, RED); }

int cmp = key.compareTo(h.key);

if (cmp < 0) { h.left = put(h.left, key, val); }

else if (cmp > 0) { h.right = put(h.right, key, val); }

else { h.val = val; }

if (isRed(h.right) && !isRed(h.left)) { h = rotateLeft(h); }

if (isRed(h.left) && isRed(h.left.left)) { h = rotateRight(h); }

if (isRed(h.left) && isRed(h.right)) { flipColors(h); }

return h;

}

just 3 lines

needed

to balance

57 of 59

Search Tree Summary

58 of 59

Search Trees

In the last 3 lectures, we talked about using search trees to implement sets/maps.

  • Binary search trees are simple, but they are subject to imbalance.
  • 2-3 Trees (B Trees) are balanced, but painful to implement and relatively slow.
  • LLRBs insertion is simple to implement (but delete is hard).
    • Works by maintaining mathematical bijection with a 2-3 trees.
  • Java’s TreeMap is a red-black tree (not left leaning).
    • Maintains correspondence with 2-3-4 tree (is not a 1-1 correspondence).
    • Allows glue links on either side (see Red-Black Tree).
    • More complex implementation, but significantly (?) faster.

59 of 59

… and Beyond

There are many other types of search trees out there.

  • Other self balancing trees: AVL trees, splay trees, treaps, etc. There are at least hundreds of different such trees.

And there are other efficient ways to implement sets and maps entirely.

  • Other linked structures: Skip lists are linked lists with express lanes.
  • Other ideas entirely: Hashing is the most common alternative. We’ll discuss this very important idea in our next lecture.