-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
solves issue #1280 #1281
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
solves issue #1280 #1281
Conversation
traverseDFS() is not working properly , while logging it says [ undefined ]. and there is also an issue with searchDFS() function, the following issue i have faced The stack array is being used as a queue instead of a stack. A queue follows the first-in, first-out (FIFO) principle, while a stack follows the last-in, first-out (LIFO) principle. For a DFS algorithm, we need to use a stack data structure. The code is pushing elements to the stack using the tree[node.right] and tree[node.left] expressions, but it should be using node.right and node.left directly. The tree array should not be used here. The code is using tree[0] as the starting node for the search, but it should be using the entire tree object as the starting node.
| const res = [] | ||
| function dfs(tree) { | ||
| if (!tree) return; | ||
| console.log(tree.value); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should either be calling a visitor function or yielding. No side effects.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But for what
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reusability.
Trees/DepthFirstSearch.js
Outdated
|
|
||
| if (curr.left) { | ||
| stack.push(curr.left) | ||
| for (let i = 0; i < tree.children.length; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just use for (const child of tree.children)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah we can use it , its a matter of preference but here it provides much more fine control
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see why the index would be needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have done that
| while (stack.length !== 0) { | ||
| for (let i = 0; i < stack.length; i++) { | ||
| const node = stack.pop() | ||
| const tree = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in a proper test case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you provide some more info on that like what kind of test you want,
it will help me to make changes then....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| } | ||
|
|
||
| function searchDFS (tree, value) { | ||
| const stack = [] | ||
| // This function takes two arguments: tree, which is the tree to be searched, and target, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be in a JS doc comment
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
|
Issue was closed via another PR. |
traverseDFS() is not working properly ,
while logging it says [ undefined ].
and there is also an issue with searchDFS() function,
the following issue i have faced
The stack array is being used as a queue instead of a stack. A queue follows the first-in, first-out (FIFO) principle, while a stack follows the last-in, first-out (LIFO) principle. For a DFS algorithm, we need to use a stack data structure.
The code is pushing elements to the stack using the tree[node.right] and tree[node.left] expressions, but it should be using node.right and node.left directly. The tree array should not be used here.
The code is using tree[0] as the starting node for the search, but it should be using the entire tree object as the starting node.
Describe your change:
Checklist:
Example:
UserProfile.jsis allowed butuserprofile.js,Userprofile.js,user-Profile.js,userProfile.jsare notFixes: #{$ISSUE_NO}.