diff --git a/chapters/FFT/cooley_tukey.md b/chapters/FFT/cooley_tukey.md index 497a1ab58..080c774da 100644 --- a/chapters/FFT/cooley_tukey.md +++ b/chapters/FFT/cooley_tukey.md @@ -72,7 +72,7 @@ For some reason, though, putting code to this transformation really helped me fi {% sample lang="jl" %} [import:2-11, lang:"julia"](code/julia/fft.jl) {% sample lang="c" %} -[import:7-19, lang:"c_cpp"](code/c/fft.c) +[import:8-19, lang:"c_cpp"](code/c/fft.c) {% sample lang="cpp" %} [import:2-11, lang:"julia"](code/julia/fft.jl) {% sample lang="hs" %} @@ -117,7 +117,7 @@ In the end, the code looks like: {% sample lang="jl" %} [import:14-31, lang:"julia"](code/julia/fft.jl) {% sample lang="c" %} -[import:21-40, lang:"c_cpp"](code/c/fft.c) +[import:20-39, lang:"c_cpp"](code/c/fft.c) {% sample lang="cpp" %} [import:27-57, lang:"c_cpp"](code/c++/fft.cpp) {% sample lang="hs" %} diff --git a/chapters/sorting_searching/bogo/bogo_sort.md b/chapters/sorting_searching/bogo/bogo_sort.md index ca5c77516..6a62e931c 100644 --- a/chapters/sorting_searching/bogo/bogo_sort.md +++ b/chapters/sorting_searching/bogo/bogo_sort.md @@ -18,9 +18,9 @@ In code, it looks something like this: {% sample lang="cs" %} [import:9-15, lang:"csharp"](code/cs/BogoSort.cs) {% sample lang="clj" %} -[import:2-10, lang:"clojure"](code/clojure/bogo.clj) +[import:2-11, lang:"clojure"](code/clojure/bogo.clj) {% sample lang="c" %} -[import:4-27, lang:"c_cpp"](code/c/bogo_sort.c) +[import:4-29, lang:"c_cpp"](code/c/bogo_sort.c) {% sample lang="java" %} [import:2-17, lang:"java"](code/java/bogo.java) {% sample lang="js" %} diff --git a/chapters/sorting_searching/bubble/bubble_sort.md b/chapters/sorting_searching/bubble/bubble_sort.md index 684b3fbce..073037515 100644 --- a/chapters/sorting_searching/bubble/bubble_sort.md +++ b/chapters/sorting_searching/bubble/bubble_sort.md @@ -13,7 +13,7 @@ This means that we need to go through the vector $$\mathcal{O}(n^2)$$ times with {% sample lang="cs" %} [import:9-27, lang:"csharp"](code/cs/BubbleSort.cs) {% sample lang="c" %} -[import:3-21, lang:"c_cpp"](code/c/bubble_sort.c) +[import:4-22, lang:"c_cpp"](code/c/bubble_sort.c) {% sample lang="java" %} [import:2-12, lang:"java"](code/java/bubble.java) {% sample lang="js" %} diff --git a/chapters/tree_traversal/code/python/Tree_example.py b/chapters/tree_traversal/code/python/Tree_example.py index 7027dbd8b..8f681ab5a 100644 --- a/chapters/tree_traversal/code/python/Tree_example.py +++ b/chapters/tree_traversal/code/python/Tree_example.py @@ -1,9 +1,9 @@ class Node: - def __init__(self): self.data = None self.children = [] + def create_tree(node, num_row, num_child): node.data = num_row @@ -14,12 +14,37 @@ def create_tree(node, num_row, num_child): return node + def DFS_recursive(node): - if len(node.children) > 0: + if node.data != None: + print(node.data) + + for child in node.children: + DFS_recursive(child) + + +def DFS_recursive_postorder(node): + for child in node.children: + DFS_recursive(child) + + if node.data != None: print(node.data) - for child in node.children: - DFS_recursive(child) + +# This assumes only 2 children, but accounts for other possibilities +def DFS_recursive_inorder_btree(node): + if (len(node.children) == 2): + DFS_recursive_inorder_btree(node.children[1]) + print(node.data) + DFS_recursive_inorder_btree(node.children[2]) + elif (len(node.children) == 1): + DFS_recursive_inorder_btree(node.children[1]) + print(node.data) + elif (len(node.children) == 0): + print(node.data) + else: + print("Not a binary tree!") + def DFS_stack(node): stack = [] @@ -34,6 +59,7 @@ def DFS_stack(node): for child in temp.children: stack.append(child) + def BFS_queue(node): queue = [] queue.append(node) @@ -47,6 +73,7 @@ def BFS_queue(node): for child in temp.children: queue.append(child) + def main(): tree = create_tree(Node(), 3, 3) @@ -59,5 +86,7 @@ def main(): print("Queue:") BFS_queue(tree) -main() + +if __name__ == '__main__': + main() diff --git a/chapters/tree_traversal/tree_traversal.md b/chapters/tree_traversal/tree_traversal.md index ebed88287..bb97b4692 100644 --- a/chapters/tree_traversal/tree_traversal.md +++ b/chapters/tree_traversal/tree_traversal.md @@ -17,7 +17,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac This has not been implemented in your chosen language, so here is the Julia code [import:3-7, lang:"julia"](code/julia/Tree.jl) {% sample lang="py" %} -[import:1-5, lang:"python"](code/python/Tree_example.py) +[import:1-4, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:3-7, lang:"julia"](code/julia/Tree.jl) @@ -43,7 +43,7 @@ Because of this, the most straightforward way to traverse the tree might be recu {% sample lang="js" %} [import:12-15, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} -[import:7-15, lang:"python"](code/python/Tree_example.py) +[import:18-23, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:9-16, lang:"julia"](code/julia/Tree.jl) @@ -79,8 +79,7 @@ This has not been implemented in your chosen language, so here is the Julia code {% sample lang="js" %} [import:17-20, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} -This has not been implemented in your chosen language, so here is the Julia code -[import:18-26, lang:"julia"](code/julia/Tree.jl) +[import:26-31, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:18-26, lang:"julia"](code/julia/Tree.jl) @@ -112,8 +111,7 @@ This has not been implemented in your chosen language, so here is the Julia code {% sample lang="js" %} [import:22-34, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} -This has not been implemented in your chosen language, so here is the Julia code -[import:28-43, lang:"julia"](code/julia/Tree.jl) +[import:34-46, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:28-43, lang:"julia"](code/julia/Tree.jl) @@ -154,7 +152,7 @@ In code, it looks like this: {% sample lang="js" %} [import:36-43, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} -[import:24-35, lang:"python"](code/python/Tree_example.py) +[import:49-60, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:45-56, lang:"julia"](code/julia/Tree.jl) @@ -187,7 +185,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="js" %} [import:45-52, lang:"javascript"](code/javascript/tree.js) {% sample lang="py" %} -[import:37-48, lang:"python"](code/python/Tree_example.py) +[import:63-74, lang:"python"](code/python/Tree_example.py) {% sample lang="scratch" %} This has not been implemented in your chosen language, so here is the Julia code [import:58-69, lang:"julia"](code/julia/Tree.jl)