Skip to content

DFS_inorder julia update for tree traversal #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 29, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions chapters/tree_traversal/code/julia/Tree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ function DFS_recursive_postorder(n::Node)
println(n.ID)
end

# This assumes only 2 children
# This assumes only 2 children, but accounts for other possibilities
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])
if (length(n.children) == 2)
DFS_recursive_inorder_btree(n.children[1])
println(n.ID)
DFS_recursive_inorder_btree(n.children[2])
elseif (length(n.children) == 1)
DFS_recursive_inorder_btree(n.children[1])
else
println(n.ID)
elseif (length(n.children) == 0)
println(n.ID)
else
println("Not a binary tree!")
end
end

Expand Down Expand Up @@ -96,6 +96,11 @@ function main()

println("Using queue-based BFS:")
BFS_queue(root);

println("Creating binary tree to test in-order traversal.")
root_binary = create_tree(3,2)
println("Using In-order DFS:")
DFS_recursive_inorder_btree(root_binary)
end

main()