将节点添加到BinarySearchTree

时间:2017-12-08 17:59:10

标签: java

我遇到问题并且无法弄清楚为什么我的节点没有被添加到二叉树。我觉得它设置节点,而不是在下一个循环中重置它。

ECustomer对象

T是一个LinkedList整数列表。

我不确定是否还必须实现Interface父级。 我可以在控制台中看到它始终将节点设置为右侧。 我的代码中的错误在哪里?

更新: 如果我在add方法中替换

    BinaryNode<E, T> parent =  new BinaryNode<>() ;

Parent<E, T> parent = this;

然后它似乎进入while循环并进行比较,我可以在控制台中看到println语句。但后来我收到了一个错误:

    Exception in thread "main" java.lang.ClassCastException: 
    teama.tree.BinaryNode cannot be cast to teama.tree.Parent
    at teama.tree.BinarySearchTree.add(BinarySearchTree.java:32)
    at teama.tree.TestBtree.main(TestBtree.java:44)

并突出显示此代码并将其转换为BinaryNode:

    parent = (Parent<E, T>) node;

如果我从中删除演员

    parent =  node;

然后突出显示带有红色下划线的node并提供它以退回。 为什么会发生这种情况以及如何解决?

以下是代码:

public class BinaryNode<E, T> {

    /** The item associated with this node. */
    private E item;
    private T item2;

    /** The node at the root of the left subtree. */
    private BinaryNode<E, T> left;

    /** The node at the root of the right subtree. */
    private BinaryNode<E, T> right;

    public BinaryNode() {

    }

    /** Put item in a leaf node. */
    public BinaryNode(E item, T item2) {
        this.item = item;
        this.item2 = item2;

    public void setChild(int direction, BinaryNode<E, T> child) {
        if (direction < 0) {
            System.out.println("set Left");
            left = child;
        } else {
            System.out.println("set Right");
            right = child;
        }
    }

此处BinaryTree班级

public class BinarySearchTree <E extends Comparable<E>, T> 
    implements Parent<E, T>, Set<E> {
 {

    /** Root node. */
    private BinaryNode<E, T> root;

    /** A BinarySearchTree is initially empty. */
    public BinarySearchTree() {
       root = null;
    }

在这里,我不得不用Parent<E,T>替换BinaryNode<E, T>,因为下面的节点是由Eclipse提供的,不匹配类型。

public void add(E target, T list) {
    // Here I replaced Parent<E> parent = this;  
    //  with BinaryNode<E, T> parent =  new BinaryNode<>()
    BinaryNode<E, T> parent =  new BinaryNode<>() ;
    BinaryNode<E, T> node = root;
    int comparison = 0;
    while (node != null) {
        System.out.println("While loop:");
        comparison = target.compareTo(node.getItem());
        System.out.println("Comparison=" + comparison);
        if (comparison < 0) {     // Go left
            System.out.println("Go Left");
            parent = node;
            node = node.getLeft();
        } else if (comparison == 0) {
            // It's already here
            return;
        } else {
            // Go right
            System.out.println("Go Right");
            parent = node;
            node = node.getRight();
        }
    }

    System.out.println("from add method: " + target +" list=" + list);
    // BinaryNode<E, T> newNode = new BinaryNode<E, T>(target, list);
    // newNode.setChild(comparison, newNode);
    parent.setChild(comparison, new BinaryNode<E, T>(target, list));
}

Parent界面:

public interface Parent<E, T> {

    /**
    * Return the left child if direction < 0, or the right child
    * otherwise.
    */
    public BinaryNode<E, T> getChild(int direction);

    /**
    * Replace the specified child of this parent with the new child.
    * If direction < 0, replace the left child.  Otherwise, replace
    * the right child.
    */
    public void setChild(int direction, BinaryNode<E, T> child);

1 个答案:

答案 0 :(得分:0)

我想我已经解决了。在我的BinaryNode类中,我没有实现接口parent,这就是为什么它给了我错配类型错误。我将implements Parent<E>添加到public class BinaryNode<E, T>后,现在工作正常。