MCS-208 · Unit 7

Advanced Trees

Section 7 of 9

Block 3 · Unit 7Advanced Trees14 min · Section 7 of 9

7.7Splay Trees

Covers IGNOU MCS-208 §7.5, 7.5.1, 7.5.2

AVL and B-trees keep the tree balanced at all times. A splay tree takes a different approach: it does not keep any balance information, but every time a node is accessed, it is moved to the root. Frequently used keys therefore stay near the top, and over any long sequence of operations the average cost is still logarithmic.

The idea

Splaying steps

Let xx be the node being splayed, pp its parent and gg its grandparent. Splaying repeats one of three steps until xx is the root:

StepSituationRotations
Zigpp is the rootOne rotation at pp (the last step only)
Zig-zigxx and pp are both left children (or both right children): a straight lineRotate at gg first (moving pp up), then at pp (moving xx up)
Zig-zagxx is a right child and pp a left child, or vice versa: a bendRotate xx above pp, then xx above gg (as in an AVL double rotation)

In the study material’s words, “zig” means one step down to the left and “zag” one step down to the right; zig-zag and zag-zig are the two mirror-image bends.

The zig-zig step is what makes splay trees work: rotating the grandparent first roughly halves the depth of every node on the access path, while rotating xx up one level at a time would not.

Top-down splaying

The study material describes top-down splaying, which does the same work during the search instead of after it. Three trees are kept: the central tree (initially the whole tree), a left tree (keys smaller than the target) and a right tree (keys larger). Going down, each zig, zig-zig or zig-zag moves the passed nodes into the left or right tree; at the end the target is made the root, with the left tree as its left subtree and the right tree as its right subtree. It needs no parent pointers and no second pass.

Insertion and deletion

  • Insert: splay the key; if it is not present, the new key becomes the root, with the splayed tree split on either side.
  • Delete: splay the key to the root, remove the root, then splay the largest key of the left subtree to its top (it then has no right child) and attach the right subtree there.

Amortized analysis

A single splay-tree operation can take O(n)O(n) time (accessing the bottom of a chain), but any sequence of mm operations on a tree with at most nn nodes takes O(mlog⁡n)O(m \log n) time, so the amortized cost is O(log⁡n)O(\log n). Three standard methods prove such bounds:

MethodIdea
AggregateBound the total cost T(m)T(m) of mm operations; amortized cost =T(m)/m= T(m)/m, the same for every operation
AccountingOvercharge cheap operations and store the extra as credit that pays for expensive ones later
PotentialDefine a “potential energy” of the data structure; amortized cost = actual cost + change in potential

Sleator and Tarjan proved the O(log⁡n)O(\log n) amortized bound for splay trees with the potential method.

Key takeaways

  • Splay tree: BST + splaying every accessed node to the root; no balance fields.
  • Zig (parent is root), zig-zig (straight line: rotate grandparent first), zig-zag (bend).
  • Top-down splaying uses left, central and right trees during the search.
  • Amortized O(log⁡n)O(\log n) per operation; a single operation may be O(n)O(n).

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Node xx is a left child of pp, and pp is a left child of gg. Which splaying step applies?

Show answer

Answer: B. Zig-zig

xx, pp and gg are in a straight line on the left: zig-zig (rotate at gg, then at pp).

Question 2 · multiple choice

When is a single zig step used?

Show answer

Answer: C. When xx‘s parent is the root

Zig moves xx up one level and is only needed as the last step when the remaining distance is odd.

Question 3 · multiple choice

What is the amortized cost of an operation on a splay tree with nn nodes?

Show answer

Answer: B. O(log⁡n)O(\log n)

Any sequence of mm operations costs O(mlog⁡n)O(m \log n). A single operation may still cost O(n)O(n), which is its worst case, not its amortized cost.

Question 4 · multiple choice

What does a splay tree store in each node besides the key and the child pointers?

Show answer

Answer: D. Nothing else is required

Unlike AVL (balance factor), red-black (colour) and AA-trees (level), splay trees need no balance information. (A parent pointer is optional, for bottom-up splaying.)

Question 5 · multiple choice

In the example, why did 2 move from depth 6 to depth 4 when only 1 was accessed?

Show answer

Answer: C. Because zig-zig steps roughly halve the depth of every node on the access path

Rotating the grandparent first pulls the whole path up, which is why the next accesses become cheaper.

Question 6 · short answer

Explain zig, zig-zig and zig-zag with diagrams and state when each is used.

Show worked answer

Let xx be the node being splayed, pp its parent and gg its grandparent. Zig: used when pp is the root; one rotation at pp makes xx the root (e.g. p(x(A,B),C)→x(A,p(B,C))p(x(A, B), C) \to x(A, p(B, C))). Zig-zig: used when xx and pp are both left (or both right) children; first rotate at gg so that pp moves up, then rotate at pp so that xx moves up: g(p(x(A,B),C),D)→x(A,p(B,g(C,D)))g(p(x(A, B), C), D) \to x(A, p(B, g(C, D))). Zig-zag: used when xx is a right child and pp a left child (or the reverse); rotate xx above pp, then xx above gg: g(p(A,x(B,C)),D)→x(p(A,B),g(C,D))g(p(A, x(B, C)), D) \to x(p(A, B), g(C, D)). Steps repeat until xx is the root.

↑ ↓ move↵ openTab switch type