AVL Trees, Splay Trees and Amortized Analysis
AVL Trees
- Target: Speed up searching (with insertion and deletion)
- Tool: Binary search trees
- Problem: Although , but the height can be as bad as
[Definition] An empty binary tree is height balanced. If is a nonempty binary tree with and as its left and right subtrees, then is height balanced if and only if
- and are height balanced, and
- where and are the heights of and , respectively.
Note: The height of an empty tree is defined to be -1, and the height of a single node is defined to be 0.
[Definition] The balance factor . In an AVL tree, .
Single Rotation
RR Rotation

LL Rotation
Double Rotation
LR Rotation

RL Rotation

Conclusion
- Single Rotation中RR对应一次逆时针,LL对应一次顺时针
- Double Rotation中从后往前读,R对应一次逆时针,L对应一次顺时针
- LR先逆时针再顺时针
- RL先顺时针再逆时针
Note: Several bf’s might be changed even if we don’t need to reconstruct the tree.

The correct answer is A.
The height of AVL Tree


Splay Trees
- Target: Any consecutive tree operations starting from an empty tree take at most time.
Note: AVL Tree is a kind of Splay Tree because every operation takes time.
- Splay Tree only means that the amortized time is . A single operation might still take time.
- The bound is weaker. But the effect is the same: There are no bad input sequences.
- Whenever a node is accessed, it must be moved to avoid the case of successive operations taking time.
- Idea: After a node is accessed, it is pushed to the root by a series of AVL tree rotations.
A simple solution
-
Only use single rotation
-
The time complexity of the worst case might be , so it doesn’t work.
Splaying

Note: The operation order of one Zig-zig is from up to down.
- Splaying not only moves the accessed node to the root, but also roughly halves the depth of most nodes on the path.

Deletions
- Step1: Find X and X will be at the root
- Step2: Remove X and there will be two subtrees and
- Step3: $FindMax ( T_L ) $ and the largest element will be the root of , and has no right child
- Step4: Make the right child of the root of
Amortized Analysis 摊还分析
- Target: Any consecutive operations take at most time. — Amortized time bound
- worst-case bound amortized bound average-case bound
- Probability is not involved in amortized bound
- 摊还分析可以保证最坏情况下每个操作的平均性能
Aggregate analysis 聚合分析
- Idea: Show that for all , a sequence of operations takes worst-case time in total. In the worst case, the average cost, or amortized cost, per operation is therefore .

- We can pop each object from the stack at most once for each time we have pushed it onto the stack
Accounting method 核算法
-
Idea: When an operation’s amortized cost exceeds its actual cost , we assign the difference to specific objects in the data structure as credit. Credit can help pay for later operations whose amortized cost is less than their actual cost.
-
The difference between aggregate analysis and accounting method is that the later one assumes that the amortized costs of the operations may differ from each other
-
For all sequences of operations, we have

Potential method 势能法
- Idea: Take a closer look at the credit :
- id defined to be the structure of the current situation
- Potential function maps the current structure of the problem into a number
- A good potential function should always assume its minimum at the start of the sequence


Splay Trees势能分析
-
势能函数是树中所有节点的 rank 之和
其中指的是子树中的节点数(包括节点),用表示节点 的 rank,
-
用表示操作后的势能,表示操作前势能

- zig
- 实际成本是一次单旋,为1
- 只有 和 的 rank 值有变化,故
- 节点 由根节点变为非根节点,故,因此
- zig-zag
-
实际成本是两次旋转,为2
-
-
操作前是根节点,操作后是根节点,rank相同,故
-
-
,根据定理可得
-
-
- zig-zig
- 实际成本是两次单旋,为2
- 操作前是根节点,操作后是根节点,rank相同,故
- [Theorem] The amortized time to splay a tree with root at node is at most .