Skip to content

Commit b9cc37e

Browse files
leiosjiegillet
authored andcommitted
DFS_inorder julia update for tree traversal (#172)
* modifying Julia DFS inorder function to allow for different tree configurations.
1 parent f344b76 commit b9cc37e

File tree

1 file changed

+14
-9
lines changed
  • chapters/tree_traversal/code/julia

1 file changed

+14
-9
lines changed

chapters/tree_traversal/code/julia/Tree.jl

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ function DFS_recursive_postorder(n::Node)
2525
println(n.ID)
2626
end
2727

28-
# This assumes only 2 children
28+
# This assumes only 2 children, but accounts for other possibilities
2929
function DFS_recursive_inorder_btree(n::Node)
3030

31-
if (length(n.children) > 2)
32-
println("Not a binary tree!")
33-
exit(1)
34-
end
35-
36-
if (length(n.children) > 0)
37-
DFS_recursive_inorder_btree(n.children[0])
31+
if (length(n.children) == 2)
32+
DFS_recursive_inorder_btree(n.children[1])
3833
println(n.ID)
34+
DFS_recursive_inorder_btree(n.children[2])
35+
elseif (length(n.children) == 1)
3936
DFS_recursive_inorder_btree(n.children[1])
40-
else
4137
println(n.ID)
38+
elseif (length(n.children) == 0)
39+
println(n.ID)
40+
else
41+
println("Not a binary tree!")
4242
end
4343
end
4444

@@ -96,6 +96,11 @@ function main()
9696

9797
println("Using queue-based BFS:")
9898
BFS_queue(root);
99+
100+
println("Creating binary tree to test in-order traversal.")
101+
root_binary = create_tree(3,2)
102+
println("Using In-order DFS:")
103+
DFS_recursive_inorder_btree(root_binary)
99104
end
100105

101106
main()

0 commit comments

Comments
 (0)