所以这是Node类:
public class Node
{
private int _info;
private Node _left;
private Node _right;
public Node()
{
//this._info = Integer.MIN_VALUE;
this._left = null;
this._right = null;
}
public int getInfo()
{
return _info;
}
public void setInfo(int _info)
{
this._info = _info;
}
public Node getLeft()
{
return _left;
}
public void setLeft(Node _left)
{
this._left = _left;
}
public Node getRight()
{
return _right;
}
public void setRight(Node _right)
{
this._right = _right;
}
}
我如何创建树:
public class BalancedBinaryTree
{
private ArrayList<Integer> _numbers;
private Node _root;
public BalancedBinaryTree(ArrayList<Integer> numbers)
{
this._numbers = new ArrayList<>();
this._numbers.addAll(numbers);
Collections.sort(this._numbers);
this._root = new Node();
this.create(this._root, 0, this._numbers.size());
}
private void create(Node tree, int i, int j)
{
if (i < j)
{
int m = i + (j - i) / 2;
tree.setInfo(this._numbers.get(m));
tree.setLeft(new Node());
create(tree.getLeft(), i, m);
tree.setRight(new Node());
create(tree.getRight(), m + 1, j);
}
}
此方法计算深度:
public static int getDepth(Node node)
{
if (node == null)
{
return 0;
}
else
{
int max = 0;
if (getDepth(node.getLeft()) > getDepth(node.getRight()))
{
max = getDepth(node.getLeft());
}
else
{
max = getDepth(node.getRight());
}
return max + 1;
}
}
这两个组合应按照其级别打印树:
public static void printLevel(Node node, int levelToDisplay, int currentLevel)
{
if (node != null)
{
printLevel(node.getLeft(), levelToDisplay, currentLevel);
if (currentLevel == levelToDisplay)
{
System.out.print(node.getInfo() + " ");
}
currentLevel++;
printLevel(node.getRight(), levelToDisplay, currentLevel);
}
}
public static void printLevels(Node node)
{
for (int i = 0; i < getDepth(node); i++)
{
System.out.println("Level :" + i);
printLevel(node, i, 0);
System.out.println();
}
}
在测试课中我有:
testNumbers.add(15);
testNumbers.add(20);
testNumbers.add(25);
testNumbers.add(30);
testNumbers.add(35);
testNumbers.add(40);
testNumbers.add(45);
BalancedBinaryTree tree = new BalancedBinaryTree(testNumbers);
BalancedBinaryTree.printLevels(tree.getRoot());
我得到了这个输出:
Level :0
0 15 20 30
Level :1
0 0 25 0 35 40
Level :2
0 0 0 45
Level :3
0
我应该
Level :0
30
Level :1
20 40
Level :2
15 25 35 45
getDepth
方法有什么问题,因为它似乎返回4个级别而不是3级?我很确定我解决了这些问题,但我需要解释以下内容:
这是修改后的printlevel
方法:
public static void printLevel(Node node, int levelToDisplay, int currentLevel)
{
if (node.getLeft() != null && node.getRight() != null)
{
printLevel(node.getLeft(), levelToDisplay, currentLevel+1);
if (currentLevel == levelToDisplay)
{
System.out.print(node.getInfo() + " ");
}
printLevel(node.getRight(), levelToDisplay, currentLevel+1);
}
}
正如你所看到的,我现在测试当前节点是否有子节点,而不是检查当前节点是否存在,这就是为什么这些零值出现的原因,因为遍历到达了右侧和左侧子节点上没有分配信息的叶子。 / p>
我想要理解的是增加currentLevel
然后将其传递给printLevel
的调用并简单地将currentLevel+1
传递给调用之间的区别。不应该是一回事吗?
getDepth
函数:
public static int getDepth(Node node)
{
if (node.getLeft() == null && node.getRight() == null)
{
return 0;
}
else
{
int max = 0;
if (getDepth(node.getLeft()) > getDepth(node.getRight()))
{
max = getDepth(node.getLeft());
}
else
{
max = getDepth(node.getRight());
}
return 1 + max;
}
}
同样的事情:遍历到达了叶子并再次调用它的子节点,从而再次返回一个额外的级别,解决方案是测试当前节点是否有子节点,而不是检查当前节点是否退出。
答案 0 :(得分:2)
getDepth方法有什么问题,因为它似乎返回4级而不是3级?
从您的打印方法看,您将从0到n的等级编号(树的根部为0)。但是,你的getDepth方法永远不会返回0。
两件事:if (node != null)
这项检查似乎没有多大意义。 Null似乎不是允许的输入(因为根是在构造树时构造的)。如果是这种情况(并且您确实想检查它),则异常可能更合适。
主要问题似乎是:return max + 1
;
所以返回的最小值是0 + 1,即1。
作为一个小旁注:我会保存getDepth的两个递归调用的值,它会大大提高性能。 此外,如果你使用短变量名,如i,m或j(以非循环索引的方式),记录它们的含义会很有帮助。
并保留你的第一个问题:
tree.setLeft(new Node());
到目前为止,这个节点的价值是多少?如果反复呼叫中的i < j
条码不能通过,会发生什么?如果您可以回答这些问题,您应该能够自己修复代码。