1. Update with the left of the parent node. Each update will update the child nodes of the same level, their parent nodes, and the nodes at the same level as their parent nodes.
if has parent
pleft = parent left
update left = left+2 where left >pleft
update right = right+2 where right>pleft
insert left=pleft +1 ,right = pleft +2
else
insert into left = max(right) +1,
right = left +2
Example:
Parent Node 1: 1 2
Insert child node: parent node 1 is 1 4 , child node 1 is 2 3 ;
Insert child nodes: parent node 1 is 1 6 , child node 1 is 4 5, child node 2 is 2 3;
Insert parent node: parent node 1 is 1 6 , child node 1 is 4 5, child node 2 is 2 3; parent node 2 is 7 8;
Insert child nodes: parent node 1 is 1 8, child node 1 is 4 5, child node 2 is 6 7, child node 3 is 2 3; parent node 2 is 9 10;
2. Update with the right of the parent node,
update its parent node and nodes at the same level as its parent node each time
(
recommended
)
if has parent
pright = parent right
update left = left+2 where left >= pright
update right = right+2 where right>= pright
insert left=pright ,right = pright + 1
else
insert into left = max(right) +1,
right = left +2
Example:
Parent Node 1: 1 2
Insert child node: parent node 1 is 1 4 , child node 1 is 2 3 ;
Insert child nodes: parent node 1 is 1 6 , child node 1 is 2 3, child node 2 is 4 5;
Insert parent node: parent node 1 is 1 6 , child node 1 is 2 3, child node 2 is 4 5; parent node 2 is 7 8;
Insert child nodes: parent node 1 is 1 8 , child node 1 is 2 3, child node 2 is 4 5, child node 3 is 6 7; parent node 2 is 9 10;