[BUG]: DepthFirstSearch.js in trees folder is not working properly
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
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.logstatements)
Feel free to rewrite / refactor it.
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 .
You have added a commit to your repo. You haven't created a PR yet.
hey I have done that(created a PR) ,
there is issue with my connection
sorry for that