Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a solution that lets you move a node to any position in the tree, either as a sibling or a child with just a single input parameter - the new left position (newlpos) of the node.</p> <p>Fundamentally there are three steps: </p> <ul> <li>Create new space for the subtree.</li> <li>Move the subtree into this space.</li> <li>Remove the old space vacated by the subtree.</li> </ul> <p>In psuedo-sql, it looks like this:</p> <pre><code>// * -- create new space for subtree * UPDATE tags SET lpos = lpos + :width WHERE lpos &gt;= :newlpos * UPDATE tags SET rpos = rpos + :width WHERE rpos &gt;= :newlpos * * -- move subtree into new space * UPDATE tags SET lpos = lpos + :distance, rpos = rpos + :distance * WHERE lpos &gt;= :tmppos AND rpos &lt; :tmppos + :width * * -- remove old space vacated by subtree * UPDATE tags SET lpos = lpos - :width WHERE lpos &gt; :oldrpos * UPDATE tags SET rpos = rpos - :width WHERE rpos &gt; :oldrpos */ </code></pre> <p>The :distance variable is the distance between the new and old positions, the :width is the size of the subtree, and :tmppos is used to keep track of the subtree being moved during the updates. These variables are defined as:</p> <pre><code>// calculate position adjustment variables int width = node.getRpos() - node.getLpos() + 1; int distance = newlpos - node.getLpos(); int tmppos = node.getLpos(); // backwards movement must account for new space if (distance &lt; 0) { distance -= width; tmppos += width; } </code></pre> <p>For a complete code example, see my blog at</p> <p><a href="http://www.ninthavenue.com.au/how-to-move-a-node-in-nested-sets-with-sql" rel="noreferrer">http://www.ninthavenue.com.au/how-to-move-a-node-in-nested-sets-with-sql</a></p> <p>If you like this solution, please up-vote.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload