import java.io.*;
class treeNode {
int data;
treeNode left = null;
treeNode right = null;
treeNode parent = null;
treeNode (int value)
{
data = value;
}
}
class treeNodes {
treeNode root = null;
void insert (treeNode node)
{
if (root == null) // the value of root is always null
{
root = node;
return;
}
treeNode cur = null;
treeNode father = null;
while (cur != null)
{
father = cur;
if (cur.data > node.data)
cur = cur.left;
else
cur = cur.right;
}
if (father.data > node.data)
father.left = node;
else
father.right = node;
node.parent = father;
}
void walkInorder (treeNode n)
{
if (n == null)
return;
walkInorder (n.left);
System.out.print (n.data + " ");
walkInorder (n.right);
}
}
class binarySearchTree {
public static void main (String [] args) {
treeNodes obj1 = new treeNodes ( );
System.out.println ("============ Array Elements ============");
int A[] = {3, 5, 7, 9, 8, 6};
for (int i = 0; i < 6; i++)
{
treeNode node = new treeNode (A[i]);
treeNodes obj = new treeNodes ( );
obj.insert(node);
}
System.out.println ("\n============ Inorder ============");
System.out.println (obj1.root.data);
obj1.walkInorder (obj1.root);
}
}
我试图制作二叉树,当我将第一个值发送到插入函数时,它应该改变 root 的值
在我的情况下,根永远不会改变有人可以告诉我为什么以及如何解决它
对不起,我的英语不好