diff --git a/chapters/tree_traversal/code/julia/Tree.jl b/chapters/tree_traversal/code/julia/Tree.jl index 06fefd9d7..92528bbc0 100644 --- a/chapters/tree_traversal/code/julia/Tree.jl +++ b/chapters/tree_traversal/code/julia/Tree.jl @@ -1,13 +1,11 @@ using DataStructures -# This has not been implemented in your chosen language, so here's Julia code struct Node children::Vector{Node} ID::Int64 Node(ID::Int64) = new(Vector{Node}(), ID) end -# This has not been implemented in your chosen language, so here's Julia code function DFS_recursive(n::Node) # Here we are doing something... println(n.ID) @@ -17,7 +15,6 @@ function DFS_recursive(n::Node) end end -# This has not been implemented in your chosen language, so here's Julia code function DFS_recursive_postorder(n::Node) for child in n.children @@ -28,7 +25,6 @@ function DFS_recursive_postorder(n::Node) println(n.ID) end -# This has not been implemented in your chosen language, so here's Julia code # This assumes only 2 children function DFS_recursive_inorder_btree(n::Node) @@ -46,7 +42,6 @@ function DFS_recursive_inorder_btree(n::Node) end end -# This has not been implemented in your chosen language, so here's Julia code function DFS_stack(n::Node) s = Stack(Node) push!(s, n) @@ -60,7 +55,6 @@ function DFS_stack(n::Node) end end -# This has not been implemented in your chosen language, so here's Julia code function BFS_queue(n::Node) q = Queue(Node) enqueue!(q, n) @@ -102,7 +96,6 @@ function main() println("Using queue-based BFS:") BFS_queue(root); - end main() diff --git a/chapters/tree_traversal/code/julia/Tree_example.jl b/chapters/tree_traversal/code/julia/Tree_example.jl deleted file mode 100644 index d4b65bf2d..000000000 --- a/chapters/tree_traversal/code/julia/Tree_example.jl +++ /dev/null @@ -1,102 +0,0 @@ -using DataStructures - -struct Node - children::Vector{Node} - ID::Int64 - Node(ID::Int64) = new(Vector{Node}(), ID) -end - -function DFS_recursive(n::Node) - # Here we are doing something... - println(n.ID) - - for child in n.children - DFS_recursive(child) - end -end - -function DFS_recursive_postorder(n::Node) - - for child in n.children - DFS_recursive_postorder(child) - end - - # Here we are doing something... - println(n.ID) -end - -# This assumes only 2 children -function DFS_recursive_inorder_btree(n::Node) - - if (length(n.children) > 2) - println("Not a binary tree!") - exit(1) - end - - if (length(n.children) > 0) - DFS_recursive_inorder_btree(n.children[0]) - println(n.ID) - DFS_recursive_inorder_btree(n.children[1]) - else - println(n.ID) - end -end - -function DFS_stack(n::Node) - s = Stack(Node) - push!(s, n) - - while(length(s) > 0) - println(top(s).ID) - temp = pop!(s) - for child in temp.children - push!(s, child) - end - end -end - -function BFS_queue(n::Node) - q = Queue(Node) - enqueue!(q, n) - - while(length(q) > 0) - println(front(q).ID) - temp = dequeue!(q) - for child in temp.children - enqueue!(q, child) - end - end -end - -# function to create a simple, balanced tree -function create_tree(num_row::Int64, num_child::Int64) - ret = Node(num_row) - if (num_row == 0) - return ret - end - - for i = 1:num_child - child = create_tree(num_row - 1, num_child) - push!(ret.children, child) - end - - return ret -end - -function main() - - println("Creating Tree") - root = create_tree(2,3) - - println("Using recursive DFS:") - DFS_recursive(root); - - println("Using stack-based DFS:") - DFS_stack(root); - - println("Using queue-based BFS:") - BFS_queue(root); - -end - -main() diff --git a/chapters/tree_traversal/tree_traversal.md b/chapters/tree_traversal/tree_traversal.md index 2d6859161..343ce5fd1 100644 --- a/chapters/tree_traversal/tree_traversal.md +++ b/chapters/tree_traversal/tree_traversal.md @@ -4,7 +4,7 @@ Trees are naturally recursive data structures, and because of this, we cannot ac {% method %} {% sample lang="jl" %} -[import:4-8, lang:"julia"](code/julia/Tree.jl) +[import:3-7, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} [import:15-18, lang:"c_cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} @@ -12,13 +12,15 @@ Trees are naturally recursive data structures, and because of this, we cannot ac {% sample lang="c" %} [import:5-10, lang:"c_cpp"](code/c/tree_traversal.c) {% sample lang="js" %} -[import:3-8, lang:"julia"](code/julia/Tree.jl) +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="py2" %} [import:1-5, lang:"python"](code/python2/Tree_example.py) {% sample lang="py3" %} [import:5-10, lang:"python"](code/python3/Tree_example.py) {% sample lang="scratch" %} -[import:3-8, lang:"julia"](code/julia/Tree.jl) +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="rs"%} [import:4-7, lang:"rust"](code/rust/tree.rs) {% endmethod %} @@ -27,7 +29,7 @@ Because of this, the most straightforward way to traverse the tree might be recu {% method %} {% sample lang="jl" %} -[import:11-18, lang:"julia"](code/julia/Tree.jl) +[import:9-16, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} [import:20-27, lang:"c_cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} @@ -39,9 +41,11 @@ Because of this, the most straightforward way to traverse the tree might be recu {% sample lang="py2" %} [import:8-16, lang:"python"](code/python2/Tree_example.py) {% sample lang="py3" %} -[import:10-18, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:9-16, lang:"julia"](code/julia/Tree.jl) {% sample lang="scratch" %} -[import:10-18, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:9-16, lang:"julia"](code/julia/Tree.jl) {% sample lang="rs"%} [import:9-15 lang:"rust"](code/rust/tree.rs) {% endmethod %} @@ -59,23 +63,30 @@ Now, in this case the first element searched through is still the root of the tr {% method %} {% sample lang="jl" %} -[import:21-29, lang:"julia"](code/julia/Tree.jl) +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="cs" %} [import:75-84, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) {% sample lang="c" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="js" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="py2" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="py3" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="scratch" %} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% sample lang="rs"%} -[import:20-29, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:18-26, lang:"julia"](code/julia/Tree.jl) {% endmethod %}

@@ -86,23 +97,30 @@ In this case, the first node visited is at the bottom of the tree and moves up t {% method %} {% sample lang="jl" %} -[import:32-47, lang:"julia"](code/julia/Tree.jl) +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="cs" %} [import:86-104, lang:"csharp"](code/cs/TreeMdAdditional/TreeMdAdditional.cs) {% sample lang="c" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="js" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="py2" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="py3" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="scratch" %} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% sample lang="rs"%} -[import:31-47, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:28-43, lang:"julia"](code/julia/Tree.jl) {% endmethod %}

@@ -123,7 +141,7 @@ In code, it looks like this: {% method %} {% sample lang="jl" %} -[import:50-61, lang:"julia"](code/julia/Tree.jl) +[import:45-56, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} [import:29-45, lang:"c_cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} @@ -139,7 +157,8 @@ In code, it looks like this: {% sample lang="py3" %} [import:31-45, lang:"python"](code/python3/Tree_example.py) {% sample lang="scratch" %} -[import:49-61, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:45-56, lang:"julia"](code/julia/Tree.jl) {% sample lang="rs"%} [import:17-24, lang:"rust"](code/rust/tree.rs) {% endmethod %} @@ -154,7 +173,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% method %} {% sample lang="jl" %} -[import:64-75, lang:"julia"](code/julia/Tree.jl) +[import:58-69, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} [import:47-61, lang:"c_cpp"](code/c++/tree_example.cpp) {% sample lang="cs" %} @@ -168,7 +187,8 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% sample lang="py3" %} [import:48-62, lang:"python"](code/python3/Tree_example.py) {% sample lang="scratch" %} -[import:63-75, lang:"julia"](code/julia/Tree.jl) +This has not been implemented in your chosen language, so here is the Julia code +[import:58-69, lang:"julia"](code/julia/Tree.jl) {% sample lang="rs"%} [import:26-34, lang:"rust"](code/rust/tree.rs) {% endmethod %} @@ -177,7 +197,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can {% method %} {% sample lang="jl" %} ### Julia -[import, lang:"julia"](code/julia/Tree_example.jl) +[import, lang:"julia"](code/julia/Tree.jl) {% sample lang="cpp" %} ### C++ [import, lang:"c_cpp"](code/c++/tree_example.cpp)