public void insert(int data) {
if (root == null) root = new AVLNode(data);
else {
AVLNode node = root;
while (node != null) {
if (node.data <= data) {
node = node.right;
}
else {
node = node.left;
}
}
node = new AVLNode(data);
}
}我试图创建一个二进制搜索树(非递归),然后访问并执行预序遍历,但当我打印root时,我只得到1个元素。我认为所有的插入都是有效的,但是root只有第一个插入,并且它的left和right没有引用任何东西,我想这就是为什么我只得到了1个元素。如何将root引用到节点树的顶部?下面是节点类btw
class AVLNode
{
AVLNode left, right;
int data;
/* Constructor */
public AVLNode()
{
left = null;
right = null;
data = 0;
}
/* Constructor */
public AVLNode(int n)
{
left = null;
right = null;
data = n;
}
}https://stackoverflow.com/questions/52711659
复制相似问题