2011-01-26 111 views
-1

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

這是一個家庭作業? – Blorgbeard 2011-01-26 01:50:17

+0

另外:到目前爲止你的代碼有什麼問題? – Blorgbeard 2011-01-26 01:51:05

+0

你好,謝謝你的答覆。我是一個新手,在這裏我很抱歉,如果我發現錯誤張貼...這是作業 – jemz 2011-01-26 01:52:19

回答

0
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); 
} 

在一個分支您要指派給current.llink,在另一個分支您要指派給newNode.rlink。哎呀。 :)

2

你的while循環似乎是錯誤的。你真正想要做的是從根開始並遍歷樹,直到到達將作爲新節點的父節點的節點。在下面,你沒有做任何遍歷或檢查來找到新節點應該去的地方。這是你真正需要做的。

while(temp!=null) { 
    current = temp; 
    root = current; 
    temp=null; 
} 

應該是這樣的:

while(parent not found) { 
    if (new node is smaller than current) { 
     if (current has left child) { 
      assign left child to current and loop 
     } else { 
      make current the parent of the new node 
     } 
    } else { 
     .... 
    } 
} 
0

你的方法添加到二叉搜索樹似乎不正確。

您需要閱讀二叉搜索樹來了解如何維護樹的結構。

下面是一些代碼,顯示如何添加到二叉搜索樹,但我定義一棵樹,如果它不包含數據。

public boolean isEmpty() { 
    return data == null; 
} 

其餘的代碼很自我解釋,應該幫助你弄清楚如何添加到樹來維護順序。

public boolean add(Comparable target) { 
    if(isEmpty()) { 
     setData(target); 
     this.setLeftTree(new BinarySearchTree()); 
     this.setRightTree(new BinarySearchTree()); 
     return true; 
    } else { 
     int comparison = getData().compareTo(target); 
     if(comparison == 0) 
      return false; 
     else { 
      if(comparison < 0) { 
       return getRightTree().add(target); 
      } else { 
       return getLeftTree().add(target); 
      } 
     } 
    } 
} 

讓我知道如果您有任何其他問題。