¿Agregar padres a los nodos en el árbol?
Frecuentes
Visto 237 veces
0
I have a tree structure and I want to be able to go through and add the parent node to a data parent variable that each child node has.
The structure of the tree is:
Class Tree {
Node root;
}
Class Node {
String data;
Node parent;
List<Node> children;
}
¿alguna sugerencia?
EDIT to be more specific
public void addParent() {
for (Node child : curNode.children) {
child.parent = curNode;
curNode = child;
findDFS(value);
}
return null;
}
with that code and the given tree:
A
/ \
D E
/ \
B S
E's parent is D when it should be A, why is that?
1 Respuestas
0
E's parent is D when it should be A, why is that?
In your loop you're doing:
for (Node child : curNode.children) {
child.parent = curNode;
curNode = child; // <-- problem is here!!!
findDFS(value);
}
which means: in the first loop you're assigning the child to the current node and continue looping over the next child with the first child as a "parent" and then again...
Respondido el 08 de diciembre de 14 a las 03:12
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java tree or haz tu propia pregunta.
As it currently stands, this question is very vague. Consider posting some examples of what you have tried and what problems you are facing. - Taylor Hx
I made it more specific - user2998228
You mean E's parent is A? Also, can the parent only have 2 child or more than 2 child since you made a list for children? - chuthan20
By "specific", I think Daemon also meant more along the lines of: what is it that you're asking? Are you encountering an error? What is the expected behaviour, and what is the observed or actual behaviour? - Paul Richter
I meant it E's parent is getting set to D when it should be A. - user2998228