Search code examples
javascriptpythonalgorithmdata-structuresdepth-first-search

Recursive Depth First Search in Javascript vs Python


So I am doing algorithm problems on AlgoExpert, and I came across a recursive DFS algorithm. Originally I tried solving it in JavaScript but was having issues. When I watched the code walk-through I saw that the way it was solved is super easy.

Here is the Python solution:

def depthFirstSearch(self, array):
    array.append(self.name);
    for child in self.children:
        child.depthFirstSearch(array);
    return array

My question is, how would you achieve the same thing in JavaScript? I tried doing:

function depthFirstSearch(array) {
    array.push(this.name);
    for(let i=0; i < array.length; i++) {
        depthFirstSearch(array[i]);
    };
    return array;
}

I keep getting an error:

depthFirstSearch is not defined

I'm confused about why I'm getting the error in JavaScript, when I'm trying to recursively call the function within itself?

Here is the whole code snippet:

class Node {
    constructor(name) {
        this.name = name;
        this.children = [];
    }

    addChild(name) {
        this.children.push(new Node(name));
        return this;
    }

    depthFirstSearch(array) {
       /*
        * Step 1: Add the root node to the array
        *                   this.name is the root at the start
        * Step 2: Check that the left child node isn't null
        * Step 3: If the left child node isn't null, Add the left child node to the array
        * Step 4: If the left child node is null check the right child node
        * Step 5: If the right child node isn't null, add the right child node to the array
        * Step 6: Repeat steps 1-5 until there are no nodes left to check
        */
        array.push(this.name);
        const children = this.children;
        for(let i=0; i < array.length; i++) {
            depthFirstSearch(array[i]);
        };
        return array;
    }
}

Solution

  • Several issues:

    • depthFirstSearch is a method of the Node class, not a global function, so it should be called on a node instance. So for instance, if you have a node named child it should be called like child.depthFirstSearch

    • As it is a method, you should not use the function keyword (in a class construct). This was correctly removed in the last code block you have in your question.

    • The argument passed to depthFirstSearch should be an array, but you pass it array[i] which is a node. It should be just array, as in the Python code.

    • The loop should not be over array, but over the children of the current node, i.e. over this.children. So you would have i < this.children.length in your for loop.

    Some other remarks:

    • No semi-colon is needed after a for loop. Adding it actually introduces a (harmless) empty statement.
    • Using a for..of instead of a plain old for loop makes the code a bit shorter and also better mimics what the Python code does.

    Corrected version:

    class Node {
    
        /* ... the other code ... */
    
        depthFirstSearch(array) {
            array.push(this.name);
            for (let child of this.children) {
                child.depthFirstSearch(array);
            }
            return array;
        }
    }