JavaScript icon indicating copy to clipboard operation
JavaScript copied to clipboard

[BUG]: DepthFirstSearch.js in trees folder is not working properly

Open sundaram2021 opened this issue 2 years ago • 4 comments

Description

tarverseDFS function is not working

Expected Behavior

tarverseDFS function should traverse the tree

Actual Behavior

In tarverseDFS function , the res array ia empty or undefined

Steps to reproduce (if applicable)

No response

Additional information

No response

sundaram2021 avatar Feb 04 '23 17:02 sundaram2021

Works fine for me:

$ node
Welcome to Node.js v16.19.0.
Type ".help" for more information.
> 
> function traverseDFS (root) {
...   const stack = [root]
...   const res = []
... 
...   while (stack.length) {
...     const curr = stack.pop()
...     res.push(curr.key)
... 
...     if (curr.right) {
...       stack.push(curr.right)
...     }
... 
...     if (curr.left) {
...       stack.push(curr.left)
...     }
...   }
... 
...   return res.reverse()
... }
undefined
> traverseDFS({key: 42, left: {key: 33}})
[ 33, 42 ]

That said, there seem to be multiple issues with this code:

  • No tests?
  • No proper module usage: Just writes its functions to the global environment (ugh)
  • General graph traversal method limited to trees (binary trees even), pretty much not documented at all
  • Side effects ("tests" seem to be embedded as console.log statements)

Feel free to rewrite / refactor it.

appgurueu avatar Feb 04 '23 18:02 appgurueu

hey, while traversing the tree using dfs algorithm there is a common conception that should be followed ,

the argument 'tree' should be passed to the function as an argument and then it should traverse it .

I have refactored the code and generated a PR ,

........

could you please merge that .

sundaram2021 avatar Feb 05 '23 02:02 sundaram2021

You have added a commit to your repo. You haven't created a PR yet.

appgurueu avatar Feb 05 '23 10:02 appgurueu

hey I have done that(created a PR) ,

there is issue with my connection

sorry for that

sundaram2021 avatar Feb 05 '23 11:02 sundaram2021