简体   繁体   中英

Using inorder traversal of a BST with an ArrayList

I'm completing a method for an assignment that uses inorder traversal of a BST containing words in a dictionary.

I understand how to use recursion to complete the inorder traversal, but I'm having trouble containing my node values in an ArrayList, which is required for the method, because every time the method calls itself again the list is recreated and all other previous values are lost.

 /**
   * Recursive Helper method that returns a list of all the words stored in the subtree rooted at
   * current sorted alphabetically from A to Z
   * 
   * @param current pointer to the current DictionaryWord within this dictionaryBST
   * @return an ArrayList of all the words stored in the subtree rooted at current
   */
  private static ArrayList<String> getAllWordsHelper(DictionaryWord current) {
    ArrayList<String> list = new ArrayList<String>();
    if (current != null) {
      getAllWordsHelper(current.getLeftChild());
      list.add(current.getWord());
      getAllWordsHelper(current.getRightChild());
    }
    return list;
  }
}

Returning an ArrayList containing the values is required, and I cannot change it to pass one in as an argument, so I'm having some trouble working my way around this- all other examples I've seen online only print the current node. Any suggestions are appreciated, thanks!

The problem is that you aren't doing anything with the values returned from your recursive call. You need to actually add them to the list:

list.addAll(getAllWordsHelpers(current.getLeftChild()));
list.add(current.getWord();
list.addAll(getAllWordsHelpers(current.getRightChild()));

A more efficient method is to pass the list into the method so that you don't need to keep creating new lists:

private void getAllWordHelpers(List<String> list, DictionaryWord current) {
    if (current != null) {
        getAllWordHelpers(list, current.getLeftChild());
        list.add(current.getWord());
        getAllWordHelpers(list, current.getRightChild());
    }
}
The problem is you want to store words across multiple call stacks during inorder traversal, which is possible only by using a global object which should be available to all call stacks during recursive calls.

So here we have used a formal argument called words which represent a list object and this object will be common to all call stacks during recursive calls.

ArrayList<String> words = getAllWordsHelper(current, null)


private static ArrayList<String> getAllWordsHelper(DictionaryWord current, List<String> words) {
    if(words == null) words = new ArrayList(); 

    if (current != null) {
      getAllWordsHelper(words, current.getLeftChild());
      list.add(current.getWord());
      getAllWordsHelper(words, current.getRightChild());
    }
    return words;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM