首页
友链
关于

数据结构与算法 课堂笔记 9:伸展树(Splay Tree)

04 / 24 / 2023 (最后编辑于 04 / 24 / 2023)
预计阅读时间 4 分钟

Features

Principle

Force update the nodes in path while accessing, rather than do that when finding inbalance.

Operations

Splay

You’ll need to splay from the node you accessed all the way to the root.

Cases

Node being accessed is:

  1. Root
    Do nothing.
  2. Child of root (gets only parent)
    Rotate once:
    1. When accessing the left son, rotate right.
    2. When accessing the right son, rotate left.
  3. Have parent and grand-parent
    When accessing LR/RL grandchild, rotate down-top twice like AVL LR/RL (Zig-Zag)
    When accessing LL/RR grandchild, rotate top-down twice like AVL LL/RR (Zig-Zig)
    E.g. for node gpng\rightarrow p\rightarrow n
    When accessing node nn, first rotate gg and pp, then rotate pp and nn.

Splitting

Split(T, x) Split TT into two non-intersect parts LL and RR (xx may be not in TT), and nodes in LL are less than xx, nodes in RR are greater than xx.

Insert

Split the tree into two parts and make xx as the new root and LL RR as its child.

Delete

Splay the required node to the root and delete it (can be done using split), then join the two subtrees left.