{"id":127,"date":"2026-11-13T12:32:07","date_gmt":"2026-11-13T04:32:07","guid":{"rendered":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-splay-tree-moves-the-accessed-node-to-the-root\/"},"modified":"2026-11-13T21:50:54","modified_gmt":"2026-11-13T13:50:54","slug":"how-a-splay-tree-moves-the-accessed-node-to-the-root","status":"publish","type":"post","link":"https:\/\/hoho.live\/luda\/index.php\/2026\/11\/13\/how-a-splay-tree-moves-the-accessed-node-to-the-root\/","title":{"rendered":"How a splay tree moves the accessed node to the root"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>Short answer:<\/strong> A <strong>splay tree<\/strong> is a binary search tree that <strong>splays<\/strong> the accessed node to the root by zig-zag and zig-zig rotations. It stores no balance bit and no priority. A single operation can take <strong>O(n)<\/strong> time, and a sequence of m operations on a tree of size at most n costs <strong>O(m log n)<\/strong> amortized. Frequently used keys end up near the root. The tree is the classic amortized self-adjusting search tree.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the problem actually asks<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">AVL and red-black trees pay for a worst-case shape on every update, including keys that are never touched again. A splay tree pays on the access itself and moves that key to the root, so the next access to it is cheap. The rotation pattern matters. Always rotating the node one step toward the root, a single zig all the way up, is not enough for the amortized bound. Zig-zig, which rotates the parent first when the node and the parent are both left children or both right children, is the step that flattens a long path.<\/p>\n\n\n<p class=\"wp-block-paragraph\">The analysis uses a potential on subtree sizes. It is amortized. Building a path and then splaying the deepest node really does take linear time once, and the potential drops so that later operations are charged less.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>What the algorithm guarantees<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Search, insert, delete, split, and join are O(log n) amortized. In-order order is preserved because every step is a rotation. Extra memory is the child pointers alone. A splay of a key that is absent splays the last node touched, which is the node where an insertion should hang. Split and join are then a few pointer moves around the root, which is why splay trees are a convenient substrate for some advanced structures.<\/p>\n\n\n<p class=\"wp-block-paragraph\">Worst-case single-operation time is \u0398(n). A real-time bound is not available. Static optimality and several other access theorems hold for splay trees and are stronger than the basic balance bound. They are reasons the structure is studied, and they still do not make one access logarithmic in the worst case.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>The working steps<\/strong><\/h4>\n\n<ol class=\"wp-block-list\">\n<li>Search for the key with an ordinary binary-search walk. Remember the node where the walk stops, which is the node itself on a hit and the parent of the missing leaf on a miss.<\/li>\n<li>Splay that node to the root. While it has a grandparent and both steps are left or both are right, rotate the parent first and then the node. That is zig-zig.<\/li>\n<li>When the two steps bend, left then right or right then left, rotate the node twice. That is zig-zag.<\/li>\n<li>When only the parent remains, one rotation finishes the splay. That is zig.<\/li>\n<li>Insert by splaying the neighbor and hanging the new key off the root. Delete by splaying the key, then joining the left and right subtrees.<\/li>\n<\/ol>\n\n\n<p class=\"wp-block-paragraph\">The logarithmic figure is amortized over a sequence. Quoting it as a worst-case height is the red-black or AVL guarantee, not this one. Single rotations up the path, without zig-zig, do not give the same potential drop. The tree can look unbalanced after an access and still be inside the amortized bound.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Approaches that look simpler and fail<\/strong><\/h4>\n\n<figure class=\"wp-block-table\">\n<table>\n<thead>\n<tr>\n<th>Approach<\/th>\n<th>Why it fails when every step is a single rotation toward the root<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>Zig the node until it is the root, and never zig-zig<\/td>\n<td>Each access of a path can cost linear time without flattening the path enough. The amortized O(log n) proof uses zig-zig.<\/td>\n<\/tr>\n<tr>\n<td>Promise O(log n) on every lookup<\/td>\n<td>One lookup on a path is \u0398(n). The bound is on the sequence.<\/td>\n<\/tr>\n<tr>\n<td>Splay a missing key and discard the walk<\/td>\n<td>The last touched node is the insertion neighbor. Discarding it wastes the rotation work the next insert needed.<\/td>\n<\/tr>\n<tr>\n<td>Expect a balance factor to stay in range<\/td>\n<td>There is no balance factor. A long spine is legal as long as the amortized accounting pays for it later.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/figure>\n\n\n<p class=\"wp-block-paragraph\">Worst-case logarithmic trees are AVL and red-black trees. A randomized tree with expected logarithmic depth and no access-driven rotation is a treap. Splay is the choice when the access distribution is skewed and an amortized bound is acceptable.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>A working example<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">This Python fragment splays the accessed node to the root. A zig-zig rotates the parent first, which is the case the amortized bound needs. After searching 1 in the tree built from 1, 2, 3 the root is <code>1<\/code>.<\/p>\n\n<pre><code class=\"language-python\">class N:\n    def __init__(self, key):\n        self.key, self.left, self.right = key, None, None\n\ndef rot_right(y):\n    x = y.left\n    y.left = x.right\n    x.right = y\n    return x\n\ndef rot_left(x):\n    y = x.right\n    x.right = y.left\n    y.left = x\n    return y\n\ndef splay(n, key):\n    if n is None or n.key == key:\n        return n\n    if key &lt; n.key:\n        if n.left is None:\n            return n\n        if key &lt; n.left.key:\n            n.left.left = splay(n.left.left, key)\n            n = rot_right(n)\n        elif key &gt; n.left.key:\n            n.left.right = splay(n.left.right, key)\n            if n.left.right:\n                n.left = rot_left(n.left)\n        return rot_right(n) if n.left else n\n    if n.right is None:\n        return n\n    if key &gt; n.right.key:\n        n.right.right = splay(n.right.right, key)\n        n = rot_left(n)\n    elif key &lt; n.right.key:\n        n.right.left = splay(n.right.left, key)\n        if n.right.left:\n            n.right = rot_right(n.right)\n    return rot_left(n) if n.right else n\n\ndef insert(n, key):\n    if n is None:\n        return N(key)\n    n = splay(n, key)\n    if n.key == key:\n        return n\n    fresh = N(key)\n    if key &lt; n.key:\n        fresh.right, fresh.left, n.left = n, n.left, None\n    else:\n        fresh.left, fresh.right, n.right = n, n.right, None\n    return fresh\n\nroot = None\nfor key in (1, 2, 3):\n    root = insert(root, key)\nroot = splay(root, 1)\nassert root.key == 1\nprint(root.key)\n<\/code><\/pre>\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Where it is used<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\"><strong>GCC<\/strong> keeps an in-memory splay tree for several compiler tables, and any cache that should move the last-used node to the root is the same idea. A single access is amortized logarithmic, not worst-case. A red-black tree is the choice when one slow lookup is unacceptable.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>When you can drop this algorithm<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">Drop a splay tree when a single operation must be logarithmic, including in real time. Use a red-black tree. Keep splay trees when locality of access is real, split and join must be simple, and the caller can tolerate an occasional linear operation.<\/p>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n<h4 class=\"wp-block-heading\"><strong>Why this stays published<\/strong><\/h4>\n\n<p class=\"wp-block-paragraph\">We publish this so a search for a splay tree returns zig-zig versus a naive single rotation, and the amortized nature of the log n bound. <strong>A splay tree brings the accessed node to the root with zig-zig and zig-zag rotations, at amortized O(log n) per operation and worst-case linear time for one access.<\/strong><\/p>\n\n\n<p class=\"wp-block-paragraph\">\u672c\u6587\u7531 HoHo \u8207 AI \u5354\u4f5c\u6574\u7406\uff0c\u6700\u5f8c\u66f4\u65b0\u65bc 2026 \u5e74 11 \u6708 13 \u65e5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Short answer: A splay tree is a binary search tree that splays the accessed node to the root by zig-zag and zig-zig rotations. It stores no balance bit and no priority. A single operation can take O(n) time, and a sequence of m operations on a tree of size&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30,34],"tags":[],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-trees"],"_links":{"self":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/comments?post=127"}],"version-history":[{"count":2,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":526,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/posts\/127\/revisions\/526"}],"wp:attachment":[{"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/media?parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/categories?post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hoho.live\/luda\/index.php\/wp-json\/wp\/v2\/tags?post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}