Skip to content

Adding the Missing Rust Tree Traversal Functions #326

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
Aug 4, 2018
Merged
Show file tree
Hide file tree
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
29 changes: 28 additions & 1 deletion contents/tree_traversal/code/rust/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ fn dfs_recursive(n: &Node) {
}
}

fn dfs_recursive_postorder(n: &Node) {
for child in &n.children {
dfs_recursive(child);
}

println!("{}", n.value);
}

fn dfs_recursive_inorder_btree(n: &Node) {
if n.children.len() == 2{
dfs_recursive_inorder_btree(&n.children[1]);
println!("{}", n.value);
dfs_recursive_inorder_btree(&n.children[0]);
} else if n.children.len() == 1 {
dfs_recursive_inorder_btree(&n.children[0]);
println!("{}", n.value);
} else if n.children.len() == 0 {
println!("{}", n.value);
} else {
println!("This is not a binary tree.");
}
}

fn dfs_stack(n: &Node) {
let mut stack = vec![n];

Expand Down Expand Up @@ -46,11 +69,15 @@ fn create_tree(num_row: u64, num_child: u64) -> Node {
}

fn main() {
let root = create_tree(3,3);
let root = create_tree(3,2);
println!("Recursive DFS:");
dfs_recursive(&root);
println!("Stack DFS:");
dfs_stack(&root);
println!("Queue BFS:");
bfs_queue(&root);
println!("Recursive PostOrder DFS: ");
dfs_recursive_postorder(&root);
println!("Recursive DFS BTree:");
dfs_recursive_inorder_btree(&root);
}
10 changes: 4 additions & 6 deletions contents/tree_traversal/tree_traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ This has not been implemented in your chosen language, so here is the Julia code
<img class="center" src="code/scratch/dfs-post.svg" width="300" />
</p>
{% sample lang="rs"%}
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:17-23, lang:"rust"](code/rust/tree.rs)
{% sample lang="hs"%}
[import:8-9, lang:"haskell"](code/haskell/TreeTraversal.hs)
{% endmethod %}
Expand Down Expand Up @@ -121,8 +120,7 @@ This has not been implemented in your chosen language, so here is the Julia code
<img class="center" src="code/scratch/dfs-in.svg" width="300" />
</p>
{% sample lang="rs"%}
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:25-38, lang:"rust"](code/rust/tree.rs)
{% sample lang="hs"%}
[import:11-15, lang:"haskell"](code/haskell/TreeTraversal.hs)
{% endmethod %}
Expand Down Expand Up @@ -163,7 +161,7 @@ In code, it looks like this:
<img class="center" src="code/scratch/dfs-stack.svg" width="400" />
</p>
{% sample lang="rs"%}
[import:17-24, lang:"rust"](code/rust/tree.rs)
[import:40-47, lang:"rust"](code/rust/tree.rs)
{% sample lang="hs"%}
This has not been implemented in your chosen language, so here is the Julia code
[import:45-56, lang:"julia"](code/julia/Tree.jl)
Expand Down Expand Up @@ -197,7 +195,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
<img class="center" src="code/scratch/bfs.svg" width="400" />
</p>
{% sample lang="rs"%}
[import:26-34, lang:"rust"](code/rust/tree.rs)
[import:49-57, lang:"rust"](code/rust/tree.rs)
{% sample lang="hs"%}
[import:17-20, lang:"haskell"](code/haskell/TreeTraversal.hs)
{% endmethod %}
Expand Down