【发布时间】:2011-05-19 12:49:06
【问题描述】:
所以,我的医生让我实现 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 万个输入上尝试这种方法?
【问题讨论】:
-
你有最糟糕的医生。我的通常只是向我收取费用才能见到他。
标签: java sorting tree binary-search-tree