Skip to content

Commit 0287fb1

Browse files
committed
Add Tree Traversal in Emojicode
This is a combination of 5 commits. This is the 1st commit message: Add Tree Traversal in Emojicode This is the commit message #2: Remove unnecessary 🤜🤛. This was suggested by thbwd: "No need to wrap 🤜children_count🤛 into 🤜🤛actually." (https://github.com/algorithm-archivists/algorithm-archive/pull/ 471#discussion_r223291032) This is the commit message #3: Extract shared logic into a method. This was suggested by thbwd: "This is shared logic. I’d recommend you extract it into a method." (https://github.com/algorithm-archivists/algorithm-archive/pull/ 471#discussion_r223291140) This is the commit message #4: Use documentation comments. This was suggested by thbwd: "Shouldn’t these be documentation comments? https://www.emojicode.org/ docs/reference/documentation.html" (https://github.com/algorithm-archivists/algorithm-archive/pull/ 471#discussion_r223291281) This is the commit message #5: Add error as a potential return value for DFS RI Binary To be able to return an error and to apply suggestion by thbwd (https://github.com/algorithm-archivists/algorithm-archive/pull/ 471#discussion_r223597592) the following changes were made: - Declare an enum to hold the error value. - Rewrite the Depth-First Search Recursive Inorder Binary method to return an optional holding the enum and adjust the returns according to that. - Call the rewritten method and check, if the returned optional holds error. If it does: Print out the error message defined in the enum. Signed-off-by: Julian Schacher <[email protected]>
1 parent 035c4c4 commit 0287fb1

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
🦃 ⏹ 🍇
2+
🔘 ⏫
3+
4+
❗️ 🔡 ➡️ 🔡 🍇
5+
↪️ 🐕 🙌 🆕⏹⏫❗️ 🍇
6+
↩️ 🔤The given tree is not binary!🔤
7+
🍉
8+
↩️ 🔤🔤
9+
🍉
10+
🍉
11+
12+
🐇 🌲 🍇
13+
🖍🆕 id 🔢
14+
🖍🆕 children 🍨🐚🌲🍆
15+
16+
🆕 depth_count 🔢 children_count 🔢 🍇
17+
1 ➡️ 🖍id
18+
🍨🍆 ➡️ 🖍children
19+
20+
🌌🐕 depth_count children_count❗️
21+
🍉
22+
23+
🔐 🆕 ⭐️ given_id 🔢 depth_count 🔢 children_count 🔢 🍇
24+
given_id ➡️ 🖍id
25+
🍨🍆 ➡️ 🖍children
26+
27+
🌌🐕 depth_count children_count❗️
28+
🍉
29+
30+
❗️ 🆔 ➡️ 🔢 🍇
31+
↩️ id
32+
🍉
33+
34+
❗️ 🧒 ➡️ 🍨🐚🌲🍆 🍇
35+
↩️ children
36+
🍉
37+
38+
📗 Depth-First Search Recursive pre-order 📗
39+
❗️ 🌀 🍇
40+
😀 🔡 id 10❗️❗️
41+
42+
🔂 child children 🍇
43+
🌀 child❗️
44+
🍉
45+
🍉
46+
47+
📗 Depth-First Search Recursive post-order 📗
48+
❗️ 🍥 🍇
49+
🔂 child children 🍇
50+
🍥 child❗️
51+
🍉
52+
53+
😀 🔡 id 10❗️❗️
54+
🍉
55+
56+
📗
57+
Depth-First Search Recursive Inorder Binary
58+
This assumes only 2 children.
59+
📗
60+
❗️ 🍭 ➡️ 🍬⏹ 🍇
61+
↪️ 🐔 children❗️ ▶️ 2 🍇
62+
↩️ 🆕⏹⏫❗️
63+
🍉
64+
65+
↪️ 🐔 children❗️ ▶️ 0 🍇
66+
🍭🐽 children 0❗️❗️
67+
😀 🔡 id 10❗️❗️
68+
🍭🐽 children 1❗️❗️
69+
🍉
70+
🙅 🍇
71+
😀 🔡 id 10❗️❗️
72+
🍉
73+
↩️ 🤷‍♀️
74+
🍉
75+
76+
📗 Depth-First Search Stack 📗
77+
❗️ 🥞 🍇
78+
🍨 🐕 🍆 ➡️ stack
79+
80+
🔁 ❎ 🐔 stack❗️ 🙌 0❗️ 🍇
81+
🐽 stack 🐔 stack❗️ ➖ 1❗️ ➡️ temp
82+
🐨 stack 🐔 stack❗️ ➖ 1❗️
83+
84+
😀 🔡 🆔 temp❗️ 10❗️❗️
85+
86+
🧒 temp❗️ ➡️ temp_children
87+
🔂 child temp_children 🍇
88+
🐻 stack child❗️
89+
🍉
90+
🍉
91+
🍉
92+
93+
📗 Breadth-First Search Queue 📗
94+
❗️ 🏢 🍇
95+
🍨 🐕 🍆 ➡️ queue
96+
97+
🔁 ❎ 🐔 queue❗️ 🙌 0❗️ 🍇
98+
🐽 queue 0❗️ ➡️ temp
99+
🐨 queue 0❗️
100+
101+
😀 🔡 🆔 temp❗️ 10❗️❗️
102+
103+
🧒 temp❗️ ➡️ temp_children
104+
🔂 child temp_children 🍇
105+
🐻 queue child❗️
106+
🍉
107+
🍉
108+
🍉
109+
110+
🔐 ❗️ 🌌 depth_count 🔢 children_count 🔢 🍇
111+
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
112+
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
113+
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 children_count❗️❗️
114+
🍉
115+
🍉
116+
🍉
117+
🍉
118+
119+
🏁 🍇
120+
🆕🌲🆕 3 3❗️ ➡️ tree
121+
😀 🔤Tree Traversal🔤️❗️
122+
😀 🔤🌀 - Depth-First Search Recursive pre-order🔤❗️
123+
🌀tree❗️
124+
😀 🔤🍥 - Depth-First Search Recursive post-order🔤❗️
125+
🍥tree❗️
126+
😀 🔤🥞 - Depth-First Search Stack🔤❗️
127+
🥞tree❗️
128+
😀 🔤🏢 - Breadth-First Search Queue🔤❗️
129+
🏢tree❗️
130+
131+
😀 🔤🍭 - Depth-First Search Recursive Inorder Binary - Error🔤❗️
132+
💭 Calling the Depth-First Search Recursive Inorder Binary method here does
133+
💭 result in an error, since "tree" is not a binary tree.
134+
️↪️ 🍭tree❗️ ➡️ return 🍇
135+
😀 🔡return❗❗️️
136+
🍉
137+
138+
🆕🌲🆕 3 2❗️ ➡️ binary_tree
139+
😀 🔤🍭 - Depth-First Search Recursive Inorder Binary🔤❗️
140+
️↪️ 🍭binary_tree❗️ ➡️ return 🍇
141+
😀 🔡return❗❗️️
142+
🍉
143+
🍉

contents/tree_traversal/tree_traversal.md

+14
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:27-34, 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:36-43, 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:45-62, 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:64-79, 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:81-96, 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)