1 of 178

Lecture 10:

Binary Search Trees

CSE 247 / CSE 502N

Fall 2016

Ron Cytron

1

2 of 178

Where are we?

  • Lists
  • Priority Queue
  • Heapsort
  • Merge sort
  • Shortest Path
  • Hashing
  • Binary Search Trees
    • Chapter 12 of text, pp. 286 and following

2

3 of 178

Trees

  • We defined binary trees previously
    • For representing a priority queue
    • An array worked well there
  • Now we consider trees using references (pointers)
  • Each node has-a
    • left child
    • right child
    • parent
    • value
    • (do the above in eclipse)
  • We will use such a tree to represent an SortedSet

3

4 of 178

API of SortedSet<E>

  • extends Set<E>
  • by providing additional methods
    • E first()
    • E last()
    • various subsetting methods we probably won’t consider here
  • Worth our consideration:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
    • Iterator<E> iterator()
      • provides elements in sorted order
  • Let’s consider various implementations

4

5 of 178

Using an unordered List?

  • Time for:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
  • And how about
    • Iterator<E> iterator()

5

6 of 178

Using an ordered List?

  • Time for:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
  • And how about
    • Iterator<E> iterator()

6

7 of 178

Using an unordered Array?

  • Time for:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
  • And how about
    • Iterator<E> iterator()

7

8 of 178

Using an ordered Array?

  • Time for:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
  • And how about
    • Iterator<E> iterator()

8

9 of 178

Using a Hash Table? (worked well for Set)

  • Time for:
    • boolean add(E element)
    • boolean contains(E element)
    • boolean remove(E element)
    • E first()
    • E last()
  • And how about
    • Iterator<E> iterator()

9

10 of 178

Using a Priority Queue?

  • Priority Queue of n elements
    • Not only first(), but also last()
      • The first element is at a known place in a MinHeap
        • How much time to find it?
        • Θ(1) if we can peek
        • Θ(log n) if we have to extract and then insert again
      • But where is the last element?
        • It could be any leaf node
        • How much time to find it?
        • Θ(n), whether we can peek or not
    • Iterator provides elements in sorted order
      • Priority Queue had no iterator
      • We can extract and re-insert everything
        • Taking Θ(n log n) time

10

11 of 178

Binary search trees

  • Neither necessarily complete nor almost complete
  • Special relationship between a node and its children
    • a.value ≤ p.value
    • p.value ≤ b.value

11

p

a

b

12 of 178

Binary search trees

  • Neither necessarily complete nor almost complete
  • Special relationship between a node and its children
    • a.value ≤ p.value
    • p.value ≤ b.value
  • Special relationship extends between a node and its descendants

12

p

a

b

p

a

b

13 of 178

Binary search trees

  • Neither necessarily complete nor almost complete
  • Special relationship between a node and its children
    • a.value ≤ p.value
    • p.value ≤ b.value
  • Special relationship extends between a node and its descendants
  • For sets
    • No duplicates
      • So ≤ becomes <

13

p

a

b

p

a

b

14 of 178

Binary search trees

  • Not necessarily complete nor almost complete
  • Special relationship between a node and its children
    • a.value ≤ p.value
    • p.value ≤ b.value
  • Special relationship extends between a node and its descendants

14

p

a

b

p

a

b

All values over here are no greater than p’s value.

In a set they are strictly smaller.

15 of 178

Binary search trees

  • Not necessarily complete nor almost complete
  • Special relationship between a node and its children
    • a.value ≤ p.value
    • p.value ≤ b.value
  • Special relationship extends between a node and its descendants

15

p

a

b

p

a

b

All values over here are no greater than p’s value.

In a set they are strictly smaller.

All values over here are no smaller than p’s value.

In a set they are strictly larger.

16 of 178

Insertions

16

Insert 6, 5, 2, 7, 5, 8

First insertion is always the root node of the tree

6

17 of 178

Insertions

17

Insert 6, v

If v ≤ p.value, go left.

p

a

b

v ≤ p.value

18 of 178

Insertions

18

Insert 6, v

If v ≥ p.value, go right.

p

a

b

v ≥ p.value

19 of 178

Insertions

19

Insert 6, v

If v = p.value, go either way (flip a coin for balance).

p

a

b

v ≥ p.value

v ≤ p.value

20 of 178

Insertions

20

Insert 6, v

p

a

b

v ≥ p.value

Recursively apply this idea through the tree until finding a place for v.

21 of 178

Let’s get the idea from some insertions

21

Insert 6, 5, 2, 7, 5, 8

Figure 12.1 from text

22 of 178

Let’s get the idea from some insertions

22

Insert 6, 5, 2, 7, 5, 8

23 of 178

Let’s get the idea from some insertions

23

Insert 6, 5, 2, 7, 5, 8

24 of 178

Let’s get the idea from some insertions

24

Insert 6, 5, 2, 7, 5, 8

25 of 178

Let’s get the idea from some insertions

25

Insert 6, 5, 2, 7, 5, 8

26 of 178

Let’s get the idea from some insertions

26

Insert 6, 5, 2, 7, 5, 8

27 of 178

Let’s get the idea from some insertions

27

Insert 6, 5, 2, 7, 5, 8

28 of 178

Let’s get the idea from some insertions

28

Insert 6, 5, 2, 7, 5, 8

29 of 178

Let’s get the idea from some insertions

29

Insert 6, 5, 2, 7, 5, 8

30 of 178

Let’s get the idea from some insertions

30

Insert 6, 5, 2, 7, 5, 8

31 of 178

Let’s get the idea from some insertions

31

Insert 6, 5, 2, 7, 5, 8

32 of 178

Let’s get the idea from some insertions

32

Insert 6, 5, 2, 7, 5, 8

33 of 178

Let’s get the idea from some insertions

33

Insert 6, 5, 2, 7, 5, 8

34 of 178

Let’s get the idea from some insertions

34

Insert 6, 5, 2, 7, 5, 8

Could go either way for next move

35 of 178

Let’s get the idea from some insertions

35

Insert 6, 5, 2, 7, 5, 8

36 of 178

Let’s get the idea from some insertions

36

Insert 6, 5, 2, 7, 5, 8

37 of 178

Let’s get the idea from some insertions

37

Insert 6, 5, 2, 7, 5, 8

38 of 178

Let’s get the idea from some insertions

38

Insert 6, 5, 2, 7, 5, 8

39 of 178

Let’s get the idea from some insertions

39

Insert 6, 5, 2, 7, 5, 8

40 of 178

Let’s get the idea from some insertions

40

Insert 6, 5, 2, 7, 5, 8

For each insertion v, there is always a place to put v.

41 of 178

Tree depends on the order of insertion

  • First example

  • Now try

41

Insert 6, 5, 2, 7, 5, 8

Insert 2, 5, 7, 6, 8, 5

42 of 178

Another insertion order

42

Insert 2, 5, 7, 6, 8, 5

43 of 178

Another insertion order

43

Insert 2, 5, 7, 6, 8, 5

44 of 178

Another insertion order

44

Insert 2, 5, 7, 6, 8, 5

45 of 178

Another insertion order

45

Insert 2, 5, 7, 6, 8, 5

46 of 178

Another insertion order

46

Insert 2, 5, 7, 6, 8, 5

47 of 178

Another insertion order

47

Insert 2, 5, 7, 6, 8, 5

48 of 178

Another insertion order

48

Insert 2, 5, 7, 6, 8, 5

49 of 178

Another insertion order

49

Insert 2, 5, 7, 6, 8, 5

50 of 178

Another insertion order

50

Insert 2, 5, 7, 6, 8, 5

51 of 178

Another insertion order

51

Insert 2, 5, 7, 6, 8, 5

52 of 178

Another insertion order

52

Insert 2, 5, 7, 6, 8, 5

53 of 178

Another insertion order

53

Insert 2, 5, 7, 6, 8, 5

54 of 178

Another insertion order

54

Insert 2, 5, 7, 6, 8, 5

55 of 178

Another insertion order

55

Insert 2, 5, 7, 6, 8, 5

56 of 178

Another insertion order

56

Insert 2, 5, 7, 6, 8, 5

57 of 178

Another insertion order

57

Insert 2, 5, 7, 6, 8, 5

58 of 178

Another insertion order

58

Insert 2, 5, 7, 6, 8, 5

59 of 178

Another insertion order

59

Insert 2, 5, 7, 6, 8, 5

60 of 178

Another insertion order

60

Insert 2, 5, 7, 6, 8, 5

Could go either way for next move

61 of 178

Another insertion order

61

Insert 2, 5, 7, 6, 8, 5

62 of 178

Another insertion order

62

Insert 2, 5, 7, 6, 8, 5

63 of 178

Another insertion order

63

Insert 2, 5, 7, 6, 8, 5

64 of 178

Another insertion order

64

Insert 2, 5, 7, 6, 8, 5

65 of 178

Two different trees for same data

65

66 of 178

Insertion

  • Order matters
    • What is the must unbalanced tree?
    • What insertion order (ironically) creates such a tree?
  • Insertion algorithm
    • Book does this iteratively
    • I’ll do it recursively (an exercise in CLRS, but I prefer this approach)
      • Each call of the helper method returns a subtree
        • Either the original subtree
        • Or new node for the insertion
    • How do we treat equal values
      • Could always go one way or the other
      • Or could flip a coin
      • Affects how we find and delete nodes later

66

67 of 178

Listing the tree’s items in sorted order

  • All elements to the left of p are ≤ p’s value
    • So they should be listed before p’s value
  • Then p’s value should be listed
  • All elements to the right of p are ≥ p’s value
    • So they should be listed after p’s value
  • This idea applies to any node p
    • So we have a recursive algorithm
    • Starts at the tree’s root
  • This is called an inorder traversal of a binary tree
    • It only makes sense on binary trees
    • It’s like a sandwich with p in the middle

67

p

a

b

68 of 178

Tree traversal

  • An inorder tree walk (p. 288 of text)
    • Visits left
    • Acts on self
    • Visits right

68

p

69 of 178

Tree traversal

  • An inorder tree walk (p. 288 of text)
    • Visits left
    • Acts on self
    • Visits right

69

p

Process everything ≤ p’s value found to the left.

70 of 178

Tree traversal

  • An inorder tree walk (p. 288 of text)
    • Visits left
    • Acts on self
    • Visits right

70

p

If something is < p’s value, it has already been processed.

Nothing < p’s value can be found elsewhere in the tree. Things > p’s value are to the right.

So now it’s p’s turn, so act upon it.

71 of 178

Tree traversal

  • An inorder tree walk (p. 288 of text)
    • Visits left
    • Acts on self
    • Visits right

71

p

Process everything ≥ p’s value found to the right.

72 of 178

Tree traversal

  • An inorder tree walk (p. 288 of text)
    • Visits left
    • Acts on self
    • Visits right
  • There are other recursive walks of a tree
    • postorder
      • visits children before acting on self
    • preorder
      • acts on self before visiting children
    • The above apply to any kind of tree

72

p

73 of 178

More implementation

  • String toString() based on inorder traversal
  • String treeDump() based on inorder traversal
    • prints tree sideways
    • can see structure
  • E first()
    • If tree is empty what should we do?
  • E last()
  • How should we accomplish Iterator<E> iterator()?
    • More general than toString() !
    • ruby makes this so much easier than Java
      • Java requires us to implement E next()
        • Instead of accepting code to act on the next element

73

74 of 178

Is an element in the tree?

  • boolean contains(Element e)
  • Can model this after add
    • Navigate left or right
    • I’ll do it recursively
      • Book favors iteration
    • Reach the item?
      • return true
    • Run out of tree (null)
      • return false

74

75 of 178

Finding the least and greatest elements

  • E first()
  • E last()
  • What do we do if there is no such element?
  • We will need a version of these to help with remove

75

76 of 178

Where is the least element of a subtree s?

s

77 of 178

Where is the least element of a subtree?

s

78 of 178

Where is the least element of a subtree s?

s

79 of 178

Where is the least element of a subtree?

s

80 of 178

Where is the least element of a subtree?

s

Gratuitous WUTexter participation poll

Do you want to see this done

  1. Iteratively
  2. Recurisvely

81 of 178

Where is the greatest element of subtree s?

s

82 of 178

Where is the greatest element of subtree s?

s

83 of 178

Where is the greatest element of subtree s?

s

For a tree of height h, how much time do all of these methods take so far?

84 of 178

Where is the greatest element of subtree s?

s

For a tree of height h, how much time do all of these methods take so far?

Θ(h)

In terms of the number of nodes, how do we describe h?

85 of 178

Where is the greatest element of subtree s?

s

For a tree of height h, how much time do all of these methods take so far?

Θ(h)

In terms of the number of nodes, how do we describe h?

h is Ω(log n) balanced tree

h is O(n) very unbalanced tree

86 of 178

Removing a node z

  • There are two easy cases
    • Node has no children
    • Node has just one child
  • And one not so easy case
    • Node has two children

86

87 of 178

A node z with no children contains val

87

z

Looking for val at z, navigating left or right as needed

88 of 178

A node z with no children contains val

88

z

Looking for val at z, navigating left or right as needed

89 of 178

A node z with no children contains val

89

z

Looking for val at z, navigating left or right as needed

Found it!

90 of 178

A node z with no children contains val

90

Node z’s parent’s left child is set to null.

We can do this by assignment as we did with insert, recursively.

91 of 178

A node z with no children contains val

91

z

Node z’s parent’s left child is set to null.

We can do this by assignment as we did with insert, recursively.

z.left = deleteHelper(tree.left, val)

92 of 178

A node z with no children contains val

92

Node z’s parent’s left child is set to null.

We can do this by assignment as we did with insert, recursively.

if z is the node to be removed and it has no children, return null to the caller for assignment.

z

return null

93 of 178

A node z with no children contains val

93

z

Node z’s parent’s left child is set to null.

We can do this by assignment as we did with insert, recursively.

if z is the node to be removed and it has no children, return null to the caller for assignment.

z.left = null

94 of 178

A node z with one child contains val

94

z

Looking for val at z, navigating left or right as needed

95 of 178

A node z with one child contains val

95

z

Looking for val at z, navigating left or right as needed.

To delete z, have its only child take z’s place

return z.left

96 of 178

A node z with one child contains val

96

z

Looking for val at z, navigating left or right as needed.

To delete z, have its only child take z’s place

return z.left

This node has value less than z, but greater than z’s parent, so it is suitable to take z’s place.

97 of 178

A node with two children

z

98 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

99 of 178

A node with two children

We can’t pick either of z’s children necessarily to replace z.

Not a binary tree!

100 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

101 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

Any of these nodes would do

102 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

Any of these nodes would do

But less than me!

103 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

Any of these nodes would do

But less than me!

104 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

Pick one of us!

105 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

….and less than all the remaining pink values.

Pick one of us!

106 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

….and less than all the remaining pink values.

Pick one of us!

Oh, then pick the smallest among us

107 of 178

A node with two children

z

We can’t pick either of z’s children necessarily to replace z.

What we place here has to be bigger than all the yellow values.

….and less than all the remaining pink values.

Pick one of us!

That would be me

108 of 178

A node with two children

z

From z, go right, and then go left as far as possible.

Place that value at z.

109 of 178

A node with two children

z

From z, go right, and then go left as far as possible.

Place that value at z.

Then delete the pink node.

It has to be one of the earlier simple cases: it cannot have two children

110 of 178

Properties of red-black trees

  • Text Section 13.1 (p. 308)
  • At the end of every operation on such a tree:
    • 1) Every node has a color, either red or black
    • 2) The root is always black
    • 3) Every leaf node is a (the) NIL node, which is black
      • So these trees’ leaves do not contain data, but are a special sentinel node
    • 4) If a node is red, then both its children are black
      • This means a path from root to a leaf cannot contain two red nodes in a row
    • 5) For each node, all (simple) paths from the node to descendant leaves contain the same number of black nodes
  • Web site: https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
    • Interactive visualization of red black trees

110

111 of 178

111

    • 1) Every node has a color, either red or black
    • 2) The root is always black
    • 3) Every leaf node is a (the) NIL node, which is black
      • So these trees’ leaves do not contain data, but are a special sentinel node
    • 4) If a node is red, then both its children are black
      • This means a path from root to a leaf cannot contain two red nodes in a row
    • 5) For each node, all (simple) paths from the node to descendant leaves contain the same number of black nodes

112 of 178

Example from text

112

Black height: these integers show the number of black nodes from this node to any leaf, not including this node.

113 of 178

Example from text

113

Black height: these integers show the number of black nodes from this node to any leaf, not including this node.

114 of 178

Example from text

114

Black height: these integers show the number of black nodes from this node to any leaf, not including this node.

115 of 178

Example from text

115

Black height: these integers show the number of black nodes from this node to any leaf, not including this node.

Recall this number is the same on any paths to a leaf.

For node x, bh(x) is the black height of that node. The integer next to node x is bh(x).

116 of 178

Example from text

116

Black height: these integers show the number of black nodes from this node to any leaf, not including this node.

Recall this number is the same on any paths to a leaf.

For node x, bh(x) is the black height of that node. The integer next to node x is bh(x).

bh(root) is the black-height of the tree. This tree’s black height is 3.

117 of 178

Example from text

117

The root is always black

118 of 178

Example from text

118

If a node is red

119 of 178

Example from text

119

If a node is red

Then both its children are black

120 of 178

Example from text

120

If a node is red

Then both its children are black

This keeps us from getting two reds in a row heading toward a leaf

121 of 178

Example from text

121

These are not null references, they are each a real node named NIL.

These nodes serve as sentinels to make the code easier to write and the proofs simpler.

122 of 178

Example from text

122

These are not null references, they are each a real node named NIL.

These nodes serve as sentinels to make the code easier to write and the proofs simpler.

They add one more black node on every path, so you would think we could do without them. They are not there to make the black node count right. They are there to simplify the code (which you will appreciate later).

123 of 178

Example from text

123

These are not null references, they are each a real node named NIL.

But because they are all the same, we don’t need them to be different nodes, so…..

They add one more black node on every path, so you would think we could do without them. They are not there to make the black node count right. They are there to simplify the code (which you will appreciate later).

124 of 178

Example from the text

124

They can all point to the same node, called T.nil in the text.

125 of 178

Example from the text

125

All nodes except T.nil have a parent reference.

It helps for the root to have one too, and it points to T.nill

126 of 178

We draw the trees like this

126

but every unspecified reference, including those on apparent leaves, goes to T.nil, which we just won’t draw to keep the pictures clean

127 of 178

Height of a red-back tree of n nodes is O(log n)

Lemma 13.1 of text: A red-black tree with n internal nodes has height at most 2 lg(n + 1)

Proof by induction on k using

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

P(0): node x has height 0 → x is T.nil → bh(x) = 0

then x has 0 internal nodes

0 = 20 - 1 so P(0) is true

127

restatement

128 of 178

Height of a red-back tree of n nodes is O(log n)

Lemma 13.1 of text: A red-black tree with n internal nodes has height at most 2 lg(n + 1)

Proof by induction on k using

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

Now consider a node x of positive height, so

it must be an internal node

it must have 2 children (all internal nodes do: T.nil if nothing else)

128

129 of 178

Consider that node x

129

x

height: h > 0

black height: bh(x)

130 of 178

Consider that internal node x

130

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

131 of 178

Consider that internal node x

131

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

For each of these children, there are two cases

132 of 178

Consider that internal node x

132

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

For each of these children, there are two cases

If the child is black, then it has black height bh(x)-1

133 of 178

Consider that internal node x

133

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

For each of these children, there are two cases

If the child is black, then it has black height bh(x)-1

If the child is red, then it has black height bh(x)

134 of 178

Consider that internal node x

134

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

For each of these children, there are two cases

If the child is black, then it has black height bh(x)-1

If the child is red, then it has black height bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

The children are covered by P(h-1)

135 of 178

Consider that internal node x

135

x

height: h > 0

black height: bh(x)

x must have 2 children

These nodes have height h-1

For each of these children, there are two cases

If the child is black, then it has black height bh(x)-1

If the child is red, then it has black height bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

The children are covered by P(h-1)

→ it has at least 2bh(x)-1-1 internal nodes

→ it has at least 2bh(x)-1 internal nodes

The worst case

136 of 178

Consider that internal node x

136

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

137 of 178

Consider that internal node x

137

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

138 of 178

Consider that internal node x

138

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+

139 of 178

Consider that internal node x

139

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+

140 of 178

Consider that internal node x

140

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+ 1

141 of 178

Consider that internal node x

141

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+ 1

----------------------

2 x 2bh(x)-1 -2 + 1

142 of 178

Consider that internal node x

142

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+ 1

----------------------

2 x 2bh(x)-1 -2 + 1

= 2bh(x) - 1

143 of 178

Consider that internal node x

143

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+ 1

----------------------

2 x 2bh(x)-1 -2 + 1

= 2bh(x) - 1

144 of 178

Consider that internal node x

144

x

height: h > 0

black height: bh(x)

P(k): node x has height k → subtree rooted at x has at least 2bh(x)-1 internal nodes

each child has at least 2bh(x)-1-1 internal nodes

Let’s add up what we have

2bh(x)-1-1

+ 2bh(x)-1-1

+ 1

----------------------

2 x 2bh(x)-1 -2 + 1

= 2bh(x) - 1

QED

145 of 178

To finish the argument

  • If the root has height h
  • Its black height is at least h/2
    • Because there are never two reds in a row
  • If n is the number of nodes in the tree
    • n ≥ 2h/2-1
    • n+1 ≥ 2h/2
    • lg(n+1) ≥ lg(2h/2)
    • lg(n+1) ≥ h/2
    • h ≤ 2lg(n+1)
    • h = O(lg n)
  • So if we maintain red-black tree properties
    • And don’t spend too much time doing that
    • Our tree will have height O(lg n), and our operations take that time

145

146 of 178

We balance by rotating

146

Maintains: α < x < β < y < γ

147 of 178

Example from text

147

148 of 178

Insertion into red-black tree

  • Like what we did with unbalanced binary search trees
    • Except each node has a color, and new nodes are always red
      • Triggers consideration of balancing
      • The red new node may have a red parent → action required
    • Inserted nodes always have T.nil as their left and right child
  • There are three cases of interest
    • One case simply recolors things, but does no rotations
      • And the process may continue up the tree to the root
    • The other two cases cause rotation(s), but at most 2
      • Left or right
      • But no further action up the tree is necessary
  • Balancing takes O(lg n) time and performs at most two rotations

148

149 of 178

Demo

1, 2, 25, 35, 3, 5, 8, 55, 60, 90, 80, 12, 75, 25, 50

149

150 of 178

The three cases

150

In this loop, z is always a red node whose parent is also red.

151 of 178

The three cases

151

The .p is the “parent of”

152 of 178

The three cases

152

Code is written for z’s parent being a left child.

153 of 178

The three cases

153

How do we know y exists? What if 7 had no right child?

154 of 178

The three cases

154

How do we know y exists? What if 7 had no right child?

y?

155 of 178

The three cases

155

How do we know y exists? Thanks to NIL it always does.

y?

NIL

156 of 178

The three cases

156

Case 1: transfers z’s grandparent’s black to z’s parent and aunt.

157 of 178

The three cases

157

To preserve black heights, 5 and 8 will become black and 7 will become red.

158 of 178

The three cases

158

z moves up the tree by two nodes, but that node (7) is definitely red. So the loop may continue.

159 of 178

The three cases

159

Otherwise the red-red problem is fixed by either one right rotation

160 of 178

The three cases

160

Otherwise the red-red problem is fixed by either one right rotation, or a left rotation followed by a right rotation.

161 of 178

The three cases

161

When done, z’s parent is black so the loop will exit.

162 of 178

The three cases

162

163 of 178

The three cases

163

164 of 178

The three cases

164

Case 1 no longer applies: z’s aunt is not red

165 of 178

The three cases

165

166 of 178

The three cases

166

167 of 178

The three cases

167

We know case 2 leaves a problem that is solved by case 3

168 of 178

The three cases

168

We know case 2 leaves a problem that is solved by case 3

169 of 178

The three cases

169

In case 3, z is its parent’s left child

170 of 178

The three cases

170

171 of 178

The three cases

171

z’s parent is black so the loop exits

172 of 178

The black height of the tree never changed

172

Black height was 2 before inserting 4.

It’s also 2 with the red-red problem.

173 of 178

The black height of the tree never changed

173

Still 2.

174 of 178

The black height of the tree never changed

174

Still 2.

175 of 178

The black height of the tree never changed

175

Still 2.

176 of 178

The black height of the tree never changed

176

Then what causes the black height of the tree to increase on insertion?

177 of 178

The black height of the tree never changed

177

Then what causes the black height of the tree to increase on insertion?

The tree’s black height must be the same on all paths to leaves.

178 of 178

The black height of the tree never changed

178

Then what causes the black height of the tree to increase on insertion?

The tree’s black height must be the same on all paths to leaves.

So the root must change to red, preserving the height, and then to black, increasing the height, for the tree’s height to increase.