如何在Java中计算嵌套数据结构中的叶子节点?

发布于 2024-10-15 23:52:05 字数 324 浏览 7 评论 0原文

我有一个类似这样的结构,我们称之为 Box 对象。

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

我试图向顶部框对象询问叶节点框的列表,在本例中是 3 个框对象。

盒子对象在名为“children”的 Vector 类型实例变量中具有其子对象列表。

孩子的数量可以是无限的。

我一直在尝试编写一个递归方法来执行此操作,但没有成功。

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 to do this, but without success.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

百善笑为先 2024-10-22 23:52:05

实现此目的的一种方法是递归遍历结构。思路如下:

  1. 空树中没有叶子节点。
  2. 在一棵有根 r 且没有子节点的树中,r 是唯一的叶子。
  3. 在以 r 为根的树中,如果 r 有子节点,则树的叶子就是这些子节点的叶子。

您可以使用这种伪代码编写递归遍历:

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);
}

希望这有帮助!

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!

甚是思念 2024-10-22 23:52:05

我已经有一段时间没有接触 Java 了,所以我确信这段代码有很多语法错误,我希望没有人因此而对我进行批评;只是想给你一些算法想法。希望它有帮助:

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 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.

待"谢繁草 2024-10-22 23:52:05

有多种方法可以编写这样的函数。这是一种解决方法。

  • 定义一个辅助函数,它接受一个节点和一个保存节点的可变队列。
  • 在该辅助函数中,检查提供的节点的子节点是否为空。如果是,则将该节点添加到队列中,然后返回。
  • 相反,如果提供的节点有任何子节点,则为每个子节点调用一次辅助函数,并传递子节点和相同的队列引用。
  • 在顶层,创建一个空队列,并调用辅助函数,传入根节点和队列。
  • 当辅助函数返回时,队列将按照发现的顺序包含所有叶子。

另一种方法使用相同的深度优先遍历,但该函数将返回它发现的叶子列表。这些列表需要针对所探索的每组兄弟姐妹进行组合,并在每个函数调用返回时备份树。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文