File tree 1 file changed +14
-9
lines changed
chapters/tree_traversal/code/julia
1 file changed +14
-9
lines changed Original file line number Diff line number Diff line change @@ -25,20 +25,20 @@ function DFS_recursive_postorder(n::Node)
25
25
println (n. ID)
26
26
end
27
27
28
- # This assumes only 2 children
28
+ # This assumes only 2 children, but accounts for other possibilities
29
29
function DFS_recursive_inorder_btree (n:: Node )
30
30
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 ])
38
33
println (n. ID)
34
+ DFS_recursive_inorder_btree (n. children[2 ])
35
+ elseif (length (n. children) == 1 )
39
36
DFS_recursive_inorder_btree (n. children[1 ])
40
- else
41
37
println (n. ID)
38
+ elseif (length (n. children) == 0 )
39
+ println (n. ID)
40
+ else
41
+ println (" Not a binary tree!" )
42
42
end
43
43
end
44
44
@@ -96,6 +96,11 @@ function main()
96
96
97
97
println (" Using queue-based BFS:" )
98
98
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)
99
104
end
100
105
101
106
main ()
You can’t perform that action at this time.
0 commit comments