Issues with implementing treeSort()
So, my doctor asking me to implement treeSort() and then test it on int[1000000] and calculate the time.
I have class BSTree<E>
which contains the following methods:
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
}
}
and I have Process<E>
class:
public class Process<E>
{
private int counter = 0;
public void append(E[] list, E element)
{
list[counter] = element;
counter++;
}
}
and I have the following Main
class:
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!
}
}
The error is:
cannot find symbol - method inorder(int[], Process<java.lang.Integer>, BTNode<java.lang.Integer>); maybe you meant: inorder(E[], Process<E>, BTNode<E>);
I fixed that by changing treeSort(int[] data)
to treeSort(Integer[] data)
. But I got an error in the main method at treeSort(temp);
and the error is:
treeSort(java.lang.Integer) in Main cannot be applied to (int[])
So, how can I deal with this issue with taking into account not increasing the complexity time where I should try this method upon 1 million inputs?
Integer[]temp = {4,2,6,4,5,2,9,7,11,0,-1,4,-5};
EDIT: use it to fix second error.
Generics cannot be basic classes. You can use Integer[] instead of int[], and rely on autoboxing.
精彩评论