Skip to content

Commit 7b7de81

Browse files
committed
Add Tree Traversal in Emojicode.
1 parent 035c4c4 commit 7b7de81

File tree

2 files changed

+134
-0
lines changed

2 files changed

+134
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
🐇 🌲 🍇
2+
🖍🆕 id 🔢
3+
🖍🆕 children 🍨🐚🌲🍆
4+
5+
🆕 depth_count 🔢 children_count 🔢 🍇
6+
1 ➡️ 🖍id
7+
🍨🍆 ➡️ 🖍children
8+
9+
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
10+
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
11+
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 🤜children_count🤛❗️❗️
12+
🍉
13+
🍉
14+
🍉
15+
16+
🔐 🆕 ⭐️ given_id 🔢 depth_count 🔢 children_count 🔢 🍇
17+
given_id ➡️ 🖍id
18+
🍨🍆 ➡️ 🖍children
19+
20+
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
21+
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
22+
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 🤜children_count🤛❗️❗️
23+
🍉
24+
🍉
25+
🍉
26+
27+
❗️ 🆔 ➡️ 🔢 🍇
28+
↩️ id
29+
🍉
30+
31+
❗️ 🧒 ➡️ 🍨🐚🌲🍆 🍇
32+
↩️ children
33+
🍉
34+
35+
💭 Depth-First Search Recursive pre-order
36+
❗️ 🌀 🍇
37+
😀 🔡 id 10❗️❗️
38+
39+
🔂 child children 🍇
40+
🌀 child❗️
41+
🍉
42+
🍉
43+
44+
💭 Depth-First Search Recursive post-order
45+
❗️ 🍥 🍇
46+
🔂 child children 🍇
47+
🍥 child❗️
48+
🍉
49+
50+
😀 🔡 id 10❗️❗️
51+
🍉
52+
53+
💭 Depth-First Search Recursive Inorder Binary
54+
💭 This assumes only 2 children.
55+
❗️ 🍭 🍇
56+
↪️ 🐔 children❗️ ▶️ 2 🍇
57+
↩️↩️
58+
🍉
59+
60+
↪️ 🐔 children❗️ ▶️ 0 🍇
61+
🍭🐽 children 0❗️❗️
62+
😀 🔡 id 10❗️❗️
63+
🍭🐽 children 1❗️❗️
64+
🍉
65+
🙅 🍇
66+
😀 🔡 id 10❗️❗️
67+
🍉
68+
🍉
69+
70+
💭 Depth-First Search Stack
71+
❗️ 🥞 🍇
72+
🍨 🐕 🍆 ➡️ stack
73+
74+
🔁 ❎ 🐔 stack❗️ 🙌 0❗️ 🍇
75+
🐽 stack 🐔 stack❗️ ➖ 1❗️ ➡️ temp
76+
🐨 stack 🐔 stack❗️ ➖ 1❗️
77+
78+
😀 🔡 🆔 temp❗️ 10❗️❗️
79+
80+
🧒 temp❗️ ➡️ temp_children
81+
🔂 child temp_children 🍇
82+
🐻 stack child❗️
83+
🍉
84+
🍉
85+
🍉
86+
87+
💭 Breadth-First Search Queue
88+
❗️ 🏢 🍇
89+
🍨 🐕 🍆 ➡️ queue
90+
91+
🔁 ❎ 🐔 queue❗️ 🙌 0❗️ 🍇
92+
🐽 queue 0❗️ ➡️ temp
93+
🐨 queue 0❗️
94+
95+
😀 🔡 🆔 temp❗️ 10❗️❗️
96+
97+
🧒 temp❗️ ➡️ temp_children
98+
🔂 child temp_children 🍇
99+
🐻 queue child❗️
100+
🍉
101+
🍉
102+
🍉
103+
🍉
104+
105+
🏁 🍇
106+
🆕🌲🆕 3 3❗️ ➡️ tree
107+
😀 🔤Tree Traversal🔤️❗️
108+
😀 🔤🌀 - Depth-First Search Recursive pre-order🔤❗️
109+
🌀tree❗️
110+
😀 🔤🍥 - Depth-First Search Recursive post-order🔤❗️
111+
🍥tree❗️
112+
😀 🔤🥞 - Depth-First Search Stack🔤❗️
113+
🥞tree❗️
114+
😀 🔤🏢 - Breadth-First Search Queue🔤❗️
115+
🏢tree❗️
116+
117+
🆕🌲🆕 3 2❗️ ➡️ binary_tree
118+
😀 🔤🍭 - Depth-First Search Recursive Inorder Binary🔤❗️
119+
🍭binary_tree❗️
120+
🍉

contents/tree_traversal/tree_traversal.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ This has not been implemented in your chosen language, so here is the Julia code
3434
[import:1-5, lang:"crystal"](code/crystal/tree-traversal.cr)
3535
{% sample lang="go" %}
3636
[import:5-8, lang:"golang"](code/golang/treetraversal.go)
37+
{% sample lang="emojic" %}
38+
[import:1-3, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
3739
{% endmethod %}
3840

3941
Because of this, the most straightforward way to traverse the tree might be recursive. This naturally leads us to the Depth-First Search (DFS) method:
@@ -70,6 +72,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
7072
[import:7-10, lang:"crystal"](code/crystal/tree-traversal.cr)
7173
{% sample lang="go" %}
7274
[import:10-15, lang:"golang"](code/golang/treetraversal.go)
75+
{% sample lang="emojic" %}
76+
[import:35-42, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
7377
{% endmethod %}
7478

7579
At least to me, this makes a lot of sense. We fight recursion with recursion! First, we first output the node we are on and then we call `DFS_recursive(...)` on each of its children nodes. This method of tree traversal does what its name implies: it goes to the depths of the tree first before going through the rest of the branches. In this case, the ordering looks like:
@@ -114,6 +118,8 @@ Now, in this case the first element searched through is still the root of the tr
114118
[import:12-15, lang:"crystal"](code/crystal/tree-traversal.cr)
115119
{% sample lang="go" %}
116120
[import:17-22, lang:"golang"](code/golang/treetraversal.go)
121+
{% sample lang="emojic" %}
122+
[import:44-51, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
117123
{% endmethod %}
118124

119125
<p>
@@ -153,6 +159,8 @@ In this case, the first node visited is at the bottom of the tree and moves up t
153159
[import:17-31, lang:"crystal"](code/crystal/tree-traversal.cr)
154160
{% sample lang="go" %}
155161
[import:24-38, lang:"golang"](code/golang/treetraversal.go)
162+
{% sample lang="emojic" %}
163+
[import:53-68, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
156164
{% endmethod %}
157165

158166
<p>
@@ -202,6 +210,8 @@ In code, it looks like this:
202210
[import:33-41, lang:"crystal"](code/crystal/tree-traversal.cr)
203211
{% sample lang="go" %}
204212
[import:40-49, lang:"golang"](code/golang/treetraversal.go)
213+
{% sample lang="emojic" %}
214+
[import:70-85, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
205215
{% endmethod %}
206216

207217
All this said, there are a few details about DFS that might not be idea, depending on the situation. For example, if we use DFS on an incredibly long tree, we will spend a lot of time going further and further down a single branch without searching the rest of the data structure. In addition, it is not the natural way humans would order a tree if asked to number all the nodes from top to bottom. I would argue a more natural traversal order would look something like this:
@@ -243,6 +253,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
243253
[import:43-51, lang:"crystal"](code/crystal/tree-traversal.cr)
244254
{% sample lang="go" %}
245255
[import:51-60, lang:"golang"](code/golang/treetraversal.go)
256+
{% sample lang="emojic" %}
257+
[import:87-102, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
246258
{% endmethod %}
247259

248260
## Example Code
@@ -286,6 +298,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
286298
[import, lang:"crystal"](code/crystal/tree-traversal.cr)
287299
{% sample lang="go" %}
288300
[import, lang:"golang"](code/golang/treetraversal.go)
301+
{% sample lang="emojic" %}
302+
[import, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
289303
{% endmethod %}
290304

291305

0 commit comments

Comments
 (0)