通用BST无法让构造函数工作

时间:2018-03-03 05:54:47

标签: binary-search-tree

我从教师那里得到了一些代码,我需要实现几个功能。我添加了insert方法,但我无法弄清楚构造函数的查找方式和内容,我不明白为什么构造树的这个调用不起作用。在java中不是很强,但理解BST的逻辑

public class BinaryTreeDriver {


public static void main(String[] args){


BinaryTree<Integer> numbers = new BinaryTree<>();

我收到编译错误无法推断类型参数BinaryTree&lt;&gt;

复制下面的其余代码

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

    private BinaryTreeNode<T> root; // the root of the tree
    private BinaryTreeNode<T> cursor; // the current node


    /**
     * Constructor for initializing a tree with node
     * being set as the root of the tree.
     * @param node
     */

    public BinaryTree(BinaryTreeNode<T> node) {

        root = node;
    }

    /**
     * Moves the cursor to the root.
     */

    public void toRoot() {

        cursor = root;
    }

    /**
     * Returns the cursor node.
     * @return cursor
     */

    public BinaryTreeNode<T> getCursor() {

        return cursor;
    }

    /**
     * Sets the root to the provided node.
     * ONLY USE IN THE DELETE METHOD
     * @param node
     */

    public void setRoot(BinaryTreeNode<T> node) {

        root = node;
    }

    /**
     * Checks if the tree node has a left child node
     * @return true if left child exists else false
     */

    public boolean hasLeftChild() {

        return cursor.getLeft() != null;
    }

    /**
     * Checks if the tree node has a right child node
     * @return true if right child exists else false
     */
    public boolean hasRightChild() {

        return cursor.getRight() != null;
    }

    /**
     * Move the cursor to the left child
     */

    public void toLeftChild() {

        cursor = cursor.getLeft();
    }
    /**
     * Move the cursor to the right child
     */

    public void toRightChild() {

        cursor = cursor.getRight();
    }

    /**
     * @return height of the tree
     */
    public int height() {

        if (root != null) {
            return root.height();

        } else {

            return 0;
        }
    }

    **/**
     * Tree-Insert
     */

    public boolean insert(T elem)
    {
        return insert(root, elem);
    }

    public boolean insert(BinaryTreeNode start, T elem)
    {

        if (start == null)
        {

            root = new BinaryTreeNode<T>(elem, null, null);
            return true;
        }

        int comparison = start.getData().compareTo(elem);

        if (comparison > 0)
        {

            if (start.getLeft() == null)
            {

                start.setLeft(new BinaryTreeNode(elem, null, null));
                return true;
            }

            return insert(start.getLeft(), elem);
        }

        else if (comparison < 0)
        {

            if (start.getRight() == null)
            {

                start.setRight(new BinaryTreeNode(elem, null, null));
                return true;
            }

            return insert(start.getRight(), elem);
        }

        else
        {

            return false;
        }**
    }
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */

    public String toString() {

        if (root != null) {

            return root.toStringPreOrder(".");

        } else {
            return "";
        }
    }
}


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

    private BinaryTreeNode<T> left; // the left child
    private BinaryTreeNode<T> right; // the right child
    private T data; // the data in this node

    public BinaryTreeNode() {

        this(null, null, null);
    }

    public BinaryTreeNode(T theData) {

        this(theData, null, null);
    }

    public BinaryTreeNode(T theData, BinaryTreeNode<T> leftChild,
            BinaryTreeNode<T> rightChild) {

        data = theData;
        left = leftChild;
        right = rightChild;
    }

    public T getData() {

        return data;
    }

    public BinaryTreeNode<T> getLeft() {

        return left;
    }

    public BinaryTreeNode<T> getRight() {

        return right;
    }

    public void setLeft(BinaryTreeNode<T> newLeft) {

        left = newLeft;
    }

    public void setRight(BinaryTreeNode<T> newRight) {

        right = newRight;
    }

    public void setData(T newData) {

        data = newData;
    }

    public void preOrder() {

        System.out.println(data);

        if (left != null) {
            left.preOrder();
        }

        if (right != null) {
            right.preOrder();
        }
    }

    public int height() {

        int leftHeight = 0; // Height of the left subtree
        int rightHeight = 0; // Height of the right subtree
        int height = 0; // The height of this subtree

        // If we have a left subtree, determine its height

        if (left != null) {

            leftHeight = left.height();
        }

        // If we have a right subtree, determine its height

        if (right != null) {

            rightHeight = right.height();
        }

        // The height of the tree rooted at this node is one more than the
        // height of the 'taller' of its children.

        if (leftHeight > rightHeight) {

            height = 1 + leftHeight;

        } else {

            height = 1 + rightHeight;
        }

        // Return the answer


        return height;
    }

    /**
     * @param pathString
     * @return the tree nodes in pre-order traversal
     */

    public String toStringPreOrder(String pathString) {

        String treeString = pathString + " : " + data + "\n";

        if (left != null) {
            treeString += left.toStringPreOrder(pathString + "L");
        }

        if (right != null) {
            treeString += right.toStringPreOrder(pathString + "R");
        }

        return treeString;
    }

}

0 个答案:

没有答案