Search code examples
javarecursionarraylistbinary-search-treeinorder

inorder traversal of a BST and adding it to a list


I have written the following code, i think there is an issue somewhere with the parameters. I can't seem to find it. I am using a custom import which has .getLeftChild() and so on which should be pretty easy to figure out.

  ArrayList<E> L = new ArrayList<E>();
    BTreeNode<E> n = T.getRoot();

   if(n==null) {
       return L;

   }else{

   sisäjärjestysKulku(n, L);   <---- refers to missing type <E>
   }
   return L;
}


private static void sisäjärjestysKulku(BTreeNode <E> n, ArrayList<E> L) { <--

    // base case:
    if (n == null) 
    return;
    // reduction:

    sisäjärjestysKulku(n.getLeftChild(), L);
    L.add(n.getElement());
    sisäjärjestysKulku(n.getRightChild(),L);

}

How would one fix this so it works. Added arrows to where Eclipse gives me an error. I am hoping for a solution that would keep this structure.


Solution

  • Seems you missed the type parameter in method declaration, try:

    private static <E> void sisäjärjestysKulku(BTreeNode<E> n, ArrayList<E> L) {