Skip to content

Commit cf56e67

Browse files
authored
JavaScript tree traversal: updated dfsInorder (#902)
1 parent 44d8118 commit cf56e67

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

CONTRIBUTORS.md

+1
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,4 @@ This file lists everyone, who contributed to this repo and wanted to show up her
6060
- Ridham177
6161
- Hugo Salou
6262
- Dimitri Belopopsky
63+
+ Henrik Abel Christensen

contents/tree_traversal/code/javascript/tree.js

+23-6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,19 @@ function createTree(rows, children) {
1010
}
1111

1212
function dfsPreorder(tree) {
13+
if (!tree) {
14+
return;
15+
}
16+
1317
console.log(tree.id);
1418
tree.children.forEach(dfsPreorder);
1519
}
1620

1721
function dfsPostorder(tree) {
22+
if (!tree) {
23+
return;
24+
}
25+
1826
tree.children.forEach(dfsPostorder);
1927
console.log(tree.id);
2028
}
@@ -24,13 +32,22 @@ function dfsInorder(tree) {
2432
return;
2533
}
2634

27-
if (tree.children.length > 2) {
28-
throw new Error("Postorder traversal is only valid for binary trees");
35+
switch (tree.children.length) {
36+
case 2:
37+
dfsInorder(tree.children[0]);
38+
console.log(tree.id);
39+
dfsInorder(tree.children[1]);
40+
break;
41+
case 1:
42+
dfsInorder(tree.children[0]);
43+
console.log(tree.id);
44+
break;
45+
case 0:
46+
console.log(tree.id);
47+
break;
48+
default:
49+
throw new Error("Postorder traversal is only valid for binary trees");
2950
}
30-
31-
dfsInorder(tree.children[0]);
32-
console.log(tree.id);
33-
dfsInorder(tree.children[1]);
3451
}
3552

3653
function dfsIterative(tree) {

contents/tree_traversal/tree_traversal.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
6262
{% sample lang="java" %}
6363
[import:20-26, lang:"java"](code/java/Tree.java)
6464
{% sample lang="js" %}
65-
[import:12-15, lang:"javascript"](code/javascript/tree.js)
65+
[import:12-19, lang:"javascript"](code/javascript/tree.js)
6666
{% sample lang="py" %}
6767
[import:18-23, lang:"python"](code/python/Tree_example.py)
6868
{% sample lang="scratch" %}
@@ -118,7 +118,7 @@ Now, in this case the first element searched through is still the root of the tr
118118
{% sample lang="java" %}
119119
[import:33-40, lang:"java"](code/java/Tree.java)
120120
{% sample lang="js" %}
121-
[import:17-20, lang:"javascript"](code/javascript/tree.js)
121+
[import:21-28, lang:"javascript"](code/javascript/tree.js)
122122
{% sample lang="py" %}
123123
[import:26-31, lang:"python"](code/python/Tree_example.py)
124124
{% sample lang="scratch" %}
@@ -169,7 +169,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
169169
{% sample lang="java" %}
170170
[import:47-64, lang:"java"](code/java/Tree.java)
171171
{% sample lang="js" %}
172-
[import:22-34, lang:"javascript"](code/javascript/tree.js)
172+
[import:30-51, lang:"javascript"](code/javascript/tree.js)
173173
{% sample lang="py" %}
174174
[import:34-46, lang:"python"](code/python/Tree_example.py)
175175
{% sample lang="scratch" %}
@@ -229,7 +229,7 @@ In code, it looks like this:
229229
{% sample lang="java" %}
230230
[import:67-81, lang:"java"](code/java/Tree.java)
231231
{% sample lang="js" %}
232-
[import:36-43, lang:"javascript"](code/javascript/tree.js)
232+
[import:53-60, lang:"javascript"](code/javascript/tree.js)
233233
{% sample lang="py" %}
234234
[import:49-60, lang:"python"](code/python/Tree_example.py)
235235
{% sample lang="scratch" %}
@@ -282,7 +282,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
282282
{% sample lang="java" %}
283283
[import:83-97, lang:"java"](code/java/Tree.java)
284284
{% sample lang="js" %}
285-
[import:45-52, lang:"javascript"](code/javascript/tree.js)
285+
[import:62-69, lang:"javascript"](code/javascript/tree.js)
286286
{% sample lang="py" %}
287287
[import:63-75, lang:"python"](code/python/Tree_example.py)
288288
{% sample lang="scratch" %}

0 commit comments

Comments
 (0)