0

I've created a TreeNode class which holds an ArrayList of tree nodes (named branch) and I want to add new branches to the tree by user entered paths. An example path being /Monkey/King/Bar where each of those would ideally be an existing branch with the exception of the last one (Bar would be the branch I'd like to add to King). Temp is a global variable I use for adding new branches to the tree and with recursion I'm trying to move down the path validating that each branch is a child of the previous branch and I'm having some trouble getting it to work. This is what I have so far and was wondering if it has something to do with not setting the parent when I re-declare the temp TreeNode. Any help would be appreciated and if anything I said is too vague please ask for clarification.

TreeNode tree = root;
boolean valid = false;
String y = x; //User entered path
for (int i = 0; i < x.length(); i++)
{
  if (x.charAt(i) == '/')
  {
      for (int j = 0; j < tree.branch.size(); j++){
        if (tree.branch.get(j).toString().equals(y)){
            System.out.println(temp.value);
            tree = tree.branch.get(j);
            temp = tree;
            valid = true;
        }
        else
          valid = false;
      }
      y = "";
}
dfb
  • 13,133
  • 2
  • 31
  • 52
Vorfall
  • 45
  • 3
  • 1
    What are you expecting from this line? if (tree.branch.get(j).toString().equals(y)) isn't y the full string /Monkey/King/Bar that does not exist yet? Shouldn't you be comparing some range of y? – rooftop Apr 30 '12 at 21:53
  • What happens when you try to use this code? How does it differ from what you were hoping would happen? "Having some trouble getting it to work" is pretty vague... – Gareth McCaughan Apr 30 '12 at 21:54
  • Have you noticed that `i` isn't used anywhere in the body of your loop other than when you check whether `x.charAt(i)` is `/`? In particular, you're not doing anything to extract any substring from `x`. Shouldn't you be? – Gareth McCaughan Apr 30 '12 at 21:55
  • Ultimately I don't want to do anything with the code but validate all existing paths, the method also returns the last string which I then would like to add to the temp tree. It checks each portion of the paths, but I don't think it properly sets temp TreeNode to the next path if that makes sense. I'm still pretty new to this so I apologize if this is really crude. – Vorfall Apr 30 '12 at 21:58
  • Also the path is a user entered string which exists, I just want to work with it from there EDIT: So given /Monkey/King/Bar assuming all nodes exist, I would end up adding TreeNode with value Bar to the branch of King – Vorfall Apr 30 '12 at 21:59

1 Answers1

1

It looks like you're trying to traverse the tree and add a node (or a branch) at the end if the path exists or something similar?

I think the problem is mainly how you're handling the string, not the nodes. You're not actually getting the parts of the path, you're doing the whole string, then nothing.

First, a better way to work with string in this way is to use String.split

String[] pathparts = String.split("/");

Next, you need to know if it's the part of the strign

for(int i=0;i<pathparts.length-1;i++){ // we don't want the last string
     // your code with .get(pathparts[i]) 
}

It looks like you're handling the rest of the code correctly if my assesment of what you're doign is correct.

dfb
  • 13,133
  • 2
  • 31
  • 52