实现treeSort()的问题

时间:2011-05-19 12:49:07

标签: java sorting tree binary-search-tree

所以,我的医生让我实现treeSort(),然后在int [1000000]上测试它并计算时间。

我的课程BSTree<E>包含以下方法:

public void treeSort(E[] data)
{
    inorder(data, new Process<E>(), root);
}

public static <E> void inorder(E[] list, Process<E> proc, BTNode<E> p)
{
    if (p != null)
    {
        inorder(list, proc, p.getLeft( ));    // Traverse its left subtree
        proc.append(list, p.getElement( ));   // Process the node
        inorder(list, proc, p.getRight( ));   // Traverse its right subtree
    }
}

我有Process<E>课程:

public class Process<E>
{
    private int counter = 0;
    public void append(E[] list, E element)
    {
        list[counter] = element;
        counter++;
    }
}

我有以下Main课程:

public class Main
{
    public static void main(String[] args)
    {
        int[] temp = {4,2,6,4,5,2,9,7,11,0,-1,4,-5};
        treeSort(temp);
        for(int s : temp) System.out.println(s);
    }

    public static void treeSort(int[] data)
    {
        BSTree<Integer> tree = new BSTree<Integer>();
        for(int i: data) tree.insert(i);
        tree.inorder(data, new Process<Integer>(), tree.getRoot()); // I get an error here!
    }
}

错误是:

cannot find symbol - method inorder(int[], Process<java.lang.Integer>, BTNode<java.lang.Integer>); maybe you meant: inorder(E[], Process<E>, BTNode<E>);

我通过将treeSort(int[] data)更改为treeSort(Integer[] data)来解决此问题。但是我在treeSort(temp);

的主要方法中出错了

,错误是:

treeSort(java.lang.Integer) in Main cannot be applied to (int[])

那么,我如何处理这个问题,同时考虑到不增加我应该在100万输入上尝试这种方法的复杂性时间?

2 个答案:

答案 0 :(得分:1)

Integer [] temp = {4,2,6,4,5,2,9,7,11,0,-1,4,-5};

编辑:用它来修复第二个错误。

答案 1 :(得分:0)

泛型不能是基础类。您可以使用Integer []而不是int [],并依赖于自动装箱。