将节点插入二叉搜索树

时间:2013-03-12 18:00:57

标签: java binary-search-tree

我已经在二进制搜索树实现中插入了几天,我知道通过使用'insert()'来填充我的根目录(我可以看到这一点)我使用Eclipse调试。为什么我的其他节点不会被添加到树中?

这是我的BST课程:

package binarySearchTree;

public class BinarySearchTree<T extends Comparable<T>> {

@SuppressWarnings("hiding")
private class BinarySearchTreeNode<T>{
    public BinarySearchTreeNode left, right;
    private T data; //LINE 8

    private BinarySearchTreeNode (T data,BinarySearchTreeNode left, BinarySearchTreeNode right ) {
        this.left = left;
        this.right = right;
        this.data = data;
    }
}
private BinarySearchTreeNode<T> root;

@SuppressWarnings("unused")
private T search(T target, BinarySearchTreeNode<T> ptr) {
    //find target in subtree A ptr
    if (root == null || ptr == null) {
        return root; //target is not in tree
    }
    int compare = target.compareTo(ptr.data); //compareTo(ptr.data);
    if (compare == 0) {
        return ptr.data; //target is found
    }
    if (compare < 0) {
        return search(target, ptr.left);
    }
    if (compare > 0) {
        return search(target, ptr.right);
    }
    return target;
}
public T search(T target) {
    return search(target);
}
public boolean isEmpty() {
    return root == null;
} 
/* To insert a data into a BST, 1st search for the data, 
 * if the data is found = duplicate data error
 * if the data is NOT found = a null pointer
 * Make this null pointer point to a NewNode holding data
 * new values go into the BST as leaves
 * Using public boolean insert (T node) & 
 * private boolean insert (T Node, BSTNode<T> ptr) as a recursive method
 */
@SuppressWarnings("unchecked")
private boolean insert(T value, BinarySearchTreeNode<T> ptr) {
    //T data = null;
    //insert data in a child of ptr, return false if duplicate is found
    //Precondition: ptr must not be null
    int compare = value.compareTo(ptr.data); //LINE 55
    if (compare == 0) {
        return false;
    }
    if (compare < 0) {
        if (ptr.left == null) {
            //found insertion point
            BinarySearchTreeNode<T> node = new BinarySearchTreeNode<>(value, null, null);
            ptr.left.data = node; //insert data in new node
            return true;
        } 
    } else {
        return insert(value, ptr.left); //LINE 67
    }
    if (compare > 0) {
        if (ptr.right == null) {
            BinarySearchTreeNode<T> node = new BinarySearchTreeNode<>(value, null, null);
            ptr.right.data = node;
            return true;
        } else {
            return insert(value, ptr.right);                    
        }
    }
    return false;
}
public boolean insert(T value) {     
    if (isEmpty()) {              
        root = new BinarySearchTreeNode<T>(value, null, null);  
        return true;  
    }
    return insert(value, root); // LINE 85  
} 
}

这是我的Main(),最后我想在控制台中打印我的BST的值,但首先我知道它们需要添加到树中:

package binarySearchTree;

public class Main {

public static void main(String[] args) {


    BinarySearchTree<String> bstStrings = new BinarySearchTree<String>();

    String s = "Hello";
    String s1 = "World";
    //String s2 = "This Morning";

    bstStrings.insert(s);
    bstStrings.insert(s1); //LINE 15
    //bstStrings.insert(s2);

    while (true){
        if (!bstStrings.isEmpty()){
            System.out.println(bstStrings + " ");
        }
        System.out.println();
        System.out.println("You should have values above this line!");break;
    }   
}
}

2 个答案:

答案 0 :(得分:2)

您的root应该是BinarySearchTree<T>而不是T 因此,您不会将值存储在根的子树中 替换它:
return insert((T) value, node);

return insert((T) value, root);

代码中的

替换如下:

public boolean insert(T value) {     
    if (isEmpty()) {              
        root = new BinarySearchTreeNode((T) value, null, null);  
        return true;  
    }
    return insert((T) value, root); // start recursion  
}    

否则,您没有树,节点没有相互链接

<强>更新
您获得NPE是因为您在insert的第一个比较中传入了null root的左子项。
您不应该返回boolean而是BinarySearchTreeNode 你的方法应该是:

@SuppressWarnings("unchecked")
private BinarySearchTreeNode<T> insert(T value, BinarySearchTreeNode<T> ptr) {  
   if(ptr == null){  
        ptr = new BinarySearchTreeNode<T>(value,null,null);  
        return ptr;  
    }  
    //your code next but return the `ptr`  
}  

然后在插入中你应该这样做:

public void insert(T value) {     

    root = insert(value, root); 
}

答案 1 :(得分:0)

首次插入后,您可以创建新节点,但不对它们执行任何操作。