0

I have a search algorithm that is supposed to parse the entire tree, find all results that could match a search query, and return them all as a list. I realize this isn't quite the point of the algorithm, but I'm doing this as a test with breadth first and depth first searches to see what is fastest by timing them. The other two searches work as intended, but when I enter the same search information as my goal for the DFID search i get an empty list. So I know my data is right, just something in the algorithm is wrong and I can't figure out what. I wrote this based off the pseudocode on Wikipedia. Here's what I have:

boolean maxDepth = false;
List<String> results = new ArrayList<String>();

public List<String> dfid(Tree t, String goal)
{
    int depth = 0;

    while (!maxDepth)
    {
        System.out.println(results);
        maxDepth = true;
        depth += 1;
        dls(t.root, goal, depth);
    }
    return results;
}

public void dls(Node node, String goal, int depth)
{
    System.out.println(depth);
    if (depth == 0 && node.data.contains(goal))
    {
        //set maxDepth to false if the node has children
        if (!node.children.isEmpty())
        {
            maxDepth = false;
        }
        results.add(node.data);
    }
    else if (depth > 0)
    {
        for(Node child : node.children)
        {
            dls(child, goal, depth-1);
        }
    }
}
zakparks31191
  • 919
  • 2
  • 21
  • 42
  • is goal a certain thing you want to find? – Ariel Pinchover Apr 15 '13 at 15:28
  • What class type is `node.data`? If `node.data.contains(goal)` returns false, your dls() method stops at the given depth: Thus, any children for which `node.data.contains(goal)` returns true would never be checked. – torquestomp Apr 15 '13 at 15:29
  • @torquestomp almost correct, but the check if the child is empty assures you still continue in the while loop in dfid – Ariel Pinchover Apr 15 '13 at 15:31
  • @torquestomp node.data is a String. – zakparks31191 Apr 15 '13 at 15:32
  • On second thought, the node.data.contains(goal) is problematic and very unstable. change the way you check for end of tree. another thing: would for(Node child : node.children) venture on every node the tree has? – Ariel Pinchover Apr 15 '13 at 15:35
  • Swap the lines `dls(t.root, goal, depth); depth += 1;` - at present you're not checking the tree's root to see if it contains `goal`. Is goal supposed to be case-insensitive? If so, then add the line `goal = goal.toLowerCase()` before your while loop, and change the contains check to `node.data.toLowerCase().contains(goal)` – Zim-Zam O'Pootertoot Apr 15 '13 at 15:35
  • @Zim-ZamO'Pootertoot lines switched, same result. Also, all the data and input is read in and made lowercase before this algorithm is called, so I know that the data and tree is all compatable before execution – zakparks31191 Apr 15 '13 at 15:38
  • would for(Node child : node.children) venture on every node the tree has? – Ariel Pinchover Apr 15 '13 at 15:39
  • @Infested To be honest, that for:each was guesswork, all the pseudocode for this algorithm I could find was for a binary tree. Since this isnt binary, I assumed that would do the same by iterating and recursing for each child in order of node.children – zakparks31191 Apr 15 '13 at 15:40
  • @speedofdark8 check how it it moves on the tree. – Ariel Pinchover Apr 15 '13 at 15:46
  • @Infested Currently, it doesn't. The way it processes now is that dls is called once, goes back to dfid, and returns an empty list. – zakparks31191 Apr 15 '13 at 15:48
  • @speedofdark8 swap the lines zim-zam suggested and add another else (after the else if depth > 0 ) to flip maxDepth to false. – Ariel Pinchover Apr 15 '13 at 15:49
  • @Infested magic. The extra else condition made it all work. Put it as an answer and I'll accept it. – zakparks31191 Apr 15 '13 at 15:53

1 Answers1

2

swap the lines zim-zam suggested and add another else (after the else if depth > 0 ) to flip maxDepth to false

Ariel Pinchover
  • 654
  • 3
  • 17
  • the problem was nicely noticed by @torquestomp that you didnt make sure you move on all the tree; you sometimes reached plots such that maxDepth is flipped to true but you should venture on the other side of the tree, but due to it being flipped to true, exiting dls and returning to dfid, you exited before ou should have. **in other words**: what gave it is you said you took it as a binary tree, and you would go only on one side of it and prematurely exiting. – Ariel Pinchover Apr 15 '13 at 15:56