Java二叉树插入方法不工作

Java二叉树插入方法不工作,java,recursion,binary-tree,binary-search-tree,nodes,Java,Recursion,Binary Tree,Binary Search Tree,Nodes,我正在为二叉树创建一个递归插入方法。此方法无法将节点添加到树中。我似乎找不出这个方法有什么问题。构造函数接受子节点和父节点的字符串标签 public void insert(String aLabel) { //if compare is positive add to right else add to left //basis case: BSTreeNode aNode = new BSTreeNode(aLabel,null); if (aNode.p

我正在为二叉树创建一个递归插入方法。此方法无法将节点添加到树中。我似乎找不出这个方法有什么问题。构造函数接受子节点和父节点的字符串标签

 public void insert(String aLabel) {

    //if compare is positive add to right else add to left
    //basis case:
    BSTreeNode aNode = new BSTreeNode(aLabel,null);
    if (aNode.parent == null) {
        aNode.parent = this;
    }
    inserts(this,aNode);
}
private void inserts(BSTreeNode aParent, BSTreeNode aNode){
    //initially the root node is the parent however a proper parent is found thorough recursion
    //left recursion:
    if(aParent.getLabel().compareTo(aNode.getLabel()) <= 0) {
        if (this.childrenLeft == null) {
            this.childrenLeft = aNode;
            aNode.parent = this;
            return;
        } else {
            childrenLeft.inserts(childrenLeft, aNode);
        }
    }
    //right recursion
    else {
        if (this.childrenRight==null) {
            this.childrenRight = aNode;
            return;
        }
        else{
            childrenRight.inserts(childrenRight,aNode);
        }

    }

}
公共空白插入(字符串阿拉贝尔){
//如果比较为正,则向右添加,否则向左添加
//基本情况:
B屏蔽阳极=新的B屏蔽(阿拉贝尔,空);
if(阳极.parent==null){
阳极.parent=这个;
}
插入件(此为阳极);
}
专用空心插入件(B屏蔽极隔离、B屏蔽极阳极){
//最初,根节点是父节点,但是通过递归可以找到合适的父节点
//左递归:

如果(aParent.getLabel().compareTo(aNode.getLabel())编辑:此答案指问题的原始版本

当您调用
insert(this.childrenLeft,阳极);
时,您仍然在同一个节点上;即
this
仍然引用旧的父节点

相反,您应该做如下操作:

childrenLeft.insert(childrenLeft, aNode);

事实上,
insert
的第一个参数是多余的,您应该重构以删除它。

我想您可能需要这样的东西

对代码进行注释,以便您了解发生了什么

// insert method takes The Node as a param and a value to store in BT
public void insert(Node node, int value) {

//Check that the value param is less than the Node (root) value,
// If so insert the data to the left of the root node. Else insert        
// the right node as it is a larger number than root
if (value < node.value) {
  if (node.left != null) {
    insert(node.left, value);
  } else {
    System.out.println("  Inserted " + value + " to left of "
        + node.value);
    node.left = new Node(value);
  }
} else if (value > node.value) {
  if (node.right != null) {
    insert(node.right, value);
  } else {
    System.out.println("  Inserted " + value + " to right of "
        + node.value);
    node.right = new Node(value);
  }
 }
}
//insert方法将节点作为参数和值存储在BT中
公共void插入(节点,int值){
//检查值param是否小于节点(根)值,
//如果是,请将数据插入根节点的左侧。否则,请插入
//右节点,因为它的数目大于根
if(值<节点值){
if(node.left!=null){
插入(node.left,值);
}否则{
System.out.println(“插入的“+值+”位于”
+节点值);
node.left=新节点(值);
}
}else if(值>节点值){
if(node.right!=null){
插入(node.right,value);
}否则{
System.out.println(“插入的“+值+”到右侧”
+节点值);
node.right=新节点(值);
}
}
}

问题出在哪里?是因为没有生成正确的BST,还是您遇到了任何异常?您的第一个if子句的条件似乎不正确。请检查。您已经检查了父项是否较小