Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

return wrong results when calling get() function after removing operation #5

Open
jizhihang opened this issue Dec 18, 2017 · 1 comment

Comments

@jizhihang
Copy link

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?

@alessio-greco
Copy link

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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants