Rank-Balanced Trees
Siddhartha Sen, Princeton University
WADS 2009
Joint work with Bernhard Haeupler and
Robert E. Tarjan
1
Observation
Computer science is (still) a young field.
We often settle for the first (good) solution.
It may not be the best: the design space is
rich.
2
Research Agenda
For fundamental problems, systematically explore
the design space to find the best solutions,
seeking
elegance: “a quality of neatness and ingenious
simplicity in the solution of a problem (especially
in science or mathematics).”
wordnet.princeton.edu/perl/webwn
Keep the design simple, allow complexity in the
analysis.
3
Searching: Dictionary Problem
Maintain a set of items, so that
Access: find a given item
Insert: add a new item
Delete: remove an item
are efficient.
Assumption: items are totally ordered, so that binary
comparison is possible
4
Binary Search Tree
<
e
>
c
a
i
g
n
k
Why
Symmetric
not hashing?
order
5
Binary Search Tree
e
>
c
a
i
>
g
<
n
k
Access k
6
Binary Search Tree
e
>
c
a
i
<
g
n
>
h
k
Insert h
7
Binary Search Tree
Find successor
Swap
Delete
e
c
a
ki
g
n
h
ki
m
Delete i
8
Problem: imbalance
How to bound the height?
• Maintain local balance condition,
rebalance after insert or delete
balanced tree
• Restructure after each access
self-adjusting tree
Store balance information in nodes,
guarantee O(log n) height
a
b
c
d
e
After (during) insert/delete, restore
balance bottom-up (top-down):
f
• Update balance information
• Restructure along access path
9
Restructuring primitive:
(Single) Rotation
y
x
right
x
y
C
A
left
B
A
B
C
Preserves symmetric order
Changes heights
Takes O(1) time
10
Known Balanced Trees
AVL trees (“passé” according to one author)
weight balanced trees
2,3 trees
not binary
B trees
red-black trees
etc.
Goal: small height, little rebalancing, simple
algorithms
11
Ranks
Each node has an integer rank, a proxy for height
Convention: leaves have rank 0, missing nodes have
rank -1
rank of tree = rank of root
rank difference of a child =
rank of parent - rank of child
i-child: node of rank difference i
i,j-node: children have rank differences i and j
12
Example of a rank-balanced tree
3
e
1 c 2
0 a 1
2 i
1 g 1
1
1 n 1
0 h 1 0 k 1
If all rank differences are positive, rank height
13
Rank Rules
AVL trees: every node is a 1,1- or 1,2-node
Rank-balanced trees: every node is a 1,1-, 1,2-, or
2,2-node (rank differences are 1 or 2)
Red-black trees: all rank differences are 0 or 1, no
0-child is the parent of another
All need one balance bit per node.
14
Height bounds
nk = minimum n for rank k
AVL trees:
n0 = 1, n1 = 2, nk = nk-1 + nk-2 + 1, nk = Fk+3 - 1
nk = Fk+3 - 1, Fk+2 < k k log n 1.44lg n
(1 5) / 2
Rank-balanced trees:
n0 = 1, n1 = 2, nk = 2nk-2,
nk = 2k/2 k 2lg n
Same bound for red-black trees
15
Insertion example
Demote a
1
0
a
Promote a
>
1
0
b
0
1
0
Promote
Rotate left
b at b
>
c
1
0
Insert ca
b
16
Insertion example
1
2
0
a
1
2
b
>
1
0
Promote b
c
1
0
>
1
0
Insert cde
d
Promotecc
Demote
Promote
Rotate left
d at d
1
0
>
0
e
10
17
Insertion example
Demote b
0
a
2
b
1
2
2
0
Insert ef
>
c
2
1
d
1
0
1
0
Rotate left
Promote
d at d
>
e
0
1
0
>
f
Promote e
1
0
18
Insertion example
d
2
1
0
a
1
b
1
0
1
c
1
e
1
0
f
1
Insert f
19
Rebalancing: insertion
Non-terminal
0, 1, or 2 rotations
O(log n) rank changes
No 2,2 nodes = AVL
trees
log n height!
20
Deletion example
de
2
Demote b
0
1
a
Delete da
f
1
b
1
0
Double
Swap with
demote
successor
e
0
1
c
1
def
Delete
1
2
0
f
1
Double rotate at c
Double promote c
21
Deletion example
2
0
b
2
c
0
e
2
Delete f
22
Rebalancing: deletion
Non-terminal
0, 1, or 2 rotations
O(log n) rank changes
23
Amortized (time-averaged) analysis
If ti is the actual time of operation i and i is the
potential of the data structure after operation i, the
amortized time of operation i is
ai t i i i 1
a t
t a if
i
i
i
F
i
0
F
0
0
e.g., if 0 0 and F 0
24
Non-terminal cases
Must decrease
potential!
25
Insertions: 1,1-node 1,2-node
Deletions: 2,2-node 1,1- or 1,2-node
= #1,1-nodes + 2 #2,2-nodes
non-terminating steps are free,
last insertion step: Δ 2,
last deletion step: Δ 3
If there are m inserts and d deletes (n = m - d), the
number of rebalancing steps is O(m + d)
26
Rank-Balanced Trees
Red-Black Trees
height 2lg n
height 2lg n
2 rotations per rebalancing
3 rotations per rebalancing
O(1) amortized rebalancing
time
O(1) amortized rebalancing
time
Yes.
No.
Are rank-balanced trees better?
27
Better height bound?
Sequential Insertions:
rank-balanced
height = lg n (best)
red-black
height = 2lg n (worst)
Theorem. The height of a rank-balanced
tree is at most log m.
Degrades gracefully from AVL trees as d/m 1
28
Proof
Give a node a count of 1 when it is inserted
Total amount of count in tree is m
Potential of a node = total count in its subtree
When a node is deleted, its count is added to its
parent if it has one
Let k be the minimum potential of a node of rank k
Claim: k satisfies
0 = 1, 1 = 2, k = 1 + k-1 + k-2 for k > 1
m Fk+3 - 1 k
29
Proof of Claim
k = 1 + k-1 + k-2 for k > 1
Easy for 1,1- and 1,2-nodes
Harder for 2,2-nodes (created by deletions)
But counts are inherited
30
Rebalancing frequency
How high does rebalancing propagate?
O(m + d) rebalancing steps total, which implies
O((m + d) / k) insertions/deletions at rank k
Actually, we can show:
Theorem. There are O((m + d) / 2k/3) rebalancing
steps at rank k.
31
Proof
Use an exponential potential:
1,1- and 2,2-nodes of rank i get potential bi
1,2-nodes of rank i get potential bi-2
where b = 21/3
The potential change in the non-terminal steps
telescopes. Combine this effect with
initialization and terminal step.
32
Telescoping potential
= -bi+3
bi+2 - bi+3
10
bi+1 - bi+2
bi
0
bi-1
-
-
10
bi+1
10
bi
10
10
12
12
12
12
33
Fix k. Cut off growth in potential at rank k:
1,1- and 2,2-nodes of rank i: bmin{i,k-3}
1,2-nodes of rank i: bmin{i-2,k-3}
Then a rebalancing that propagates to rank k or
above decreases the potential by bk-3.
The same idea works for red-black trees (we
think).
34
Preliminary evaluation
Test
N
X
# rots
106
Red-black trees
# bals
avg.
6
pLen
10
max.
pLen
Rank-balanced trees
# rots
# bals
avg.
6
6
pLen
10
10
max.
pLen
id
8192
67108864
26.443 116.070
10.472
15.627
29.553 133.737
10.390
15.092
queue_id
8192
67108864
50.317 285.129
11.375
22.501
50.325 184.527
11.195
13.999
work_set_id 8192
67108864
41.714 185.348
10.510
16.181
43.686 159.689
10.445
15.345
zipf_id
8192
67108864
25.238 112.858
10.413
15.458
28.272 130.927
10.338
15.045
dyn_zipf_id.
8192
67108864
23.176 103.472
10.477
15.661
26.038 125.985
10.404
15.158
35
Preliminary evaluation
Test
N
X
# rots
106
Red-black trees
# bals
avg.
6
pLen
10
max.
pLen
Rank-balanced trees
# rots
# bals
avg.
6
6
pLen
10
10
max.
pLen
id
8192
67108864
26.443 116.070
10.472
15.627
29.553 133.737
10.390
15.092
queue_id
8192
67108864
50.317 285.129
11.375
22.501
50.325 184.527
11.195
13.999
work_set_id 8192
67108864
41.714 185.348
10.510
16.181
43.686 159.689
10.445
15.345
zipf_id
8192
67108864
25.238 112.858
10.413
15.458
28.272 130.927
10.338
15.045
dyn_zipf_id.
8192
67108864
23.176 103.472
10.477
15.661
26.038 125.985
10.404
15.158
36
Preliminary evaluation
Test
N
X
# rots
106
Red-black trees
# bals
avg.
6
pLen
10
max.
pLen
Rank-balanced trees
# rots
# bals
avg.
6
6
pLen
10
10
max.
pLen
id
8192
67108864
26.443 116.070
10.472
15.627
29.553 133.737
10.390
15.092
queue_id
8192
67108864
50.317 285.129
11.375
22.501
50.325 184.527
11.195
13.999
work_set_id 8192
67108864
41.714 185.348
10.510
16.181
43.686 159.689
10.445
15.345
zipf_id
8192
67108864
25.238 112.858
10.413
15.458
28.272 130.927
10.338
15.045
dyn_zipf_id.
8192
67108864
23.176 103.472
10.477
15.661
26.038 125.985
10.404
15.158
37
Preliminary evaluation
Test
N
X
# rots
106
Red-black trees
# bals
avg.
6
pLen
10
max.
pLen
Rank-balanced trees
# rots
# bals
avg.
6
6
pLen
10
10
max.
pLen
id
8192
67108864
26.443 116.070
10.472
15.627
29.553 133.737
10.390
15.092
queue_id
8192
67108864
50.317 285.129
11.375
22.501
50.325 184.527
11.195
13.999
work_set_id 8192
67108864
41.714 185.348
10.510
16.181
43.686 159.689
10.445
15.345
zipf_id
8192
67108864
25.238 112.858
10.413
15.458
28.272 130.927
10.338
15.045
dyn_zipf_id.
8192
67108864
23.176 103.472
10.477
15.661
26.038 125.985
10.404
15.158
38
Conclusion
Rank-balanced trees are a relaxation of AVL trees
with behavior at least as good as red-black trees
and better in important ways.
Especially the height bound of min{2lg n, log m}
Exponential potential functions yield new insights
into the efficiency of rebalancing. We anticipate
more applications.
39
Is rebalancing necessary?
For insertions, yes. But what about deletions?
Deletion rebalancing is complicated, ignored by
textbooks, and many database systems do not do it
So, can we avoid deletion rebalancing?
Yes. Relaxation of AVL trees, ravl trees, achieves
log m access time using lglg m + 1 balance bits
… and no rebalancing during deletion!
40
Thank you
41
Extra slides
42
© Copyright 2026 Paperzz