Skip to content

Commit 50a8b94

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 2nd commit message: 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 3rd commit message: 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 4th commit message: 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 5th commit message: 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 <jspp@posteo.net>
1 parent e00884b commit 50a8b94

File tree

2 files changed

+157
-0
lines changed

2 files changed

+157
-0
lines changed
Lines changed: 143 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ As a note, a `node` struct is not necessary in javascript, so this is an example
3636
[import:5-8, lang:"go"](code/golang/treetraversal.go)
3737
{% sample lang="asm-x64" %}
3838
[import:24-27, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
39+
{% sample lang="emojic" %}
40+
[import:1-3, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
3941
{% endmethod %}
4042

4143
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:
@@ -74,6 +76,8 @@ Because of this, the most straightforward way to traverse the tree might be recu
7476
[import:10-15, lang:"go"](code/golang/treetraversal.go)
7577
{% sample lang="asm-x64" %}
7678
[import:290-314, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
79+
{% sample lang="emojic" %}
80+
[import:27-34, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
7781
{% endmethod %}
7882

7983
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:
@@ -120,6 +124,8 @@ Now, in this case the first element searched through is still the root of the tr
120124
[import:17-22, lang:"go"](code/golang/treetraversal.go)
121125
{% sample lang="asm-x64" %}
122126
[import:316-344, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
127+
{% sample lang="emojic" %}
128+
[import:36-43, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
123129
{% endmethod %}
124130

125131
<p>
@@ -161,6 +167,8 @@ In this case, the first node visited is at the bottom of the tree and moves up t
161167
[import:24-38, lang:"go"](code/golang/treetraversal.go)
162168
{% sample lang="asm-x64" %}
163169
[import:346-396, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
170+
{% sample lang="emojic" %}
171+
[import:45-62, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
164172
{% endmethod %}
165173

166174
<p>
@@ -212,6 +220,8 @@ In code, it looks like this:
212220
[import:40-49, lang:"go"](code/golang/treetraversal.go)
213221
{% sample lang="asm-x64" %}
214222
[import:398-445, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
223+
{% sample lang="emojic" %}
224+
[import:64-79, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
215225
{% endmethod %}
216226

217227
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:
@@ -255,6 +265,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
255265
[import:51-60, lang:"go"](code/golang/treetraversal.go)
256266
{% sample lang="asm-x64" %}
257267
[import:447-498, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
268+
{% sample lang="emojic" %}
269+
[import:81-96, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
258270
{% endmethod %}
259271

260272
## Example Code
@@ -300,6 +312,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu
300312
[import, lang:"go"](code/golang/treetraversal.go)
301313
{% sample lang="asm-x64" %}
302314
[import, lang:"asm-x64"](code/asm-x64/tree_traversal.s)
315+
{% sample lang="emojic" %}
316+
[import, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
303317
{% endmethod %}
304318

305319

0 commit comments

Comments
 (0)