【问题标题】:BinarySearchTree not adding elements to treeBinarySearchTree 不向树添加元素
【发布时间】:2018-06-05 18:42:51
【问题描述】:

如果我放了为什么会这样

tree.addElement(10, tree.root);

它可以工作,但如果我再做一次

tree.addElement(20, tree.root);

它不工作?我只想向树中添加元素。我的方法有什么问题?编译器给我的错误很简单:

LinkedBinarySearchTree.addElement(LinkedBinarySearchTree.java:68)

public void addElement(T element, BinaryTreeNode node) 
{
    if (!(element instanceof Comparable))
        throw new NonComparableElementException("LinkedBinarySearchTree");

    Comparable<T> comparableElement = (Comparable<T>)element;

    if (isEmpty())
        root = new BinaryTreeNode<T>(element);
    else 
    {
        if (comparableElement.compareTo(root.getElement()) < 0)
        {
            if (root.getLeft() == null) 
                this.getRootNode().setLeft(new BinaryTreeNode<T>(element));
            else
                addElement(element, root.getLeft());
        }
        else
        {
            if (root.getRight() == null) 
                this.getRootNode().setRight(new BinaryTreeNode<T>(element));
            else
                addElement(element, root.getRight());
        }
    }
    modCount++;

}

下面是剩下的代码:

 public class BinaryTreeNode<T>
{
protected T element;
protected BinaryTreeNode<T> left, right;

/**
 * Creates a new tree node with the specified data.
 *
 * @param obj the element that will become a part of the new tree node
 */
public BinaryTreeNode(T obj) 
{
    element = obj;
    left = null;
    right = null;
}

/**
 * Creates a new tree node with the specified data.
 *
 * @param obj the element that will become a part of the new tree node
 * @param left the tree that will be the left subtree of this node
 * @param right the tree that will be the right subtree of this node
 */
public BinaryTreeNode(T obj, LinkedBinaryTree<T> left, LinkedBinaryTree<T> right) 
{
    element = obj;
    if (left == null)
        this.left = null;
    else
        this.left = left.getRootNode();

    if (right == null)
        this.right = null;
    else
        this.right = right.getRootNode();
}

/**
 * Returns the number of non-null children of this node.
 *
 * @return the integer number of non-null children of this node 
 */
public int numChildren() 
{
    int children = 0;

    if (left != null)
        children = 1 + left.numChildren();

    if (right != null)
        children = children + 1 + right.numChildren();

    return children;
}

/**
 * Return true if this node is a leaf and false otherwise.
 *
 * @return true if this node is a leaf and false otherwise
 */
public boolean isLeaf() 
{
    return (numChildren() == 0);
}

/**
 * Return the element at this node.
 *
 * @return the element stored at this node
 */
public T getElement() 
{
    return element;
}

/**
 * Return the right child of this node.
 *
 * @return the right child of this node
 */
public BinaryTreeNode<T> getRight() 
{
    return right;
}

/**
 * Sets the right child of this node.
 *
 * @param node the right child of this node
 */
public void setRight(BinaryTreeNode<T> node) 
{
    right = node;
}

/**
 * Return the left child of this node.
 *
 * @return the left child of the node
 */
public BinaryTreeNode<T> getLeft() 
{
    return left;
}

/**
 * Sets the left child of this node.
 *
 * @param node the left child of this node
 */
public void setLeft(BinaryTreeNode<T> node) 
{
    left = node;
}

}

* Creates an empty binary search tree.
 */
public LinkedBinarySearchTree() 
{
    super();
}

/**
 * Creates a binary search with the specified element as its root.
 *
 * @param element the element that will be the root of the new binary
 *        search tree
 */
public LinkedBinarySearchTree(T element) 
{
    super(element);

    if (!(element instanceof Comparable))
        throw new NonComparableElementException("LinkedBinarySearchTree");
}

/**
 * Adds the specified object to the binary search tree in the
 * appropriate position according to its natural order.  Note that
 * equal elements are added to the right.
 *
 * @param element the element to be added to the binary search tree
 * @return 
 */
public void addElement(T element, BinaryTreeNode node) 
{
    if (!(element instanceof Comparable))
        throw new NonComparableElementException("LinkedBinarySearchTree");

    Comparable<T> comparableElement = (Comparable<T>)element;

    if (isEmpty())
        root = new BinaryTreeNode<T>(element);
    else 
    {
        if (comparableElement.compareTo(root.getElement()) < 0)
        {
            if (root.getLeft() == null) 
                this.getRootNode().setLeft(new BinaryTreeNode<T>(element));
            else
                addElement(element, root.getLeft());
        }
        else
        {
            if (root.getRight() == null) 
                this.getRootNode().setRight(new BinaryTreeNode<T>(element));
            else
                addElement(element, root.getRight());
        }
    }
    modCount++;

}

/**
 * Adds the specified object to the binary search tree in the
 * appropriate position according to its natural order.  Note that
 * equal elements are added to the right.
 *
 * @param element the element to be added to the binary search tree
 */
public  LinkedBinarySearchTree(T element, BinaryTreeNode<T> node) 
{
    Comparable<T> comparableElement = (Comparable<T>)element;

    if (comparableElement.compareTo(node.getElement()) < 0)
    {
        if (node.getLeft() == null) 
            node.setLeft(new BinaryTreeNode<T>(element));
        else
            addElement(element, node.getLeft());
    }
    else
    {
        if (node.getRight() == null) 
            node.setRight(new BinaryTreeNode<T>(element));
        else
            addElement(element, node.getRight());
    }
}

【问题讨论】:

  • 如果报错,怎么运行一次?
  • 你能给出完整的堆栈跟踪吗?这将有助于调试问题
  • 向我们展示BinaryTreeNode 构造函数和getElement()
  • 我编辑了代码以添加更多细节。

标签: java binary-search-tree


【解决方案1】:

我怀疑你得到一个StackOverflowError,因为你传递的是root.getLeft(和其他类似的)而不是node.getLeft

if (isEmpty())
    root = new BinaryTreeNode<T>(element);

上面的方法在第一次插入元素时创建了一个根,这是正确的。

遍历逻辑必须从传递给方法的当前节点开始,而不是

这是一个更正的:

else 
{
    if (comparableElement.compareTo(node.getElement()) < 0)
    {
        if (node.getLeft() == null) 
            node.setLeft(new BinaryTreeNode<T>(element));
        else
            addElement(element, node.getLeft());
    }
    else
    {
        if (node.getRight() == null) 
            node.setRight(new BinaryTreeNode<T>(element));
        else
            addElement(element, node.getRight());
    }
}

奖金: 您可以将类型参数 T 设为有界以摆脱 Comparable 检查。

参考资料:

Java- The meaning of &lt;T extends Comparable&lt;T&gt;&gt;?

Bounded Type Parameters Oracle Tutorial

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-09
    • 2020-04-20
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多