diff --git a/contents/tree_traversal/code/emojicode/tree_traversal.emojic b/contents/tree_traversal/code/emojicode/tree_traversal.emojic new file mode 100644 index 000000000..c7fe418ed --- /dev/null +++ b/contents/tree_traversal/code/emojicode/tree_traversal.emojic @@ -0,0 +1,143 @@ +πŸ¦ƒ ⏹ πŸ‡ + πŸ”˜ ⏫ + + ❗️ πŸ”‘ ➑️ πŸ”‘ πŸ‡ + β†ͺ️ πŸ• πŸ™Œ πŸ†•βΉβ«β—οΈ πŸ‡ + ↩️ πŸ”€The given tree is not binary!πŸ”€ + πŸ‰ + ↩️ πŸ”€πŸ”€ + πŸ‰ +πŸ‰ + +πŸ‡ 🌲 πŸ‡ + πŸ–πŸ†• id πŸ”’ + πŸ–πŸ†• children πŸ¨πŸšπŸŒ²πŸ† + + πŸ†• depth_count πŸ”’ children_count πŸ”’ πŸ‡ + 1 ➑️ πŸ–id + πŸ¨πŸ† ➑️ πŸ–children + + πŸŒŒπŸ• depth_count children_count❗️ + πŸ‰ + + πŸ” πŸ†• ⭐️ given_id πŸ”’ depth_count πŸ”’ children_count πŸ”’ πŸ‡ + given_id ➑️ πŸ–id + πŸ¨πŸ† ➑️ πŸ–children + + πŸŒŒπŸ• depth_count children_count❗️ + πŸ‰ + + ❗️ πŸ†” ➑️ πŸ”’ πŸ‡ + ↩️ id + πŸ‰ + + ❗️ πŸ§’ ➑️ πŸ¨πŸšπŸŒ²πŸ† πŸ‡ + ↩️ children + πŸ‰ + + πŸ“— Depth-First Search Recursive pre-order πŸ“— + ❗️ πŸŒ€ πŸ‡ + πŸ˜€ πŸ”‘ id 10❗️❗️ + + πŸ”‚ child children πŸ‡ + πŸŒ€ child❗️ + πŸ‰ + πŸ‰ + + πŸ“— Depth-First Search Recursive post-order πŸ“— + ❗️ πŸ₯ πŸ‡ + πŸ”‚ child children πŸ‡ + πŸ₯ child❗️ + πŸ‰ + + πŸ˜€ πŸ”‘ id 10❗️❗️ + πŸ‰ + + πŸ“— + Depth-First Search Recursive Inorder Binary + This assumes only 2 children. + πŸ“— + ❗️ 🍭 ➑️ 🍬⏹ πŸ‡ + β†ͺ️ πŸ” children❗️ ▢️ 2 πŸ‡ + ↩️ πŸ†•βΉβ«β—οΈ + πŸ‰ + + β†ͺ️ πŸ” children❗️ ▢️ 0 πŸ‡ + 🍭🐽 children 0❗️❗️ + πŸ˜€ πŸ”‘ id 10❗️❗️ + 🍭🐽 children 1❗️❗️ + πŸ‰ + πŸ™… πŸ‡ + πŸ˜€ πŸ”‘ id 10❗️❗️ + πŸ‰ + ↩️ πŸ€·β€β™€οΈ + πŸ‰ + + πŸ“— Depth-First Search Stack πŸ“— + ❗️ πŸ₯ž πŸ‡ + 🍨 πŸ• πŸ† ➑️ stack + + πŸ” ❎ πŸ” stack❗️ πŸ™Œ 0❗️ πŸ‡ + 🐽 stack πŸ” stack❗️ βž– 1❗️ ➑️ temp + 🐨 stack πŸ” stack❗️ βž– 1❗️ + + πŸ˜€ πŸ”‘ πŸ†” temp❗️ 10❗️❗️ + + πŸ§’ temp❗️ ➑️ temp_children + πŸ”‚ child temp_children πŸ‡ + 🐻 stack child❗️ + πŸ‰ + πŸ‰ + πŸ‰ + + πŸ“— Breadth-First Search Queue πŸ“— + ❗️ 🏒 πŸ‡ + 🍨 πŸ• πŸ† ➑️ queue + + πŸ” ❎ πŸ” queue❗️ πŸ™Œ 0❗️ πŸ‡ + 🐽 queue 0❗️ ➑️ temp + 🐨 queue 0❗️ + + πŸ˜€ πŸ”‘ πŸ†” temp❗️ 10❗️❗️ + + πŸ§’ temp❗️ ➑️ temp_children + πŸ”‚ child temp_children πŸ‡ + 🐻 queue child❗️ + πŸ‰ + πŸ‰ + πŸ‰ + + πŸ” ❗️ 🌌 depth_count πŸ”’ children_count πŸ”’ πŸ‡ + β†ͺ️ ❎ depth_count β—€οΈπŸ™Œ 1❗️ πŸ‡ + πŸ”‚ i πŸ†•β©β© 0 children_count❗️ πŸ‡ + 🐻 children πŸ†•πŸŒ²β­οΈ 🀜id βœ–οΈ 10 βž• i βž• 1πŸ€› 🀜depth_count βž– 1πŸ€› children_count❗️❗️ + πŸ‰ + πŸ‰ + πŸ‰ +πŸ‰ + +🏁 πŸ‡ + πŸ†•πŸŒ²πŸ†• 3 3❗️ ➑️ tree + πŸ˜€ πŸ”€Tree TraversalπŸ”€οΈβ—οΈ + πŸ˜€ πŸ”€πŸŒ€ - Depth-First Search Recursive pre-orderπŸ”€β—οΈ + πŸŒ€tree❗️ + πŸ˜€ πŸ”€πŸ₯ - Depth-First Search Recursive post-orderπŸ”€β—οΈ + πŸ₯tree❗️ + πŸ˜€ πŸ”€πŸ₯ž - Depth-First Search StackπŸ”€β—οΈ + πŸ₯žtree❗️ + πŸ˜€ πŸ”€πŸ’ - Breadth-First Search QueueπŸ”€β—οΈ + 🏒tree❗️ + + πŸ˜€ πŸ”€πŸ­ - Depth-First Search Recursive Inorder Binary - ErrorπŸ”€β—οΈ + πŸ’­ Calling the Depth-First Search Recursive Inorder Binary method here does + πŸ’­ result in an error, since "tree" is not a binary tree. + ️β†ͺ️ 🍭tree❗️ ➑️ return πŸ‡ + πŸ˜€ πŸ”‘return❗❗️️ + πŸ‰ + + πŸ†•πŸŒ²πŸ†• 3 2❗️ ➑️ binary_tree + πŸ˜€ πŸ”€πŸ­ - Depth-First Search Recursive Inorder BinaryπŸ”€β—οΈ + ️β†ͺ️ 🍭binary_tree❗️ ➑️ return πŸ‡ + πŸ˜€ πŸ”‘return❗❗️️ + πŸ‰ +πŸ‰ diff --git a/contents/tree_traversal/tree_traversal.md b/contents/tree_traversal/tree_traversal.md index d2ccba18a..0b27315f5 100644 --- a/contents/tree_traversal/tree_traversal.md +++ b/contents/tree_traversal/tree_traversal.md @@ -36,6 +36,8 @@ As a note, a `node` struct is not necessary in javascript, so this is an example [import:5-8, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import:24-27, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import:1-3, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %} 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 [import:10-15, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import:290-314, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import:27-34, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %} 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 [import:17-22, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import:316-344, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import:36-43, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %}

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

@@ -212,6 +220,8 @@ In code, it looks like this: [import:40-49, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import:398-445, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import:64-79, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %} 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 [import:51-60, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import:447-498, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import:81-96, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %} ## Example Code @@ -300,6 +312,8 @@ The code snippets were taken from this [Scratch project](https://scratch.mit.edu [import, lang:"go"](code/golang/treetraversal.go) {% sample lang="asm-x64" %} [import, lang:"asm-x64"](code/asm-x64/tree_traversal.s) +{% sample lang="emojic" %} +[import, lang:"emojicode"](code/emojicode/tree_traversal.emojic) {% endmethod %}