Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Binary search trees. Search, insert, delete, and split have amortized complexity O(log n) & actual complexity O(n). Actual and amortized complexity of join is O(1). Priority queue and double-ended priority queue versions outperform heaps, deaps, etc. over a sequence of operations. Two varieties.
Bottom up. Top down.
25
25
If there is a pair whose key is k, the node containing this pair is the splay node. Otherwise, the parent of the external node where the search terminates is the splay node.
If there is already a pair whose key is newPair.key, the node containing this pair is the splay node. Otherwise, the newly inserted node is the splay node.
25
If there is a pair whose key is k, the parent of the node that is physically deleted from the tree is the splay node. Otherwise, the parent of the external node where the search terminates is the splay node.
Splay
Let q be the splay node. q is moved up the tree using a series of splay steps. In a splay step, the node q moves up the tree by 0, 1, or 2 levels. Every splay step, except possibly the last one, moves q two levels up.
Splay Step
If q = null or q is the root, do nothing (splay is over). If q is at level 2, do a one-level move and terminate the splay operation.
p q a b c q a b p c
Splay Step
If q is at a level > 2, do a two-level move and continue the splay operation.
gp p q a b c d b c q a p gp d p a q b c gp
search tree.
However, a rotation is done whenever an LL or RR
move is made.
Move down 2 levels at a time, except (possibly) in the
When the splay node is reached, T, 7, and the subtree rooted at the splay node are combined into a single binary search tree.
B b D E m g e a
A C c d m f g D E e a
B b D d m f g E e a
A C c
B b
A a c E m f g e d C D
B b a
A C c d m f g D E e
B b
Two-Level Moves
A a B C c d m f g D E e b
A a c d m C D f g E e
B b
Let m be the splay node. RL move from A to C. RR move from C to E. L move from E to m.
RL Move
A a C c d m f g D E e f B b c d m g C D E e a
T 7
RL Move
T 7
B b
RR Move
T 7
RR Move
T 7
A C c d m f g D E e a
B b a
A D C E m f g e c d
B b
L Move
T 7
L Move
T 7
A a C E m f g e c d D
B b a
A D C c m f g d E
B b e
Wrap Up
m f g a C c d A D E e c
T 7
Wrap Up
m A b a C d D f g E e
T 7
B b
Wrap Up
m A a C c d D f g E e B b