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 %}