Consecutive keys in a binary search tree 2026-10-31
Two stored keys are consecutive when no stored key lies strictly between them. Their numerical difference need not be one. After splaying on followed by splaying on , the node is the left child of with an empty right subtree. To prove this, after the first call is the minimum of the right subtree of . The last step crossing is either a zig step or a right-left zig-zag step; both make the left child of . A nonempty right subtree of would require a stored key strictly between them.
Past exam of the computer science course of the University of Oxford 2018 Algorithms 1 b Solution 2026-10-31
First perform splaying on the key to be deleted, making its node the root of a tree. Delete that root, leaving left and right subtrees . If either is empty, return the other. Otherwise find the maximum key of by following right-child references, and perform splaying on within . This makes the root of with an empty right subtree. Attach there. The result is the original splay tree with exactly the selected key removed. An unsuccessful lookup leaves the represented set unchanged.
For algorithm correctness, all keys of are smaller than the deleted key, and all keys of are larger. Splaying preserves the binary search tree property by the permitted assumption. Since is the maximum of , it has no larger key within ; since every key of exceeds , attaching as its right subtree preserves every ordering condition. All remaining keys occur once. This is the join-based deletion of a splay tree.[ref]
Self-Adjusting Binary Search Trees by Daniel Sleator and Robert Tarjan. Author-hosted publication record and abstract: splay-tree search, insertion and deletion operations
Past exam of the computer science course of the University of Oxford 2018 Algorithms 1 e Solution 2026-10-31
Write . After splaying on , that key is at the root, and is the smallest key in its right subtree. This uses the integer assumption: no stored key lies strictly between and . Thus has an empty left subtree while it remains within the right subtree of .
Now perform ordinary bottom-up splaying on . Until the final step involving , all tree rotations occur below and leave it at the root. There are only two possible final cases.
- If is the right child of , a single zig step rotates above , placing immediately to its left.
- Otherwise is a grandchild of . Its parent lies in the right subtree of and must satisfy . Hence the last two directions are right then left. A zig-zag step rotates above and then above , again placing immediately to its left. A right-right final configuration is impossible because is the minimum of this subtree.
Therefore becomes the left child of . The argument applies more generally to consecutive keys in a binary search tree, even when their numerical difference is not one.
Past exam of the computer science course of the University of Oxford 2018 Algorithms 1 f Solution 2026-10-31
Use the usual bottom-up splaying operations. We prove the stronger result that at most three successful calls suffice. No insertions or deletions are needed.
If the chosen key is the minimum stored key, first perform splaying on and then on its immediate successor. The argument for consecutive keys in a binary search tree makes the left child of the successor with an empty right subtree; minimality makes its left subtree empty too. Hence is a leaf of a rooted tree. The maximum-key case is the mirror image.
Suppose is an interior key. Since at least four keys are stored, there are either two keys larger than or two smaller than . In the former case choose four consecutive stored keyswhere is the predecessor of , and are the next two successors. Perform .
Here is a complete check of the local shapes. After the first call, is the root. After the second, is the root; every key below is in its left subtree , and every key above stays in the right subtree of . These outside subtrees cannot interfere with the next search. Between and there are only . The last step of splaying on is either a zig step, leaving as its right child; a zig-zag step, with the same result; or a zig-zig step, leaving some as its right child with immediately to the right of . Consequently the possible right subtrees of are exactly among these four forms, using for a node with its left and right subtrees:Here an unexpanded or denotes a node with no children. Each row follows by the indicated one or two tree rotations in each bottom-up step. All rows end with as the right child of , with both its subtrees empty. If instead two keys below are available, reflect the construction, exchanging left and right and reversing the order of keys. This proves splaying a chosen node into a leaf for every node of every splay tree of size at least four.
Splaying 2026-10-31
In ordinary bottom-up splaying, locate the selected node by a binary search tree search and repeatedly move it toward the root. If its parent is the root, perform a zig step. Otherwise use a zig-zig step when the node and its parent occupy the same left/right directions, or a zig-zag step when their directions differ. Each tree rotation preserves the inorder traversal and hence the represented key order. The process ends with the selected key at the root. Unsuccessful access policies can instead move the last visited node to the root; arguments about successful calls should say the requested key exists.
Splaying a chosen node into a leaf 2026-10-31
Every node of a splay tree with at least four distinct keys can become a leaf of a rooted tree using only successful splaying operations. At an extreme key, access that key and then its sole adjacent stored key; the consecutive keys in a binary search tree property makes the extreme key a leaf. For an interior key with two larger stored keys, take consecutive and access ; the mirror construction handles two smaller keys.
For a quick shape proof, use for a node and its two subtrees. After accessing , the right subtree of can only be , , , or , with outside subtrees below and above . This follows by inspecting the final zig step, zig-zig step or zig-zag step that moves above the former root . In each case, accessing yields , where is a leaf and denotes an empty subtree. These rotations prove the bound for arbitrary outside subtrees.
Splay tree 2026-10-31
A splay tree is a binary search tree whose access operations use splaying to move a selected key to the root through tree rotations. It stores no explicit balance or height information. A single access can take linear time, while the standard operations have logarithmic amortized time complexity. The distinction matters: amortized analysis bounds the total cost of an operation sequence, not the worst cost of every individual operation.[ref]
Self-Adjusting Binary Search Trees by Daniel Sleator and Robert Tarjan. Author-hosted abstract: representation, operations and amortized logarithmic bound