<p>It can be done with a linear number of operations.</p>

<p>Suppose you start with an arbitrary given tree $T_0$ over keys $[n]$ and want to reach an arbitrary given $T$ over keys $[n]$ using splay operations. (In case we have to start with the empty tree, just insert $[n]$ in any order.)</p>

<p>A result of Cleary [*1] (see also Lucas [*2]) shows that you can get from $T_0$ to $T$ using $O(n)$ normal rotations, even if you restrict yourself to rotations at the root and at the right child of the root. </p>

<p>Let's try to simulate these restricted rotations with splay operations. Rotation at the root is easy, just splay the corresponding child.</p>

<p>What about rotations at the right child? There are two cases (see figure).
<a href="https://i.sstatic.net/mAvtw.png" rel="noreferrer"><img src="https://i.sstatic.net/mAvtw.png" alt="enter image description here"></a>
The first case (left to right) is easy: splay y, then splay x.</p>

<p>The second case (right to left) is more tricky:</p>

<p>Suppose z has a right child t. Then we can: </p>

<p>splay t, then splay z, then splay x, obtaining the required rotation.</p>

<p>We had to assume that t exists. We can make sure of this, by inserting an $\infty$ key element in the beginning and making sure that it will always remain a leaf. We can make sure it is a leaf in the beginning, for instance by splaying $\infty$, and then 1,2,3,...,n. Afterwards, t $=\infty$ is never splayed, except if the above situation arises where it is the right child of z, and then we can see that it stays a leaf after the transformation.</p>

<p>In the end, we just need to clip away this leaf and get the target tree $T$. This last step is a bit of cheating, since a proper splay deletion would clip away the leaf, then splay its parent.</p>

<p>We can get around this cheating by deleting $\infty$ earlier, i.e. the last time when the above right-to-left transformation involving $\infty$ happens. Then, after the splay z and before the splay x steps, we can do a proper splay-delete t (t $= \infty$) operation, and since t is the child of the root at that time, no unintended side-effect happens.</p>

<p>A (perhaps simpler) alternative formulation is that, if in the above right-to-left transformation z has no right child, then we do the sequence:
insert(n+1), splay(z), delete(n+1), splay(x). (assuming that insert works by inserting as a leaf and then splaying an element).</p>

<p>[*1] S. Cleary, Restricted rotation distance between binary trees,
Inform. Process. Lett. 84 (2002) 333–338</p>

<p>[*2] J. Lucas, A direct algorithm for restricted rotation distance, 
Inform. Process. Lett. 90 (2004) 129–134</p>

CC BY-SA 3.0
2016-09-02T14:29:07.027Z
Hi, thank you for answering the question. Could you change all splay operations to find operations ? If you are using it for an existing vertex, both of these operations are semantically equal (but splay operation is not allowed in my list of operations).
Bartosz Bednarczyk
CC BY-SA 3.0
2016-09-02T15:43:27.677Z
Yes, wherever I wrote "splay", it can be read as "find", i.e. "successful searches"
László Kozma
CC BY-SA 3.0
2016-09-02T10:11:43.863Z
2016-09-02T14:28:12.057Z
2016-09-02T14:28:12.057Z
László Kozma
<p>I will assume that the reader is familiar with some undergraduate algorithms and data structures. To people who are not familiar with splay trees I recommend to read through this link : <a href="https://en.wikipedia.org/wiki/Splay_tree" rel="nofollow noreferrer">https://en.wikipedia.org/wiki/Splay_tree</a></p>

<p>An interesting problem is :</p>

<blockquote>
  <p>Given a binary search tree $T$ with $n$ nodes. Find the sequence $S$ of
  operations (find, insert, delete) to obtain a Splay tree
  $T_{Splay}$, such that $T_{Splay} = T$.</p>
</blockquote>

<p>For example the splay tree equal to this one</p>

<p><a href="https://i.sstatic.net/DtVLT.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DtVLT.png" alt="Example tree"></a></p>

<p>could be generated by the following sequence: </p>

<pre><code>Insert(3), Insert(2), Insert(1), Find(2)
</code></pre>

<p>My questions are:</p>

<ul>
<li>Is it possible to obtain $T_{Splay}$ for every binary search tree $T$?</li>
<li>If the answer for the above question is positive, find an algorithm to solve this problem. What about its complexity?</li>
<li>If the answer for the above question is positive, how long need to be the sequence $S$?</li>
</ul>

<p>Unfortunately I can't find any solutions to these problems which means that either it could be hard or simply uninteresting for most of us. I found also a few links about similar problems but none of them could be applied to this case. The full list of links below:</p>

<ul>
<li><a href="https://stackoverflow.com/questions/14564437/a-sequence-that-forms-the-same-avl-and-splay-trees">https://stackoverflow.com/questions/14564437/a-sequence-that-forms-the-same-avl-and-splay-trees</a></li>
<li><a href="https://stackoverflow.com/questions/22413727/determining-if-a-binary-search-tree-can-be-constructed-by-a-sequence-of-splay-tr">https://stackoverflow.com/questions/22413727/determining-if-a-binary-search-tree-can-be-constructed-by-a-sequence-of-splay-tr</a></li>
</ul>

<p>Thanks for any answers,<br>
Bartosz Bednarczyk</p>

<ds.algorithms><ds.data-structures><tree>
For a given binary-search tree obtain an isomorphic splay tree
Bartosz Bednarczyk
# For a given binary-search tree obtain an isomorphic splay tree

(Asked by: username_0 on 2016-08-28)

I will assume that the reader is familiar with some undergraduate algorithms and data structures. To people who are not familiar with splay trees I recommend to read through this link : https://en.wikipedia.org/wiki/Splay_tree
An interesting problem is :

Given a binary search tree $T$ with $n$ nodes. Find the sequence $S$ of
  operations (find, insert, delete) to obtain a Splay tree
  $T_{Splay}$, such that $T_{Splay} = T$.

For example the splay tree equal to this one

could be generated by the following sequence: 
Insert(3), Insert(2), Insert(1), Find(2)

My questions are:

Is it possible to obtain $T_{Splay}$ for every binary search tree $T$?
If the answer for the above question is positive, find an algorithm to solve this problem. What about its complexity?
If the answer for the above question is positive, how long need to be the sequence $S$?

Unfortunately I can't find any solutions to these problems which means that either it could be hard or simply uninteresting for most of us. I found also a few links about similar problems but none of them could be applied to this case. The full list of links below:

https://stackoverflow.com/questions/14564437/a-sequence-that-forms-the-same-avl-and-splay-trees
https://stackoverflow.com/questions/22413727/determining-if-a-binary-search-tree-can-be-constructed-by-a-sequence-of-splay-tr

Thanks for any answers,
Bartosz Bednarczyk

**Tags:** ds.algorithms, ds.data-structures, tree

---

## 1 answers

### Answer by username_1 on 2016-09-02. Score: 5 | Accepted answer

It can be done with a linear number of operations.

Suppose you start with an arbitrary given tree $T\_0$ over keys $[n]$ and want to reach an arbitrary given $T$ over keys $[n]$ using splay operations. (In case we have to start with the empty tree, just insert $[n]$ in any order.)

A result of Cleary [\*1] (see also Lucas [\*2]) shows that you can get from $T\_0$ to $T$ using $O(n)$ normal rotations, even if you restrict yourself to rotations at the root and at the right child of the root.

Let's try to simulate these restricted rotations with splay operations. Rotation at the root is easy, just splay the corresponding child.

What about rotations at the right child? There are two cases (see figure).
[![enter image description here](https://i.sstatic.net/mAvtw.png)](https://i.sstatic.net/mAvtw.png)
The first case (left to right) is easy: splay y, then splay x.

The second case (right to left) is more tricky:

Suppose z has a right child t. Then we can:

splay t, then splay z, then splay x, obtaining the required rotation.

We had to assume that t exists. We can make sure of this, by inserting an $\infty$ key element in the beginning and making sure that it will always remain a leaf. We can make sure it is a leaf in the beginning, for instance by splaying $\infty$, and then 1,2,3,...,n. Afterwards, t $=\infty$ is never splayed, except if the above situation arises where it is the right child of z, and then we can see that it stays a leaf after the transformation.

In the end, we just need to clip away this leaf and get the target tree $T$. This last step is a bit of cheating, since a proper splay deletion would clip away the leaf, then splay its parent.

We can get around this cheating by deleting $\infty$ earlier, i.e. the last time when the above right-to-left transformation involving $\infty$ happens. Then, after the splay z and before the splay x steps, we can do a proper splay-delete t (t $= \infty$) operation, and since t is the child of the root at that time, no unintended side-effect happens.

A (perhaps simpler) alternative formulation is that, if in the above right-to-left transformation z has no right child, then we do the sequence:
insert(n+1), splay(z), delete(n+1), splay(x). (assuming that insert works by inserting as a leaf and then splaying an element).

[\*1] S. Cleary, Restricted rotation distance between binary trees,
Inform. Process. Lett. 84 (2002) 333–338

[\*2] J. Lucas, A direct algorithm for restricted rotation distance,
Inform. Process. Lett. 90 (2004) 129–134

> **Comments:**
> **username_0**: Hi, thank you for answering the question. Could you change all splay operations to find operations ? If you are using it for an existing vertex, both of these operations are semantically equal (but splay operation is not allowed in my list of operations).  
> **username_1**: Yes, wherever I wrote "splay", it can be read as "find", i.e. "successful searches"  

---

