Bug fixes and improvements (#1380)

* preorder, inorder, postorder -> pre-order, in-order, post-order

* Bug fixes

* Bug fixes

* Update what_is_dsa.md

* Sync zh and zh-hant versions

* Sync zh and zh-hant versions.

* Update performance_evaluation.md and time_complexity.md

* Add @khoaxuantu to the landing page.

* Sync zh and zh-hant versions

* Add @ khoaxuantu to the landing page of zh-hant and en versions.
This commit is contained in:
Yudong Jin
2024-05-31 16:39:06 +08:00
committed by GitHub
parent 39a6890b7e
commit 3f4220de81
91 changed files with 1709 additions and 181 deletions
@@ -10,7 +10,7 @@ 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>>>) {
fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<&Rc<RefCell<TreeNode>>>) {
if root.is_none() {
return;
}
@@ -19,8 +19,8 @@ fn pre_order(res: &mut Vec<Rc<RefCell<TreeNode>>>, root: Option<Rc<RefCell<TreeN
// 記錄解
res.push(node.clone());
}
pre_order(res, node.borrow().left.clone());
pre_order(res, node.borrow().right.clone());
pre_order(res, node.borrow().left.as_ref());
pre_order(res, node.borrow().right.as_ref());
}
}
@@ -32,7 +32,7 @@ pub fn main() {
// 前序走訪
let mut res = Vec::new();
pre_order(&mut res, root);
pre_order(&mut res, root.as_ref());
println!("\n輸出所有值為 7 的節點");
let mut vals = Vec::new();
@@ -13,7 +13,7 @@ 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>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
if root.is_none() {
return;
@@ -25,10 +25,10 @@ fn pre_order(
// 記錄解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// 回退
path.remove(path.len() - 1);
path.pop();
}
}
@@ -41,7 +41,7 @@ pub fn main() {
// 前序走訪
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
pre_order(&mut res, &mut path, root.as_ref());
println!("\n輸出所有根節點到節點 7 的路徑");
for path in res {
@@ -13,7 +13,7 @@ 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>>>,
root: Option<&Rc<RefCell<TreeNode>>>,
) {
// 剪枝
if root.is_none() || root.as_ref().unwrap().borrow().val == 3 {
@@ -26,10 +26,10 @@ fn pre_order(
// 記錄解
res.push(path.clone());
}
pre_order(res, path, node.borrow().left.clone());
pre_order(res, path, node.borrow().right.clone());
pre_order(res, path, node.borrow().left.as_ref());
pre_order(res, path, node.borrow().right.as_ref());
// 回退
path.remove(path.len() - 1);
path.pop();
}
}
@@ -42,7 +42,7 @@ pub fn main() {
// 前序走訪
let mut path = Vec::new();
let mut res = Vec::new();
pre_order(&mut res, &mut path, root);
pre_order(&mut res, &mut path, root.as_ref());
println!("\n輸出所有根節點到節點 7 的路徑,路徑中不包含值為 3 的節點");
for path in res {
@@ -11,7 +11,7 @@ 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;
return !state.is_empty() && state.last().unwrap().borrow().val == 7;
}
/* 記錄解 */
@@ -23,8 +23,8 @@ fn record_solution(
}
/* 判斷在當前狀態下,該選擇是否合法 */
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNode>>) -> bool {
return choice.borrow().val != 3;
fn is_valid(_: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Option<&Rc<RefCell<TreeNode>>>) -> bool {
return choice.is_some() && choice.unwrap().borrow().val != 3;
}
/* 更新狀態 */
@@ -34,13 +34,13 @@ fn make_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, choice: Rc<RefCell<TreeNo
/* 恢復狀態 */
fn undo_choice(state: &mut Vec<Rc<RefCell<TreeNode>>>, _: Rc<RefCell<TreeNode>>) {
state.remove(state.len() - 1);
state.pop();
}
/* 回溯演算法:例題三 */
fn backtrack(
state: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &mut Vec<Rc<RefCell<TreeNode>>>,
choices: &Vec<Option<&Rc<RefCell<TreeNode>>>>,
res: &mut Vec<Vec<Rc<RefCell<TreeNode>>>>,
) {
// 檢查是否為解
@@ -49,22 +49,22 @@ fn backtrack(
record_solution(state, res);
}
// 走訪所有選擇
for choice in choices {
for &choice in choices.iter() {
// 剪枝:檢查選擇是否合法
if is_valid(state, choice.clone()) {
if is_valid(state, choice) {
// 嘗試:做出選擇,更新狀態
make_choice(state, choice.clone());
make_choice(state, choice.unwrap().clone());
// 進行下一輪選擇
backtrack(
state,
&mut vec![
choice.borrow().left.clone().unwrap(),
choice.borrow().right.clone().unwrap(),
&vec![
choice.unwrap().borrow().left.as_ref(),
choice.unwrap().borrow().right.as_ref(),
],
res,
);
// 回退:撤銷選擇,恢復到之前的狀態
undo_choice(state, choice.clone());
undo_choice(state, choice.unwrap().clone());
}
}
}
@@ -77,7 +77,7 @@ pub fn main() {
// 回溯演算法
let mut res = Vec::new();
backtrack(&mut Vec::new(), &mut vec![root.unwrap()], &mut res);
backtrack(&mut Vec::new(), &mut vec![root.as_ref()], &mut res);
println!("\n輸出所有根節點到節點 7 的路徑,要求路徑中不包含值為 3 的節點");
for path in res {