c# - How can I generate a tree with duplicate values -
i need generate tree in node can have particular children, number of node cannot repeat in same branch.
1:5 2:5 3:5 4:5 5:1,2,3,4 ':' means node n can have children x,y,z,... root 0, has possible children, not repeat in tree. 0 / / | \ \ 1 2 3 4 5 | | | | / / \ \ 5 5 5 5 1 2 3 4 / | \ / | \ / | \ / | \ 2 3 4 1 3 4 1 2 4 1 2 3
you need loop 1 5 , have if inside loop see if child same node on (the parent), if is, use continue skip it.
if can't repeat in whole branch, have check grand-parent node too, in fact, ancestor nodes. while loop inside main loop. loop until ancestor node checking not 0. inside while loop, need set ancestor ancestor's parent.
this "recurse" (call itself) making each new level down until numbers in particular branch used up.
pseudo-code:
void maketree() { addnode(0, null); } function node addnode(nodenumber, parent) { nodecollection nodes; if (parent != null) nodes = parent.nodes; else nodes = tree.nodes; node addnode = nodes.add(nodenumber); (int = 1; <= 5; i++) { bool alreadyexists = false; node ancestor = addnode; while (ancestor != null) { if (ancestor == childnode) { alreadyexists = true; break; } ancestor = ancestor.parent; } if (!alreadyexists) addnode(childnode, addnode); } }
Comments
Post a Comment