二叉搜索树的 K[] preOrder() 方法:关于通用返回

发布于 2024-12-12 21:42:59 字数 1369 浏览 2 评论 0原文

实施使用:2011 年 10 月 28 日的数据结构实验室练习 要做的事: 实现二叉搜索树

问题: preOrder()、inOrder() 和 postOrder() 方法的 K[] 返回 问题

详细信息: BST 只能将其根作为参数。上面提到的方法已在我们教授给出的接口中进行了如下描述:

    /**
    * Returns an array of keys filled according
    * to the pre-order traversing in a BST.  
    */      
    public K[] preOrder();      
    public K[] order();         
    public K[] postOrder();

我可以使用以下代码实例化通用数组:

public K[] preOrder() {

    if (root == null) { return null; }

    ArrayList<K> list = new ArrayList<K>();
    preOrderRecursive(root,list);
    K[] toReturn = (K[]) Array.newInstance(this.getRoot().getKey().getClass(), list.size());

    for (int i = 0; i < list.size(); i++) {
        toReturn[i] = list.get(i);
    }

    return toReturn;
}

但是,当我使用我们教授也提供的测试类测试该方法时,我得到了 nullPointerException ,我认为指的是 BST 的根,它已经被实例化一次,但已在测试中的某个点被删除,并且当它再次调用该方法时,该方法返回 null,而不是预期的空数组测试:

    (...)

    tree1 = new BSTImpl<Integer, Integer>();
    for (int i = 0; i < SIZE; i++) {
        tree1.insert(i, i);
    }
    tree1.remove(1);
    tree1.remove(2);
    tree1.remove(3);
    tree1.remove(4);
    assertArrayEquals(new Integer[]{},tree1.preOrder());

    (...)

知道我无法更改返回类型或方法的参数,我该怎么做才能避免此异常?我可以以某种方式获取组件类型并使用它来实例化空数组(我是如何做到这一点的?)?

也欢迎任何改进我的代码的建议。

Implementation Use: Data Structure Lab Exercise for October/28/2011
To do: Implement a Binary Search Tree

Problem: K[] return of methods preOrder(), inOrder() and postOrder()

Problem Details:
The BST must only have its root as a parameter. The methods above mentioned have been described in an interface given by our professor as the following:

    /**
    * Returns an array of keys filled according
    * to the pre-order traversing in a BST.  
    */      
    public K[] preOrder();      
    public K[] order();         
    public K[] postOrder();

I could instantiate the generic array with the following code:

public K[] preOrder() {

    if (root == null) { return null; }

    ArrayList<K> list = new ArrayList<K>();
    preOrderRecursive(root,list);
    K[] toReturn = (K[]) Array.newInstance(this.getRoot().getKey().getClass(), list.size());

    for (int i = 0; i < list.size(); i++) {
        toReturn[i] = list.get(i);
    }

    return toReturn;
}

But, when i tested the method using a testing class also provided by our professor, i got a nullPointerException, wich i think is referring to the root of the BST, that has been once instantiated, but has been removed at a point in the test, and when it calls the method again, the method returns null, not an empty array as expected by the test:

    (...)

    tree1 = new BSTImpl<Integer, Integer>();
    for (int i = 0; i < SIZE; i++) {
        tree1.insert(i, i);
    }
    tree1.remove(1);
    tree1.remove(2);
    tree1.remove(3);
    tree1.remove(4);
    assertArrayEquals(new Integer[]{},tree1.preOrder());

    (...)

Knowing that i can't change the return type nor the parameters of the method, what can i do to avoid this Exception? Can i somehow get the component type and use it to instantiate the empty array (how'd i do this?)?

Any tips to improve my code are also welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

本王不退位尔等都是臣 2024-12-19 21:43:00

问题是:您真的需要返回一个Integer数组吗? - 我不这么认为。

我假设 assertArrayEquals() 不会检查数组的组件类型,而只是比较所包含的值。

根据您展示的代码,我想说由于类型擦除,其他任何事情都是不可能的。

因此,请尝试返回 Object[] 或任何适用于您的情况的超类型:

return new Object[0];

return list.toArray();

(需要未经检查的强制转换,但这应该没问题)

编辑:

好的,所以
在这种情况下,请使用:

return new Comparable[0];

return (Comparable[])list.toArray(new Comparable[]);

Question is: Do you really need an Integer array returned? - I don't think so.

I assume assertArrayEquals() does not check the component type of the arrays but rather only compares the contained values.

With the code you showed, I'd say anything else would not be possible due to type erasure.

So try to return an Object[] or whatever super-type applies in your case:

return new Object[0];

and

return list.toArray();

(unchecked casts required, but that should be o.k.)

EDIT:

Ok, so <K extends Comparable>?
In that case use:

return new Comparable[0];

and

return (Comparable[])list.toArray(new Comparable[]);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文