0

I can't figure out why my code is getting a StackOverFlow error. I'm trying to add a file as a node to its folder in a tree structure. I know the problem lies somewhere in the recursion, but I don't see why my function is resulting in infinite recursion. Here's my code:

    private void addToFolders(String fileName, String folderName, Node<String> folders) {
      /** Base case: if root node is the new folder, add file under it **/
      if (folderName == folders.getData()) {
        folders.addChild(fileName);
        return;
      }
      /** If root node is not it, then look through its children **/
      List<Node<String>> children = folders.getChildren();
      for (int i = 0; i < children.size(); i++) {
        addToFolders(fileName, folderName, children.get(i));
      }
    }

Please advice, and thanks for your help in advance.

  • My guess is that you make too many recursive calls. Could it be that there is a loop in the datastructure? – Turing85 Aug 13 '21 at 17:06

0 Answers0