Skip to content

Commit c1fc4ae

Browse files
committed
added if-not-tree checks
1 parent 44449b1 commit c1fc4ae

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

contents/tree_traversal/code/javascript/tree.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,28 @@ 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
}
2129

2230
function dfsInorder(tree) {
31+
if (!tree) {
32+
return;
33+
}
34+
2335
switch (tree.children.length) {
2436
case 2:
2537
dfsInorder(tree.children[0]);

contents/tree_traversal/tree_traversal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
6060
{% sample lang="java" %}
6161
[import:21-27, lang:"java"](code/java/Tree.java)
6262
{% sample lang="js" %}
63-
[import:12-15, lang:"javascript"](code/javascript/tree.js)
63+
[import:12-19, lang:"javascript"](code/javascript/tree.js)
6464
{% sample lang="py" %}
6565
[import:18-23, lang:"python"](code/python/Tree_example.py)
6666
{% sample lang="scratch" %}
@@ -114,7 +114,7 @@ Now, in this case the first element searched through is still the root of the tr
114114
{% sample lang="java" %}
115115
[import:34-41, lang:"java"](code/java/Tree.java)
116116
{% sample lang="js" %}
117-
[import:17-20, lang:"javascript"](code/javascript/tree.js)
117+
[import:21-28, lang:"javascript"](code/javascript/tree.js)
118118
{% sample lang="py" %}
119119
[import:26-31, lang:"python"](code/python/Tree_example.py)
120120
{% sample lang="scratch" %}
@@ -163,7 +163,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
163163
{% sample lang="java" %}
164164
[import:48-62, lang:"java"](code/java/Tree.java)
165165
{% sample lang="js" %}
166-
[import:22-39, lang:"javascript"](code/javascript/tree.js)
166+
[import:30-51, lang:"javascript"](code/javascript/tree.js)
167167
{% sample lang="py" %}
168168
[import:34-46, lang:"python"](code/python/Tree_example.py)
169169
{% sample lang="scratch" %}
@@ -221,7 +221,7 @@ In code, it looks like this:
221221
{% sample lang="java" %}
222222
[import:65-79, lang:"java"](code/java/Tree.java)
223223
{% sample lang="js" %}
224-
[import:41-48, lang:"javascript"](code/javascript/tree.js)
224+
[import:53-60, lang:"javascript"](code/javascript/tree.js)
225225
{% sample lang="py" %}
226226
[import:49-60, lang:"python"](code/python/Tree_example.py)
227227
{% sample lang="scratch" %}
@@ -272,7 +272,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
272272
{% sample lang="java" %}
273273
[import:81-95, lang:"java"](code/java/Tree.java)
274274
{% sample lang="js" %}
275-
[import:50-57, lang:"javascript"](code/javascript/tree.js)
275+
[import:62-69, lang:"javascript"](code/javascript/tree.js)
276276
{% sample lang="py" %}
277277
[import:63-75, lang:"python"](code/python/Tree_example.py)
278278
{% sample lang="scratch" %}

0 commit comments

Comments
 (0)