2011-01-26 108 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); 
      } 
     } 
    } 
} 

让我知道如果您有任何其他问题。