Skip to content

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

Closed
wants to merge 2 commits into from
Closed

Conversation

sundaram2021
Copy link

@sundaram2021 sundaram2021 commented Feb 5, 2023

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.

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

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);
Copy link
Collaborator

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But for what

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For reusability.


if (curr.left) {
stack.push(curr.left)
for (let i = 0; i < tree.children.length; i++) {
Copy link
Collaborator

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)

Copy link
Author

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

Copy link
Collaborator

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.

Copy link
Author

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 = {
Copy link
Collaborator

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.

Copy link
Author

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....

Copy link
Collaborator

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,
Copy link
Collaborator

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

@stale
Copy link

stale bot commented Jun 18, 2023

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.

@stale stale bot added the stale Closed due to age label Jun 18, 2023
@appgurueu
Copy link
Collaborator

Issue was closed via another PR.

@appgurueu appgurueu closed this Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale Closed due to age
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants