You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
t = tree('root');
[ t node1 ] = t.addnode(1, 'Node 1'); %% attach to root
% node1 now contains the index of the first node.
[ t node2 ] = t.addnode(1, 'Node 2'); %% attach to root
[ t node11 ] = t.addnode(node1, 'Child of node 1'); %% attach to first node
disp(t.tostring)
t=t.removenode(node1);
disp(t.tostring);
t.get(1)
t.get(node2) // wrong result return!
I expected the result of get(node2) is 'Node 2', but get the 'Child of node 1'! why???
where is wrong?
The text was updated successfully, but these errors were encountered:
This happen because the id of every node added after node1 depends on node1 presence
Removing node1 makes every node with higher id than it become smaller
t = tree('root'); %% root is id = 1
[ t node1 ] = t.addnode(1, 'Node 1'); %% 'Node 1' has id = 2
[ t node2 ] = t.addnode(1, 'Node 2'); %% 'Node 2' has id = 3
[ t node11 ] = t.addnode(node1, 'Child of node 1'); %% 'Child of node 1' has id = 4
disp(t.tostring)
t=t.removenode(node1);%% every id higher than 1 is reduced by 1, and 'Child of node 1' is to be inserted in its place
disp(t.tostring);
t.get(node2) // rightly result what is now in id=3. which is 'Child of node 1' and not 'Node 2' anymore.
The tree structure is implemented as a list of nodes and parents ( though, i think it would've been better to implement it as a list of node / parent / child vector. While harder to manage, it would've been much faster to delete parents, create the depth and breadth iterators that are used quite a lot and such )
t = tree('root');
[ t node1 ] = t.addnode(1, 'Node 1'); %% attach to root
% node1 now contains the index of the first node.
[ t node2 ] = t.addnode(1, 'Node 2'); %% attach to root
[ t node11 ] = t.addnode(node1, 'Child of node 1'); %% attach to first node
disp(t.tostring)
t=t.removenode(node1);
disp(t.tostring);
t.get(1)
t.get(node2) // wrong result return!
I expected the result of get(node2) is 'Node 2', but get the 'Child of node 1'! why???
where is wrong?
The text was updated successfully, but these errors were encountered: