Java 向二叉树插入元素

Java 向二叉树插入元素,java,data-structures,binary-tree,Java,Data Structures,Binary Tree,这是我对二进制节点类的实现: public class BinaryNode{ int element; BinaryNode left; BinaryNode right; BinaryNode(int theElement,BinaryNode lt,BinaryNode rt){ element=theElement; left=lt; right=rt; } BinaryNode

这是我对二进制节点类的实现:

public class BinaryNode{
    int element;
    BinaryNode left;
    BinaryNode right;
    BinaryNode(int theElement,BinaryNode lt,BinaryNode rt){
        element=theElement;
        left=lt;
        right=rt;       
    }
    BinaryNode(int theElement){
        this(theElement,null,null);
    }

}
下面是我在binaryTree类中的insert方法

public class BinaryTree {
    private BinaryNode root;

    public BinaryTree(){
        root= null;
    }
    BinaryTree(int nodeValue){
        root=new BinaryNode(nodeValue);

    }
public void insert(BinaryNode node,int x){
        if(node==null){
            node=new BinaryNode(x);
        }
        else if(node.element<x){
            insert(node.left,x);
        }
        else if (node.element>x){
            insert(node.right,x);
        }
        else
            System.out.println("Duplicates not allowed");
    }  
但是在插入5之后,我如何调用insert方法来添加像10,12,78

2) 另外,当我查看一些插入到二叉树的代码时,我发现了这段代码

/** 
   Inserts the given data into the binary tree. 
   Uses a recursive helper. 
  */ 
  public void insert(int data) { 
    root = insert(root, data); 
  } 


  /** 
   Recursive insert -- given a node pointer, recur down and 
   insert the given data into the tree. Returns the new 
   node pointer (the standard way to communicate 
   a changed pointer back to the caller). 
  */ 
  private Node insert(Node node, int data) { 
    if (node==null) { 
      node = new Node(data); 
    } 
    else { 
      if (data <= node.data) { 
        node.left = insert(node.left, data); 
      } 
      else { 
        node.right = insert(node.right, data); 
      } 
    }

    return(node); // in any case, return the new pointer to the caller 
  } 
/**
将给定的数据插入到二叉树中。
使用递归帮助器。
*/ 
公共无效插入(整型数据){
根=插入(根,数据);
} 
/** 
递归插入——给定一个节点指针,向下递归并
将给定的数据插入到树中。返回新的
节点指针(通信的标准方式)
已更改的指针(返回到调用方)。
*/ 
专用节点插入(节点,int数据){
如果(node==null){
节点=新节点(数据);
} 
否则{

如果(数据在二叉树中插入元素只需要树和元素作为输入。树本身应该确定应该更新哪个节点。这是通过从根开始的递归函数实现的:这是作用于节点的helper函数。

在二叉树中插入元素应该需要只有树和元素作为输入。树本身应该决定应该更新哪个节点。这是通过从根开始的递归函数实现的:这是作用于节点的帮助函数。

第一个问题是,由于t.root是私有的,您将无法直接访问它。您或者需要一个getter

public BinaryNode getRoot() {
    return this.root;
}
或者公开

使用了helper方法,因此可以确定BinaryTree的新根。并且因为根不应该返回给调用方。但是,由于递归地将某些内容插入到二叉树中更容易,因此使用私有方法来实现这一点。 您可以使用以下方法:

public static void main(String[] args) {
    BinaryTree t = new BinaryTree(5); //Create a new tree with one item
    t.insert(12); // Assuming that you used the implementation with the helper method
    t.insert(3);  //

    t.insert(t.getRoot(),12);  // Assuming you used your implementation
    t.insert(t.getRoot(),3);   //
}

第一个问题是您无法直接访问t.root,因为它是私有的

public BinaryNode getRoot() {
    return this.root;
}
或者公开

使用了helper方法,因此可以确定BinaryTree的新根。并且因为根不应该返回给调用方。但是,由于递归地将某些内容插入到二叉树中更容易,因此使用私有方法来实现这一点。 您可以使用以下方法:

public static void main(String[] args) {
    BinaryTree t = new BinaryTree(5); //Create a new tree with one item
    t.insert(12); // Assuming that you used the implementation with the helper method
    t.insert(3);  //

    t.insert(t.getRoot(),12);  // Assuming you used your implementation
    t.insert(t.getRoot(),3);   //
}

我不必将root声明为public。问题是我没有设置根节点。当我喜欢
t.root=t.insert(t.root,5);t.insert(t.root,6);t.insert(t.root,4);t.insert(t.root,60);t.insert(t.root,25)