开发者

How to calculate leaf nodes in a nested data structure in Java?

I have a structure like this of what we'll call Box objects.

Box--+---Box----Box
     |
     +---Box-+--Box
             |
             +--Box
             |
             +--Box

I'm trying to ask the top box object for a list of the leaf node Boxes, which is the 3 box objects in this case.

The box object has a list of its children in an instance variable of type Vector called children.

The number of children can be unlimited.

I've been trying to write a single recursive method t开发者_运维百科o do this, but without success.


One way to do this would be a recursive traversal of the structure. The idea is as follows:

  1. There are no leaf nodes in the empty tree.
  2. In a tree with root r with no children, then r is the only leaf.
  3. In a tree with root r, if r has children, then the leaves of the tree are the leaves of those children.

You could write a recursive traversal with this sort of pseudocode:

void findChildren (Box current, List<Box> found) {
    /* Case 1. */
    if (current == null) return;

    /* Case 2. */
    if (current.children.isEmpty()) {
        found.add(current);
        return;
    }

    /* Case 3. */
    for (Box child: current.children)
        findChildren(child, found);
}

Hope this helps!


it has been awhile since I've done Java, so I'm sure this code has plenty of syntax errors, and I hope no one marks me down for it; just trying to give you some algorithm ideas. Hopefully it helps:

vector<Box> getLeaves(Box root)
{
    vector<Box> tempList;    //vector to hold nodes to check
    vector<Box> tempList2;   //vector to hold nodes' children
    vector<Box> leafList;
    bool goflag = true;

    tempList.add(root);

    while(goflag){
        for(int i = 0; i < tempList.size; i++){
            if(tempList[i].children.isEmpty()){
                leafList.add(tempList[i]);
            }
            else{
                //add all children to tempList2
                for(int c = 0; c < tempList[i].children.size; c++){
                    tempList2.add(tempList[i].children[c])
            }
        }
        if(tempList2.isEmpty()) //no more childs
            goflag = false;
        else
            tempList = tempList2;
        tempList2.clear();
    }
    return leafList;
}

It goes through all the nodes, adding children to the next list to check, and adding leaves to a list to be returned.


There are several ways to write such a function. Here's one approach to work through.

  • Define a helper function that takes a node and a mutable queue holding nodes.
  • In that helper function, check if the supplied node's children are empty. If so, add that node to the queue, and return.
  • If instead the supplied node has any children, call the helper function once for each of the children, passing the child and the same queue reference through.
  • At the top level, create an empty queue, and call the helper function, passing in the root node and the queue.
  • When the helper function returns, the queue contains all the leaves in the order they were discovered.

A different approach uses the same depth-first traversal, but the function would return the list of leaves it discovered. These lists would need to be combined for each set of siblings explored, working back up the tree as each function call returns.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜