Splay Trees¶
Target¶
Any M consecutive tree operations starting from an empty tree take at most \(O(M \log N)\) time.
So AVL tree is a kind of splay tree.
It just means that the amortized(摊还) time is \(O(\log N)\).
Note: access: 访问、接触
Idea¶
After a node is accessed, it is pushed to the root by a series of AVL tree rotations.
Insertion¶
For any nonroot node X, denote its parent by P and grandparent by G:
P is the root¶
Rotate X and P
P is not the root¶
Zig-zag¶
Double rotation
Zig-zig¶
Single rotation
Splaying roughly halves the depth of most nodes on the path.
Deletion¶
- Find X
X will be at the root.
- Remove X
There will be two subtrees \(T_L\) and \(T_R\).
-
FindMax(\(T_L\))
-
Make \(T_R\) the right child of the root of \(T_L\)
Problem¶
1¶
D
查询完后结果如上图所示
2¶
A
查询完后,这个最大的节点就会被当作是根。根据二叉搜索树的定义可知,这个树没有右子树