Skip to content

JavaScript tree traversal: updated dfsInorder #902

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

Merged
merged 3 commits into from
Oct 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
- Ridham177
- Hugo Salou
- Dimitri Belopopsky
+ Henrik Abel Christensen
29 changes: 23 additions & 6 deletions contents/tree_traversal/code/javascript/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,19 @@ function createTree(rows, children) {
}

function dfsPreorder(tree) {
if (!tree) {
return;
}

console.log(tree.id);
tree.children.forEach(dfsPreorder);
}

function dfsPostorder(tree) {
if (!tree) {
return;
}

tree.children.forEach(dfsPostorder);
console.log(tree.id);
}
Expand All @@ -24,13 +32,22 @@ function dfsInorder(tree) {
return;
}

if (tree.children.length > 2) {
throw new Error("Postorder traversal is only valid for binary trees");
switch (tree.children.length) {
case 2:
dfsInorder(tree.children[0]);
console.log(tree.id);
dfsInorder(tree.children[1]);
break;
case 1:
dfsInorder(tree.children[0]);
console.log(tree.id);
break;
case 0:
console.log(tree.id);
break;
default:
throw new Error("Postorder traversal is only valid for binary trees");
}

dfsInorder(tree.children[0]);
console.log(tree.id);
dfsInorder(tree.children[1]);
}

function dfsIterative(tree) {
Expand Down
10 changes: 5 additions & 5 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
{% sample lang="java" %}
[import:21-27, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:12-15, lang:"javascript"](code/javascript/tree.js)
[import:12-19, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:18-23, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
Expand Down Expand Up @@ -114,7 +114,7 @@ Now, in this case the first element searched through is still the root of the tr
{% sample lang="java" %}
[import:34-41, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:17-20, lang:"javascript"](code/javascript/tree.js)
[import:21-28, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:26-31, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
Expand Down Expand Up @@ -163,7 +163,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
{% sample lang="java" %}
[import:48-62, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:22-34, lang:"javascript"](code/javascript/tree.js)
[import:30-51, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:34-46, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
Expand Down Expand Up @@ -221,7 +221,7 @@ In code, it looks like this:
{% sample lang="java" %}
[import:65-79, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:36-43, lang:"javascript"](code/javascript/tree.js)
[import:53-60, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:49-60, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
Expand Down Expand Up @@ -272,7 +272,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
{% sample lang="java" %}
[import:81-95, lang:"java"](code/java/Tree.java)
{% sample lang="js" %}
[import:45-52, lang:"javascript"](code/javascript/tree.js)
[import:62-69, lang:"javascript"](code/javascript/tree.js)
{% sample lang="py" %}
[import:63-75, lang:"python"](code/python/Tree_example.py)
{% sample lang="scratch" %}
Expand Down