Ok, you faggots. Since many of you seem to dislike recursion tell me how you would extract information out of the tree in pic related.
You only get access to the top node. Each node can have any number of subnodes. Create a list that contains all bottom nodes that don't have subnodes of their own.
These are my solutions (pseudo code and not tested, don't bitch). They don't return exactly the same lists, though, as the order of the elements will be different.
I'm a recursion fag so loop code might be shit. I'm also just a hobby programmer so both might be shit.
Recursion
List getBottomNodes(Node parent_node) {
List bottom_nodes;
addBottomNodesToList(parent_node, bottom_nodes);
return bottom_nodes;
}
void addBottomNodesToList(Node node, List bottom_nodes) {
if (node.getChildNodes().size() > 0) {
for (Node child_node: node.getChildNodes()) {
addBottomNodesToList(child_node, bottom_nodes);
}
} else {
bottom_nodes.add(node);
}
}
Loop
List getBottomNodes(Node parent_node) {
List bottom_nodes, List parent_nodes;
parent_nodes.add(parent_node);
while (parent_nodes.size() != 0) {
for(Node child_node: parent_nodes.get(0).getChildNodes()) {
if(child_node.getChildNodes() > 0) {
parent_nodes.add(child_node);
} else {
bottom_nodes.add(child_node);
}
}
parent_nodes.remove(0);
}
}