Docsity

Red-Black Trees: Balanced Binary Search Trees, Study notes of Computer science

Toggle sidebar
bg1
7 Red-Black Trees
Binary search trees are an elegant implementation of the
dictionary data type, which requires support for
item SEARCH (item),
void INSE RT (item),
void DELETE (item),
and possible additional operations. Their main disadvan-
tage is the worst case time Ω(n)for a single operation.
The reasons are insertions and deletions that tend to get
the tree unbalanced. It is possible to counteract this ten-
dency with occasional local restructuring operations and
to guarantee logarithmic time per operation.
2-3-4 trees. A special type of balanced tree is the 2-3-4
tree. Each internal node stores one, two, or three items
and has two, three, or four children. Each leaf has the
same depth. As shown in Figure 15, the items in the in-
ternal nodes separate the items stored in the subtrees and
thus facilitate fast searching. In the smallest 2-3-4 tree of
7 15
25204 1792
Figure 15: A 2-3-4 tree of height two. All items are stored in
internal nodes.
height h, every internal node has exactly two children, so
we have 2hleaves and 2h−1internal nodes. In the largest
2-3-4 tree of height h, every internal node has four chil-
dren, so we have 4hleaves and (4h−1)/3internal nodes.
We can store a 2-3-4 tree in a binary tree by expanding a
node with i > 1items and i+ 1 children into inodes each
with one item, as shown in Figure 16.
Red-black trees. Suppose we color each edge of a bi-
nary search tree either red or black. The color is conve-
niently stored in the lower node of the edge. Such a edge-
colored tree is a red-black tree if
(1) there are no two consecutive red edges on any de-
scending path and every maximal such path ends with
a black edge;
(2) all maximal descending paths have the same number
of black edges.
ba c
or
a b
b
a
c
a
a
b
b
Figure 16: Transforming a 2-3-4 tree into a binary tree. Bold
edges are called red and the others are called black.
The number of black edges on a maximal descending path
is the black height, denoted as bh (). When we transform
a 2-3-4 tree into a binary tree as in Figure 16, we get a red-
black tree. The result of transforming the tree in Figure 15
17
20
15
25
2
9
7
4
Figure 17: A red-black tree obtained from the 2-3-4 tree in Fig-
ure 15.
is shown in Figure 17.
HEIGHT LEMMA. A red-black tree with ninternal nodes
has height at most 2 log2(n+ 1).
PROOF. The number of leaves is n+ 1. Contract each
red edge to get a 2-3-4 tree with n+ 1 leaves. Its height
is h≤log2(n+ 1). We have bh() = h, and by Rule
(1) the height of the red-black tree is at most 2bh()≤
2 log2(n+ 1).
Rotations. Restructuring a red-black tree can be done
with only one operation (and its symmetric version): a ro-
tation that moves a subtree from one side to another, as
shown in Figure 18. The ordered sequence of nodes in the
left tree of Figure 18 is
. . . , order(A), ν, order(B), µ, order(C),...,
and this is also the ordered sequence of nodes in the right
tree. In other words, a rotation maintains the ordering.
Function ZIG below implements the right rotation:
22
pf3
pf4

Partial preview of the text

Download Red-Black Trees: Balanced Binary Search Trees and more Study notes Computer science in PDF only on Docsity!

7 Red-Black Trees

Binary search trees are an elegant implementation of the dictionary data type, which requires support for

item SEARCH (item), void INSERT (item), void DELETE (item),

and possible additional operations. Their main disadvan- tage is the worst case time Ω(n) for a single operation. The reasons are insertions and deletions that tend to get the tree unbalanced. It is possible to counteract this ten- dency with occasional local restructuring operations and to guarantee logarithmic time per operation.

2-3-4 trees. A special type of balanced tree is the 2-3- tree. Each internal node stores one, two, or three items and has two, three, or four children. Each leaf has the same depth. As shown in Figure 15, the items in the in- ternal nodes separate the items stored in the subtrees and thus facilitate fast searching. In the smallest 2-3-4 tree of

7 15

2 4 9 17 2025

Figure 15: A 2-3-4 tree of height two. All items are stored in internal nodes.

height h, every internal node has exactly two children, so we have 2 h^ leaves and 2 h^ − 1 internal nodes. In the largest 2-3-4 tree of height h, every internal node has four chil- dren, so we have 4 h^ leaves and (4h^ − 1)/ 3 internal nodes. We can store a 2-3-4 tree in a binary tree by expanding a node with i > 1 items and i + 1 children into i nodes each with one item, as shown in Figure 16.

Red-black trees. Suppose we color each edge of a bi- nary search tree either red or black. The color is conve- niently stored in the lower node of the edge. Such a edge- colored tree is a red-black tree if

(1) there are no two consecutive red edges on any de- scending path and every maximal such path ends with a black edge; (2) all maximal descending paths have the same number of black edges.

a b c

a (^) or b

b

a

a c

b a

b

Figure 16: Transforming a 2-3-4 tree into a binary tree. Bold edges are called red and the others are called black.

The number of black edges on a maximal descending path is the black height , denoted as bh(̺ ). When we transform a 2-3-4 tree into a binary tree as in Figure 16, we get a red- black tree. The result of transforming the tree in Figure 15

17

20

15

25 2

9

7 4

Figure 17: A red-black tree obtained from the 2-3-4 tree in Fig- ure 15.

is shown in Figure 17.

HEIGHT LEMMA. A red-black tree with n internal nodes has height at most 2 log 2 (n + 1).

PROOF. The number of leaves is n + 1. Contract each red edge to get a 2-3-4 tree with n + 1 leaves. Its height is h ≤ log 2 (n + 1). We have bh(̺ ) = h, and by Rule (1) the height of the red-black tree is at most 2 bh(̺ ) ≤ 2 log 2 (n + 1).

Rotations. Restructuring a red-black tree can be done with only one operation (and its symmetric version): a ro- tation that moves a subtree from one side to another, as shown in Figure 18. The ordered sequence of nodes in the left tree of Figure 18 is

... , order(A), ν, order(B), μ, order(C),... ,

and this is also the ordered sequence of nodes in the right tree. In other words, a rotation maintains the ordering. Function ZIG below implements the right rotation:

B C

A A B

C

right rotation

left rotation

Zig

Zag

ν

μ ν μ

Figure 18: From left to right a right rotation and from right to left a left rotation.

Node ∗ ZIG(Node ∗ μ) assert μ 6 = NULL and ν = μ → ℓ 6 = NULL; μ → ℓ = ν → r; ν → r = μ; return ν.

Function ZAG is symmetric and performs a left rotation. Occasionally, it is necessary to perform two rotations in sequence, and it is convenient to combine them into a sin- gle operation referred to as a double rotation , as shown in Figure 19. We use a function ZIGZAG to implement a

A

right rotation ZigZag

double

ν

μ

κ

κ ν μ

B C

A

D B (^) C D

Figure 19: The double right rotation at μ is the concatenation of a single left rotation at ν and a single right rotation at μ.

double right rotation and the symmetric function ZAGZIG to implement a double left rotation.

Node ∗ ZIGZAG(Node ∗ μ) μ → ℓ = ZAG(μ → ℓ); return ZIG(μ).

The double right rotation is the composition of two single rotations: ZIGZAG(μ) = ZIG(μ) ◦ ZAG(ν). Remember that the composition of functions is written from right to left, so the single left rotation of ν precedes the single right rotation of μ. Single rotations preserve the ordering of nodes and so do double rotations.

Insertion. Before studying the details of the restructur- ing algorithms for red-black trees, we look at the trees that arise in a short insertion sequence, as shown in Figure 20. After adding 10, 7, 13, 4, we have two red edges in se- quence and repair this by promoting 10 (A). After adding

2, we repair the two red edges in sequence by a single ro- tation of 7 (B). After adding 5, we promote 4 (C), and after adding 6, we do a double rotation of 7 (D).

5

(^4 ) 2

4

5

2 7

10

6 5

13

7

2 7

6

10 4

A (^7)

D

C

13 B 4 2

10 13 4

10 10 7 13

Figure 20: Sequence of red-black trees generated by inserting the items 10, 7, 13, 4, 2, 5, 6 in this sequence.

An item x is added by substituting a new internal node for a leaf at the appropriate position. To satisfy Rule (2) of the red-black tree definition, color the incoming edge of the new node red, as shown in Figure 21. Start the

ν ν x

Figure 21: The incoming edge of a newly added node is always red.

adjustment of color and structure at the parent ν of the new node. We state the properties maintained by the insertion algorithm as invariants that apply to a node ν traced by the algorithm.

INVARIANT I. The only possible violation of the red- black tree properties is that of Rule (1) at the node ν, and if ν has a red incoming edge then it has ex- actly one red outgoing edge.

Observe that Invariant I holds right after adding x. We continue with the analysis of all the cases that may arise. The local adjustment operations depend on the neighbor- hood of ν.

Case 1. The incoming edge of ν is black. Done.

μ ν κ (^) μ

κ

ν

Figure 27: Left rotation of μ.

Case 2 has a symmetric case in which ν is the right child of μ. Case 2.2 seems problematic because it recurses without moving ν any closer to the root. However, the configura- tion excludes the possibility of Case 2.2 occurring again. If we enter Cases 2.1.2 or 2.1.3 then the termination is im- mediate. If we enter Case 2.1.1 then the termination fol- lows because the incoming edge of μ is red. The deletion may cause logarithmically many demotions but at most three rotations.

Summary. The red-black tree is an implementation of the dictionary data type and supports the operations search, insert, delete in logarithmic time each. An inser- tion or deletion requires the equivalent of at most three single rotations. The red-black tree also supports finding the minimum, maximum and the inorder successor, prede- cessor of a given node in logarithmic time each.