你能幫我嗎?我正在製作一個節點插入的二叉樹。如何在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