zig : update codes style && rust : add codes for chapter_backtracking. (#613)

* zig : update codes style

* rust : add codes for chapter_backtracking

* zig : update codes style
This commit is contained in:
sjinzh
2023-07-16 15:36:28 +08:00
committed by GitHub
parent 51a4c5089e
commit ead33ca863
17 changed files with 312 additions and 71 deletions

View File

@@ -299,5 +299,25 @@ path = "chapter_backtracking/permutations_i.rs"
name = "permutations_ii"
path = "chapter_backtracking/permutations_ii.rs"
# Run Command: cargo run --bin preorder_traversal_i_compact
[[bin]]
name = "preorder_traversal_i_compact"
path = "chapter_backtracking/preorder_traversal_i_compact.rs"
# Run Command: cargo run --bin preorder_traversal_ii_compact
[[bin]]
name = "preorder_traversal_ii_compact"
path = "chapter_backtracking/preorder_traversal_ii_compact.rs"
# Run Command: cargo run --bin preorder_traversal_iii_compact
[[bin]]
name = "preorder_traversal_iii_compact"
path = "chapter_backtracking/preorder_traversal_iii_compact.rs"
# Run Command: cargo run --bin preorder_traversal_iii_template
[[bin]]
name = "preorder_traversal_iii_template"
path = "chapter_backtracking/preorder_traversal_iii_template.rs"
[dependencies]
rand = "0.8.5"
rand = "0.8.5"

View File

@@ -0,0 +1,43 @@
/*
* File: preorder_traversal_i_compact.rs
* Created Time: 2023-07-15
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};
/* 前序遍历:例题一 */
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
if let Some(node) = root {
if node.borrow().val == 7 {
// 记录解
res.push(node.clone());
}
pre_order(res, node.borrow().left.clone());
pre_order(res, node.borrow().right.clone());
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());
// 前序遍历
let mut res = Vec::new();
pre_order(&mut res, root);
println!("\n输出所有值为 7 的节点");
let mut vals = Vec::new();
for node in res {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}

View File

@@ -0,0 +1,50 @@
/*
* File: preorder_traversal_ii_compact.rs
* Created Time: 2023-07-15
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};
/* 前序遍历:例题二 */
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
if let Some(node) = root {
// 尝试
path.push(node.clone());
if node.borrow().val == 7 {
// 记录解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());
// 前序遍历
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
println!("\n输出所有根节点到节点 7 的路径");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}

View File

@@ -0,0 +1,51 @@
/*
* File: preorder_traversal_iii_compact.rs
* Created Time: 2023-07-15
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};
/* 前序遍历:例题三 */
fn pre_order(res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>, path: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeNode>>>) {
// 剪枝
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
return;
}
if let Some(node) = root {
// 尝试
path.push(node.clone());
if node.borrow().val == 7 {
// 记录解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
// 回退
path.remove(path.len() - 1);
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());
// 前序遍历
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
println!("\n输出所有根节点到节点 7 的路径,且路径中不包含值为 3 的节点");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}

View File

@@ -0,0 +1,77 @@
/*
* File: preorder_traversal_iii_template.rs
* Created Time: 2023-07-15
* Author: sjinzh (sjinzh@gmail.com)
*/
include!("../include/include.rs");
use std::{cell::RefCell, rc::Rc};
use tree_node::{vec_to_tree, TreeNode};
/* 判断当前状态是否为解 */
fn is_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>) -> bool {
return !state.is_empty() && state.get(state.len() - 1).unwrap().borrow().val == 7;
}
/* 记录解 */
fn record_solution(state: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {
res.push(state.clone());
}
/* 判断在当前状态下,该选择是否合法 */
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {
return choice.borrow().val != 3;
}
/* 更新状态 */
fn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) {
state.push(choice);
}
/* 恢复状态 */
fn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {
state.remove(state.len() - 1);
}
/* 回溯算法:例题三 */
fn backtrack(state: &mut Vec<Rc<RefCell<TreeNode>>>, choices: &mut Vec<Rc<RefCell<TreeNode>>>, res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>) {
// 检查是否为解
if is_solution(state) {
// 记录解
record_solution(state, res);
return;
}
// 遍历所有选择
for choice in choices {
// 剪枝:检查选择是否合法
if is_valid(state, choice.clone()) {
// 尝试:做出选择,更新状态
make_choice(state, choice.clone());
// 进行下一轮选择
backtrack(state, &mut vec![choice.borrow().left.clone().unwrap(), choice.borrow().right.clone().unwrap()], res);
// 回退:撤销选择,恢复到之前的状态
undo_choice(state, choice.clone());
}
}
}
/* Driver Code */
pub fn main() {
let root = vec_to_tree([1, 7, 3, 4, 5, 6, 7].map(|x| Some(x)).to_vec());
println!("初始化二叉树");
print_util::print_tree(root.as_ref().unwrap());
// 回溯算法
let mut res = Vec::new();
backtrack(&mut Vec::new(), &mut vec![root.unwrap()], &mut res);
println!("\n输出所有根节点到节点 7 的路径,要求路径中不包含值为 3 的节点");
for path in res {
let mut vals = Vec::new();
for node in path {
vals.push(node.borrow().val)
}
println!("{:?}", vals);
}
}