Skip to content

Commit 6139820

Browse files
committed
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)
1 parent 10687ff commit 6139820

File tree

2 files changed

+15
-15
lines changed

2 files changed

+15
-15
lines changed

contents/tree_traversal/code/emojicode/tree_traversal.emojic

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,14 @@
66
1 ➡️ 🖍id
77
🍨🍆 ➡️ 🖍children
88

9-
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
10-
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
11-
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 children_count❗️❗️
12-
🍉
13-
🍉
9+
🌌🐕 depth_count children_count❗️
1410
🍉
1511

1612
🔐 🆕 ⭐️ given_id 🔢 depth_count 🔢 children_count 🔢 🍇
1713
given_id ➡️ 🖍id
1814
🍨🍆 ➡️ 🖍children
1915

20-
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
21-
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
22-
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 children_count❗️❗️
23-
🍉
24-
🍉
16+
🌌🐕 depth_count children_count❗️
2517
🍉
2618

2719
❗️ 🆔 ➡️ 🔢 🍇
@@ -100,6 +92,14 @@
10092
🍉
10193
🍉
10294
🍉
95+
96+
🔐 ❗️ 🌌 depth_count 🔢 children_count 🔢 🍇
97+
↪️ ❎ depth_count ◀️🙌 1❗️ 🍇
98+
🔂 i 🆕⏩⏩ 0 children_count❗️ 🍇
99+
🐻 children 🆕🌲⭐️ 🤜id ✖️ 10 ➕ i ➕ 1🤛 🤜depth_count ➖ 1🤛 children_count❗️❗️
100+
🍉
101+
🍉
102+
🍉
103103
🍉
104104

105105
🏁 🍇

contents/tree_traversal/tree_traversal.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ Because of this, the most straightforward way to traverse the tree might be recu
7373
{% sample lang="go" %}
7474
[import:10-15, lang:"golang"](code/golang/treetraversal.go)
7575
{% sample lang="emojic" %}
76-
[import:35-42, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
76+
[import:27-34, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
7777
{% endmethod %}
7878

7979
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:
@@ -119,7 +119,7 @@ Now, in this case the first element searched through is still the root of the tr
119119
{% sample lang="go" %}
120120
[import:17-22, lang:"golang"](code/golang/treetraversal.go)
121121
{% sample lang="emojic" %}
122-
[import:44-51, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
122+
[import:36-43, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
123123
{% endmethod %}
124124

125125
<p>
@@ -160,7 +160,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
160160
{% sample lang="go" %}
161161
[import:24-38, lang:"golang"](code/golang/treetraversal.go)
162162
{% sample lang="emojic" %}
163-
[import:53-68, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
163+
[import:45-60, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
164164
{% endmethod %}
165165

166166
<p>
@@ -211,7 +211,7 @@ In code, it looks like this:
211211
{% sample lang="go" %}
212212
[import:40-49, lang:"golang"](code/golang/treetraversal.go)
213213
{% sample lang="emojic" %}
214-
[import:70-85, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
214+
[import:62-77, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
215215
{% endmethod %}
216216

217217
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:
@@ -254,7 +254,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
254254
{% sample lang="go" %}
255255
[import:51-60, lang:"golang"](code/golang/treetraversal.go)
256256
{% sample lang="emojic" %}
257-
[import:87-102, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
257+
[import:79-94, lang:"emojicode"](code/emojicode/tree_traversal.emojic)
258258
{% endmethod %}
259259

260260
## Example Code

0 commit comments

Comments
 (0)