繁体   English   中英

如何在 Java 中使用递归从 N 叉树中提取最大值

[英]How to extract the maximum value from a N-ary tree using recursion in Java

我正在尝试找到一种方法来跟踪我的代码中的最大值。 现在我知道我的 for 循环查看树中的每个节点并将其与最后一个最大值进行比较。 但我的问题是,当 function 调用从堆栈中弹出时,我最终只评估了第一组孩子,而我的最大值是从那些而不是整个树中选择的。 节点可以有任意数量的子节点和有效负载。

public static Integer max(TreeNode<Integer> root) {

    if (root == null) {
        return null;
    }
    int maximum = root.payload;

    if (root != null) {
        for (int i = 0; i < root.children.size(); i++) {
            if (root.children.get(i).payload > maximum) {
                maximum = root.children.get(i).payload;
            }
            max(root.children.get(i));
        }
    }
    return maximum;
}

树示例:

                    1
         4          6        7
         9               10  11  12

最大:12 我的最大:7

public class TreeNode<T> {
    public ArrayList<TreeNode<T>> children;
    public T payload;
}

你可以试试这个

    public static Integer max(TreeNode<Integer> root) {
        if (root == null) {
            return null;
        } 

        if (root.children == null) {
                return null;
        }

        int maximum = root.payload;
        for (int i = 0; i < root.children.size(); i++) {
            if (root.children.get(i).payload > maximum) {
                maximum = root.children.get(i).payload;
            }
            Integer childsMaximun = max(root.children.get(i));
            if ((childsMaximun != null) && (childsMaximun > maximum)){
              maximum = childsMaximun;
            }
        }
        return maximum;
    }
public static int maximum(TreeNode<Integer> t) {
    if (t == null) {
        return 0;
    }
    int max = t.payload;
    List<TreeNode<Integer>> children = t.children;

    if (children != null && children.size() > 0) {
        for (TreeNode<Integer> e: children) {
            // recursive call here
            int maxNext = maximum(e);
            if (maxNext > max) max = maxNext;
        }
    } 

    return max;
}

您的解决方案不是递归的。 您只是在检查rootchildren中的max ,而不是更深。

您应该遍历所有children并继续更新max 我已经使用递归完成了它,还有很多其他方法可以遍历一棵树。

您需要添加构造函数来初始化 ArrayList,试试我的 TreeNode class:

public class TreeNode<T> {
public T payload;
public List<TreeNode<T>> children;

public TreeNode(T payload) {
    this.payload = payload;
    children = new ArrayList<>();
}

public void addChildren(T... childs){
    for (T child: childs){
        TreeNode<T> node = new TreeNode<>(child);
        children.add(node);
    }
}
public void addChildren(TreeNode<T>... childs){
    for (TreeNode<T> child: childs){
        children.add(child);
    }
}

public static TreeNode<Integer> DEFAULT(){


    TreeNode<Integer> root7 = new TreeNode<>(7);
    root7.addChildren(10,11,12);

    TreeNode<Integer> root6 = new TreeNode<>(6);

    TreeNode<Integer> root4 = new TreeNode<>(4);
    root4.addChildren(9);

    TreeNode<Integer> root1 = new TreeNode<>(1);

    root1.addChildren(root7, root6, root4);

    return root1;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM