# To Graft Nodes

Unexpectedly, to graft a base node into the child of another node is easy to implement in the FF model. Require prior calculation, but it can graft by one UPDATE statement.

```      DEPTH|
|
0 | [A]--+-----------+
|      |           |
1 |     [B]--+---+  [C]--+---+
|          |   |       |   |
2 |         [D] [E]     [F] [G]--+---+
|                              |   |
3 |                             [H] [I]
-----+-------------------------------------------
|  0   1   2   3   4   5   6   7   8   QUEUE```

Explain in the example to graft the node [G] to the child node of the [E].

1. QUEUE of the new parent node [E] is 3.
2. The range of QUEUE to graft sub-tree is between [G] and [I] (QUEUE from 6 to 8).
3. Node located between the [E] and [G] are shifted 3 steps as number of sub-tree nodes.
4. When depth of [E] is Ed, and depth of [G] is Gd, DEPTH of the entire sub-tree nodes are shifted steps by (Ed - Gd + 1).

Please see two figures below, you can see ranges of (2) and (3). Grafting process related to QUEUE is summarized as "(2) and (3) replaced, and then supply new number of QUEUE and DEPTH for each node".

```      DEPTH|
|
0 | [A]--+-----------+
|      |           |
1 |     [B]--+---+  [C]--+---+
|          |   |       |   |
2 |         [D] [E]     [F] [G]--+---+
|                              |   |
3 |                             [H] [I]
-----+-------------------------------------------
|  0   1   2   3   4   5   6   7   8   QUEUE
*   *---*   *-------*
(1)   (3)       (2)```
```      DEPTH|
|
0 | [A]--+-----------------------+
|      |                       |
1 |     [B]--+---+              [C]--+
|          |   |                   |
2 |         [D] [E]--+              [F]
|                  |
3 |                 [G]--+---+
|                      |   |
4 |                     [H] [I]
-----+-------------------------------------------
|  0   1   2   3   4   5   6   7   8   QUEUE
*   *-------*   *---*
(1)     (2)       (3)```

It is different to calculate QUEUE in each direction to graft left or right, but basic consideration is same.

## Damage possible of the referential integrity.

When UPDATE statement to graft nodes is issued only once. Therefore it is very low damage possible of the referential integrity.