你能帮我吗?我正在制作一个节点插入的二叉树。如何在BST规则方面将新节点插入当前节点?二叉树问题;需要帮助
例如:首先根是空的。
输入数字:50
这将显示“成功!”
插入号码:40
在50
插入数字左子树成功插入:20
在40
插入数字左子树成功插入:80
成功插入右侧子树50
你能帮我吗?预先感谢您希望您的积极响应......
这里是我的代码:
class Node
{
public int num;
public Node llink;
public Node rlink;
}
public class BinaryTreeOperations
{
//public Node llink=null;
// public Node rlink=null;
private Node temp;
private Node current;
private Node root;
public boolean isEmpty()
{
return root==null;
}
public void insertNum(int n)
{
temp=null;
current=null;
Node newNode = new Node();
newNode.num=n;
newNode.llink=null;
newNode.rlink=null;
if(isEmpty())
{
root=newNode;
System.out.println("Successfully inserted!");
}
else
{
temp=root;
while(temp!=null)
{
current = temp;
root = current;
temp=null;
}
if(n<current.num)
{
current.llink=newNode;
//current.llink=temp;
System.out.println("inserted on the left subtree " +current.num);
}
else
{
newNode.rlink=newNode;
System.out.println("inserted on the right subtree "+current.num);
}
}
}
这是一个家庭作业? – Blorgbeard 2011-01-26 01:50:17
另外:到目前为止你的代码有什么问题? – Blorgbeard 2011-01-26 01:51:05
你好,谢谢你的答复。我是一个新手,在这里我很抱歉,如果我发现错误张贴...这是作业 – jemz 2011-01-26 01:52:19