3.
1 Height-Balanced Trees
3.2 Weight-Balanced Trees
Chen Song
Height-Balanced Trees
Height-Balanced Trees
Height – The maximum length of any path from the root
to a leaf.
Height-Balanced Tree –
In each interior node, the height of the right
subtree and left subtree differ by at most one.
Height-Balanced Trees
Example:
Height-Balanced Trees
Theorem
A height-balanced tree of height h has at least
3+ 5 1+ 5 ℎ 3− 5 1− 5 ℎ
( )( ) −( )( )
2 5 2 2 5 2
leaves.
Height-Balanced Trees
Tree T
h-2
h-1
Tree T has at least leaves:
leaves(h)=leaves(h-1)+leaves(h-2)
leaves(0)=1
leaves(1)=2
Characteristic equation:
xh=xh-1+xh-2 => x2-x-1=0
Height-Balanced Trees
Insert and Delete
After insert or delete:
Case 1: Tree is still balanced
Case 2: Tree is not balanced at node n
|n->left-height − n->right-height|=2
Height-Balanced Trees
1. n->left->height = n->right->height+2 and
n->left->left->height=n->right->height+1
Right rotation on node n
2. n->left->height = n->right->height+2 and
n->left->left->height=n->right-height
Left rotation on node n->left, and follow right rotation
on node n
Height-Balanced Trees
3. n-> right->height = n->left->height+2 and
n->right->right->height=n->left->height+1
Left rotation on node n->left
4. n->right->height = n->left->height+2 and
n->right->right->height=n->left-height
Right rotation on node n->right, and follow left rotation
on node n
Height-Balanced Trees
Height-Balanced Tree structure supports search, insert, and
delete in O(logn) time
Search => O(logn)
Insert => search + insert + rebalance => O(logn)
O(logn) O(1) O(logn)
Delete => search + delete + rebalance => O(logn)
O(logn) O(1) O(logn)
Weight-Balanced Trees
Weight-Balanced Trees
Weight –
The number of leaves of a tree.
Weight-Balanced Tree –
The weight of the right and left subtree in each
node differ by at most one.
Weight-Balanced Trees
α-weight-balanced trees
For each subtree, the left and right sub-subtrees has
each at least a fraction of α of total weight of the subtree.
Tree T:
αWT≤WT1≤(1-α)WT
αWT≤WT2≤(1-α)WT
T2
T1
Weight-Balanced Trees
Theorem
An α-weight-balanced tree of height h≥2 has at least
1 ℎ
( ) leaves.
1−α
Weight-Balanced Trees
Rebalance
2 1
n is current node α∈[ , 1 − ]
7 2
Case 1:
n->left->weight ≥ α*n->weight and
n->right->weight ≥ α*n->weight
No rebalancing
Weight-Balanced Trees
Case 2:
n->right->weight ≥ α*n->weight
If n->left->left->weight > (α+ε)n->weight,
do right rotation on node n
Else left rotation on n->left, followed right rotation on
node n.
2 1
ε≤ α -2α+
2
Weight-Balanced Trees
Case 3:
n->left->weight ≥ α*n->weight
If n->right->right->weight > (α+ε)n->weight,
do left rotation on node n
Else right rotation on n->right, followed left rotation
on node n.
Weight-Balanced Trees
Theorem
The weight-balanced tree structure supports search, insert,
and delete in O(logn) time.
Search => O(logn)
Insert => search + insert + rebalance => O(logn)
O(logn) O(1) O(logn)
Delete => search + delete + rebalance => O(logn)
O(logn) O(1) O(logn)
END