Balanced Trees Balanced trees have height O(lg n). Height-balanced trees At each node, height of left and right subtrees are “close”. e.g.

Download Report

Transcript Balanced Trees Balanced trees have height O(lg n). Height-balanced trees At each node, height of left and right subtrees are “close”. e.g.

Balanced Trees
Balanced trees have height O(lg n).
Height-balanced trees
At each node, height of left and right subtrees are “close”.
e.g. AVL trees, B-trees, red-black trees, splay trees
Weight-balanced trees
At each node, number of nodes in left and right subtrees
are “close”.
Search, Predecessor, Successor, Minimum, Maximum
O(lg n) time.
Insert and Delete may have to rebalance the tree.
Rebalancing Heuristic -- Rotation
Right-Rotate(T, y)
y
x
Left-Rotate(T, x)
x
CC
A
A
B
B
Rotation
Preserves the inorder key sequence.
Takes O(1) time.
To be used by insertion and deletion on a red-black tree.
y
An Example of Rotation
x
77
11
44
33
22
66
y
18
99
14
14
Left-Rotate(x)
17
17
12
12
y
18
x
11
1919
2020
2222
Red-Black Trees
A “balanced” binary search tree with height (lg n).
Basic dynamic-set operations:
Search, Predecessor, Successor, Minimum
Maximum, Insert, Delete
all take O(lg n) time.
Node of a Red-Black Tree
color
key
left
right
parent
NIL as pointers to external nodes (leaves) of the tree.
Key-bearing nodes as internal nodes of the tree.
Red-Black Properties
12
internal
node
8
14
NILl
4
9
NILl
NILl
15
NILl
NILl
external node (requiring
no extra storage)
5
NILl
NILl
NILl
1. Every node is either red or black.
2. The root is black.
3. Every leaf (NIL) is black.
No path is more than twice
as long as any other.
4. If a node is red, then both of its children are black.
5. Every simple path from the root to a descendant leaf
contains the same number of black nodes.
Sentinel (Save Storage)
parent
12
8
4
14
9
15
5
nil(T)
All Nodes Are Black
A complete binary tree!
12
8
14
4
NILl
9
NILl
NILl
13
NILl
NILl
20
NILl
NILl
NILl
Red nodes may be seen as “fill-ins” to a complete binary search tree.
Black-Height
The black-height of a node x, denoted bh(x), is the number
of black nodes on any path from x (excluded) to a leaf.
bh = 3
17
2
bh = 2
14
21
2
10
1
1
1
7
1
1
1
12
15
23
19
16
NILl
20
NILl
1
NILl
1
3
NILl
NILl NILl
NILl
NILl NILl
NILl
NILl
NILl
A node at height h has black-height ≥ h/2.
NILl
#Internal Nodes vs Black Height
Claim
bh(x)
Subtree rooted at node x contains  2 – 1 internal nodes.
Proof
By induction on the height h of x.
h=0
0
2 – 1 = 0 internal node.
xx
h>0
black height
or
x
≥bh(x)–1
bh(x)–1
x
≥bh(x)–1
bh(x)–1
height ≤ h – 1
#internal nodes
(by induction)
bh(x)–1
≥2 – 1
bh(x)–1
internal node total: ≥ 2
bh(x)–1
≥2 –1
bh(x)–1
bh(x)–1
≥2–1
bh(x)
– 1 + 2 – 1+1=2 –1
bh(x)–1
≥2–1
#Internal Nodes vs Height
Lemma
A red-black tree with n internal nodes has height at
most 2lg (n+1).
Proof The root of a red-black tree of height h has black-height  h/2.
This is because on any path down from the root a red node is always
followed by a black node (but not necessarily vice versa).
By the claim there are at least 2 h/2– 1 internal nodes in the tree.
Therefore n  2 h/2 – 1 and h  2 lg(n+1).
Corollary
Search, Minimum, Maximum, Successor, and Predecessor
can be done in O(lg n) time.