Skip to content

Commit 338ba33

Browse files
committed
Improve JS example for tree traversals
1 parent 8d49a3b commit 338ba33

File tree

3 files changed

+63
-71
lines changed

3 files changed

+63
-71
lines changed

chapters/tree_traversal/code/javascript/Tree_example.js

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
function createTree(rows, children) {
2+
if (rows === 0) {
3+
return { id: rows, children: [] };
4+
}
5+
6+
return {
7+
id: rows,
8+
children: Array.from(Array(children).keys(), () => createTree(rows - 1, children))
9+
};
10+
}
11+
12+
function dfsPreorder(tree) {
13+
console.log(tree.id);
14+
tree.children.forEach(dfs);
15+
}
16+
17+
function dfsPostorder(tree) {
18+
tree.children.forEach(dfs);
19+
console.log(tree.id);
20+
}
21+
22+
function dfsInorder(tree) {
23+
if (!tree) {
24+
return;
25+
}
26+
27+
if (tree.children.length > 2) {
28+
throw new Error("Postorder traversal is only valid for binary trees");
29+
}
30+
31+
dfsInorder(tree.children[0]);
32+
console.log(tree.id);
33+
dfsInorder(tree.children[1]);
34+
}
35+
36+
function dfsIterative(tree) {
37+
const stack = [tree];
38+
while (stack.length > 0) {
39+
const current = stack.pop();
40+
console.log(current.id);
41+
stack.push(...current.children);
42+
}
43+
}
44+
45+
function bfs(tree) {
46+
const queue = [tree];
47+
while (queue.length > 0) {
48+
const [current] = queue.splice(0, 1);
49+
console.log(current.id);
50+
queue.push(...current.children);
51+
}
52+
}
53+
54+
const root = createTree(3, 3);
55+
dfsRecursive(root);
56+
dfsIterative(root);
57+
bfs(root);

chapters/tree_traversal/tree_traversal.md

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
3939
{% sample lang="c" %}
4040
[import:37-45, lang:"c_cpp"](code/c/tree_traversal.c)
4141
{% sample lang="js" %}
42-
[import:15-23, lang:"javascript"](code/javascript/Tree_example.js)
42+
[import:12-15, lang:"javascript"](code/javascript/tree.js)
4343
{% sample lang="py2" %}
4444
[import:8-16, lang:"python"](code/python2/Tree_example.py)
4545
{% sample lang="py3" %}
@@ -76,8 +76,7 @@ This has not been implemented in your chosen language, so here is the Julia code
7676
{% sample lang="c" %}
7777
[import:47-53, lang:"c_cpp"](code/c/tree_traversal.c)
7878
{% sample lang="js" %}
79-
This has not been implemented in your chosen language, so here is the Julia code
80-
[import:18-26, lang:"julia"](code/julia/Tree.jl)
79+
[import:17-20, lang:"javascript"](code/javascript/tree.js)
8180
{% sample lang="py2" %}
8281
This has not been implemented in your chosen language, so here is the Julia code
8382
[import:18-26, lang:"julia"](code/julia/Tree.jl)
@@ -111,8 +110,7 @@ This has not been implemented in your chosen language, so here is the Julia code
111110
{% sample lang="c" %}
112111
[import:55-73, lang:"c_cpp"](code/c/tree_traversal.c)
113112
{% sample lang="js" %}
114-
This has not been implemented in your chosen language, so here is the Julia code
115-
[import:28-43, lang:"julia"](code/julia/Tree.jl)
113+
[import:22-34, lang:"javascript"](code/javascript/tree.js)
116114
{% sample lang="py2" %}
117115
This has not been implemented in your chosen language, so here is the Julia code
118116
[import:28-43, lang:"julia"](code/julia/Tree.jl)
@@ -155,7 +153,7 @@ In code, it looks like this:
155153
{% sample lang="c" %}
156154
[import:75-93, lang:"c_cpp"](code/c/tree_traversal.c)
157155
{% sample lang="js" %}
158-
[import:25-40, lang:"javascript"](code/javascript/Tree_example.js)
156+
[import:36-43, lang:"javascript"](code/javascript/tree.js)
159157
{% sample lang="py2" %}
160158
[import:25-36, lang:"python"](code/python2/Tree_example.py)
161159
{% sample lang="py3" %}
@@ -188,7 +186,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
188186
{% sample lang="c" %}
189187
[import:95-113, lang:"c_cpp"](code/c/tree_traversal.c)
190188
{% sample lang="js" %}
191-
[import:42-57, lang:"javascript"](code/javascript/Tree_example.js)
189+
[import:45-52, lang:"javascript"](code/javascript/tree.js)
192190
{% sample lang="py2" %}
193191
[import:38-49, lang:"python"](code/python2/Tree_example.py)
194192
{% sample lang="py3" %}
@@ -224,7 +222,7 @@ tree_traversal.c
224222
[import, lang:"c_cpp"](code/c/tree_traversal.c)
225223
{% sample lang="js" %}
226224
### JavaScript
227-
[import, lang:"javascript"](code/javascript/Tree_example.js)
225+
[import, lang:"javascript"](code/javascript/tree.js)
228226
{% sample lang="py2" %}
229227
### Python 2
230228
[import, lang:"python"](code/python2/Tree_example.py)

0 commit comments

Comments
 (0)