Skip to content

Commit 65fa1dd

Browse files
authored
Merge pull request #573 from barskern/simplify-rust-impls
Use pattern matching in tree traversal (Rust)
2 parents 6d718f7 + 94a96ae commit 65fa1dd

File tree

2 files changed

+21
-15
lines changed

2 files changed

+21
-15
lines changed

contents/tree_traversal/code/rust/tree.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@ fn dfs_recursive_postorder(n: &Node) {
2323
}
2424

2525
fn dfs_recursive_inorder_btree(n: &Node) {
26-
if n.children.len() == 2 {
27-
dfs_recursive_inorder_btree(&n.children[1]);
28-
println!("{}", n.value);
29-
dfs_recursive_inorder_btree(&n.children[0]);
30-
} else if n.children.len() == 1 {
31-
dfs_recursive_inorder_btree(&n.children[0]);
32-
println!("{}", n.value);
33-
} else if n.children.len() == 0 {
34-
println!("{}", n.value);
35-
} else {
36-
println!("This is not a binary tree.");
26+
match &n.children[..] {
27+
[left, right] => {
28+
dfs_recursive_inorder_btree(left);
29+
println!("{}", n.value);
30+
dfs_recursive_inorder_btree(right);
31+
}
32+
[left] => {
33+
dfs_recursive_inorder_btree(left);
34+
println!("{}", n.value);
35+
}
36+
[] => println!("{}", n.value),
37+
_ => println!("This is not a binary tree."),
3738
}
3839
}
3940

@@ -76,14 +77,19 @@ fn create_tree(num_row: u64, num_child: u64) -> Node {
7677

7778
fn main() {
7879
let root = create_tree(2, 3);
80+
7981
println!("Recursive DFS:");
8082
dfs_recursive(&root);
83+
8184
println!("Stack DFS:");
8285
dfs_stack(&root);
86+
8387
println!("Queue BFS:");
8488
bfs_queue(&root);
89+
8590
println!("Recursive post-order DFS:");
8691
dfs_recursive_postorder(&root);
92+
8793
println!("Recursive in-order DFS BTree:");
8894
let root_binary = create_tree(3, 2);
8995
dfs_recursive_inorder_btree(&root_binary);

contents/tree_traversal/tree_traversal.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Now, in this case the first element searched through is still the root of the tr
111111
<img class="center" src="code/scratch/dfs-post.svg" width="300" />
112112
</p>
113113
{% sample lang="rs"%}
114-
[import:17-23, lang:"rust"](code/rust/tree.rs)
114+
[import:17-24, lang:"rust"](code/rust/tree.rs)
115115
{% sample lang="hs"%}
116116
[import:9-10, lang:"haskell"](code/haskell/TreeTraversal.hs)
117117
{% sample lang="swift"%}
@@ -154,7 +154,7 @@ In this case, the first node visited is at the bottom of the tree and moves up t
154154
<img class="center" src="code/scratch/dfs-in.svg" width="300" />
155155
</p>
156156
{% sample lang="rs"%}
157-
[import:25-38, lang:"rust"](code/rust/tree.rs)
157+
[import:25-40, lang:"rust"](code/rust/tree.rs)
158158
{% sample lang="hs"%}
159159
[import:12-16, lang:"haskell"](code/haskell/TreeTraversal.hs)
160160
{% sample lang="swift"%}
@@ -207,7 +207,7 @@ In code, it looks like this:
207207
<img class="center" src="code/scratch/dfs-stack.svg" width="400" />
208208
</p>
209209
{% sample lang="rs"%}
210-
[import:40-47, lang:"rust"](code/rust/tree.rs)
210+
[import:41-48, lang:"rust"](code/rust/tree.rs)
211211
{% sample lang="hs"%}
212212
[import:18-22, lang:"haskell"](code/haskell/TreeTraversal.hs)
213213
{% sample lang="swift"%}
@@ -252,7 +252,7 @@ And this is exactly what Breadth-First Search (BFS) does! On top of that, it can
252252
<img class="center" src="code/scratch/bfs.svg" width="400" />
253253
</p>
254254
{% sample lang="rs"%}
255-
[import:49-57, lang:"rust"](code/rust/tree.rs)
255+
[import:50-58, lang:"rust"](code/rust/tree.rs)
256256
{% sample lang="hs"%}
257257
[import:24-28, lang:"haskell"](code/haskell/TreeTraversal.hs)
258258
{% sample lang="swift"%}

0 commit comments

Comments
 (0)