如何在Java中使用递归从N元树中提取最大值
我试图找到一种方法来跟踪代码中的最大值。现在我知道我的for循环会查看树中的每个节点,并将其与最后一个max进行比较。但我的问题是,当函数调用从堆栈中弹出时,我只会计算第一组子节点,而我的max是从这些节点中选择的,而不是从整个树中选择的。节点可以有任意数量的子节点和有效负载如何在Java中使用递归从N元树中提取最大值,java,for-loop,recursion,tree,max,Java,For Loop,Recursion,Tree,Max,我试图找到一种方法来跟踪代码中的最大值。现在我知道我的for循环会查看树中的每个节点,并将其与最后一个max进行比较。但我的问题是,当函数调用从堆栈中弹出时,我只会计算第一组子节点,而我的max是从这些节点中选择的,而不是从整个树中选择的。节点可以有任意数量的子节点和有效负载 public static Integer max(TreeNode<Integer> root) { if (root == null) { return null; }
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;
}
最高:12
我的最大值:7
公共类树节点{
公共ArrayList儿童;
公共T载荷;
}
您可以试试这个
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;
}
公共静态整数最大值(TreeNode根){
if(root==null){
返回null;
}
if(root.children==null){
返回null;
}
int最大值=root.payload;
for(int i=0;i最大值){
最大值=root.children.get(i).payload;
}
整数childsMaximun=max(root.children.get(i));
如果((childsMaximun!=null)和&(childsMaximun>最大值)){
最大值=儿童最大值;
}
}
返回最大值;
}
公共静态整数最大值(TreeNode t){
如果(t==null){
返回0;
}
int max=t.有效载荷;
列出儿童=t儿童;
if(children!=null&&children.size()>0){
适用于(TreeNode e:儿童){
//在这里递归调用
int maxNext=最大值(e);
如果(maxNext>max)max=maxNext;
}
}
返回最大值;
}
您的解决方案不是递归的。您只需在根目录的子目录中检查最大值
,而不是更深
您应该遍历所有子项
,并不断更新max
。我使用递归完成了这项工作,可以使用许多其他方法遍历树。您需要添加构造函数来初始化ArrayList,请尝试我的TreeNode类:
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;
}
}
公共类树节点{
公共T载荷;
公开儿童名单;
公共树节点(T有效载荷){
这个有效载荷=有效载荷;
children=newarraylist();
}
公共儿童(T…儿童){
对于(T儿童:儿童){
树节点=新树节点(子节点);
添加(节点);
}
}
公共儿童(TreeNode…儿童){
for(TreeNode儿童:儿童){
添加(child);
}
}
公共静态树节点默认值(){
TreeNode root7=新的TreeNode(7);
7.添加儿童(10,11,12);
TreeNode root6=新的TreeNode(6);
TreeNode root4=新的TreeNode(4);
4.添加儿童(9);
TreeNode root1=新的TreeNode(1);
root1.addChildren(root7,root6,root4);
返回根1;
}
}
在max方法之外设置maximun静态变量,或将maximun参数添加到max方法。我的约束不允许在max方法之外使用静态变量。有没有不改变最大参数的方法?给我看看你的TreeNode类,plzI刚刚在上面添加了TreeNode类。你应该检查树节点的输入值或尝试打印它,我尝试过了,答案都很好。当我尝试这种方法时,我在for循环线上得到了一个空指针异常。在“Integer childMaximum=max(root.children.get(i));”行上还有三个。这是递归调用的问题吗?或者for循环?只需要验证子数组不为null。我更新了帖子@德克萨斯州。您还应该实现一个更好的树节点,它有更多的验证。我在if语句的开头和int maxNext行收到一个null指针异常。我的测试要求我在root为null时返回null。。。但该编辑确实修复了我得到的空指针异常。谢谢你可以根据自己的需要修改代码。我只是不清楚为什么我写if(root==null)返回-1代码>我不再获得空指针异常。但是当我写if(root==null)时,返回null代码>在同一行代码中,我得到一个空指针异常?这是因为递归调用。如果返回null,则该值将在其上一次调用中使用,这将为您提供一个NPE。理想情况下,为了获得最大值,应该返回-1(假设树的值只有正值)。
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;
}
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;
}
}