1 of 78

Tree Rotation and Red-Black Trees

1

Lecture 18 (Data Structures 4)

CS61B, Spring 2024 @ UC Berkeley

Slides credit: Josh Hug

2 of 78

B-Trees Are Ugly to Implement

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

3 of 78

The Bad News

B-Trees for small L, e.g. 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

4 of 78

Definition of Tree Rotation

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

5 of 78

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

6 of 78

Tree Rotation Definition (Demo)

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

I’m going left!!

7 of 78

Tree Rotation Definition (Demo)

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

I’m going left!!

I’ll be G’s new boss.

8 of 78

Tree Rotation Definition (Demo)

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

I went left of P.

I am G’s new boss.

9 of 78

Tree Rotation Definition (Demo)

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

I am G’s new boss.

I don’t make sense.

I went left of P.

10 of 78

Tree Rotation Definition (Demo)

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

I am G’s new boss.

I don’t make sense.

I went left of P.

Where should go?

k

j

l

11 of 78

Tree Rotation Definition (Demo)

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

I am G’s new boss.

I went left of P.

Where should go: To the right of G.

k

j

l

I got transferred from P to G.

12 of 78

Tree Rotation Definition (All in One Slide)

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

I’m going left!!

I went left.

I’ll be G’s new boss.

I am G’s new boss.

G

C

P

k

r

A

B

j

l

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

13 of 78

Tree Rotation Definition (Alternate Interpretation)

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!

14 of 78

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

15 of 78

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.

G

C

P

k

r

A

B

j

l

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

C

k

r

A

B

j

l

G

P

C

k

r

A

B

j

l

G P

16 of 78

Tree Balancing

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

17 of 78

BSTs

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

1

3

2

3

2

1

18 of 78

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!

19 of 78

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

20 of 78

Rotation for Balance

Rotation:

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

Can use rotation to balance a BST.

  • 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

21 of 78

Demo: Balancing with Tree Rotation

4

1

6

13

9

17

8

22 of 78

Demo: Balancing with Tree Rotation

4

1

6

13

9

17

8

rotateLeft(9)

23 of 78

Demo: Balancing with Tree Rotation

4

1

6

17

13

9

8

24 of 78

Demo: Balancing with Tree Rotation

4

1

6

17

13

9

8

rotateLeft(6)

25 of 78

Demo: Balancing with Tree Rotation

1

6

8

17

13

9

4

26 of 78

Demo: Balancing with Tree Rotation

1

6

8

17

13

9

4

rotateLeft(1)

27 of 78

Demo: Balancing with Tree Rotation

4

6

8

17

13

9

1

28 of 78

Demo: Balancing with Tree Rotation

4

6

8

17

13

9

1

rotateRight(6)

29 of 78

Demo: Balancing with Tree Rotation

1

4

8

17

13

9

6

30 of 78

Demo: Balancing with Tree Rotation

1

4

8

17

13

9

6

4

1

6

13

9

17

8

rotateLeft(9)

rotateLeft(6)

rotateLeft(1)

rotateRight(6)

31 of 78

Some Rotations are Undefined

  • Rotating a node right is undefined if that node has no left child.
    • We would need to promote that node's left child, but it doesn't exist.
  • Rotating a node left is undefined if that node has no right child.
  • We won't need to perform any undefined rotations in this lecture, so don't worry about them.

4

1

rotateRight(1)

???

32 of 78

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

33 of 78

The 2-3 Tree Isometry

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

34 of 78

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.

35 of 78

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

????

36 of 78

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

37 of 78

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”.

38 of 78

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.

39 of 78

LLRB Properties

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

40 of 78

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

41 of 78

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

42 of 78

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

43 of 78

LLRB Problem #1: yellkey.com/view

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

44 of 78

LLRB Problem #1: yellkey.com/view

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.

45 of 78

LLRB Problem #2: yellkey.com/chair

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

46 of 78

LLRB Problem #2: yellkey.com/chair

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

47 of 78

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

48 of 78

Extra: LLRB Invariants

Lecture 18, CS61B, Spring 2024

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.

49 of 78

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

50 of 78

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.

51 of 78

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.

52 of 78

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.

53 of 78

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

54 of 78

Maintaining Isometry with Rotations

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

55 of 78

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.

56 of 78

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.�

57 of 78

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)

58 of 78

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

add(E)

add(E)

add(E)

World 2-3

59 of 78

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

add(S)

add(S)

World 2-3

60 of 78

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. What rotation fixes this?

B

A

E

B

A

E S

B

A

E

B

A

E

S

LLRB World

add(S)

add(S)

World 2-3

B

A

S

E

Hint: This is the correct representation of the 2-3 tree.

What rotation operation gives us this tree?

61 of 78

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

add(S)

rotateLeft(E)

add(S)

World 2-3

62 of 78

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

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”.

World 2-3

63 of 78

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)

64 of 78

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)

Hint: This is the correct representation of the 2-3 tree.

What rotation operation gives us this tree?

B

A

S

E

Z

65 of 78

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)

66 of 78

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?

67 of 78

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

G

B

X

A

C

Hint 2: This is the correct representation of the 2-3 tree.

How do we get this tree?

�Hint 3: We don't need rotation.

68 of 78

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.

69 of 78

… 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.

70 of 78

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)

71 of 78

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)

72 of 78

Optional Exercise

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

73 of 78

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

74 of 78

Runtime and Implementation

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

75 of 78

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

76 of 78

Search Tree Summary

Lecture 18, CS61B, Spring 2024

B-Trees Are Ugly to Implement

Tree Rotation

  • Definition
  • Tree Balancing

Left Leaning Red-Black Trees (LLRBs)

  • The 2-3 Tree Isometry
  • LLRB Properties
  • Maintaining Isometry with Rotations
  • Optional Exercise
  • Runtime and Implementation

Search Tree Summary

77 of 78

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.

78 of 78

… 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.