mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-12 23:36:06 +00:00
cargo fmt rust code (#1131)
* cargo fmt code * Add empty line to seperate unrelated comments * Fix review * Update bubble_sort.rs * Update merge_sort.rs --------- Co-authored-by: Yudong Jin <krahets@163.com>
This commit is contained in:
@@ -196,4 +196,4 @@ fn main() {
|
||||
println!("中序遍历为:{:?}", res);
|
||||
res = abt.post_order();
|
||||
println!("后序遍历为:{:?}", res);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
|
||||
include!("../include/include.rs");
|
||||
|
||||
use tree_node::TreeNode;
|
||||
use std::rc::Rc;
|
||||
use std::cmp::Ordering;
|
||||
use std::cell::RefCell;
|
||||
use std::cmp::Ordering;
|
||||
use std::rc::Rc;
|
||||
use tree_node::TreeNode;
|
||||
|
||||
type OptionTreeNodeRc = Option<Rc<RefCell<TreeNode>>>;
|
||||
|
||||
@@ -157,6 +157,7 @@ impl AVLTree {
|
||||
}
|
||||
}
|
||||
Self::update_height(Some(node.clone())); // 更新节点高度
|
||||
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = Self::rotate(Some(node)).unwrap();
|
||||
// 返回子树的根节点
|
||||
@@ -211,6 +212,7 @@ impl AVLTree {
|
||||
node.borrow_mut().val = temp.borrow().val;
|
||||
}
|
||||
Self::update_height(Some(node.clone())); // 更新节点高度
|
||||
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = Self::rotate(Some(node)).unwrap();
|
||||
// 返回子树的根节点
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
include!("../include/include.rs");
|
||||
|
||||
use std::cell::RefCell;
|
||||
use std::rc::Rc;
|
||||
use std::cmp::Ordering;
|
||||
use std::rc::Rc;
|
||||
|
||||
use tree_node::TreeNode;
|
||||
|
||||
@@ -89,8 +89,8 @@ impl BinarySearchTree {
|
||||
/* 删除节点 */
|
||||
pub fn remove(&mut self, num: i32) {
|
||||
// 若树为空,直接提前返回
|
||||
if self.root.is_none() {
|
||||
return;
|
||||
if self.root.is_none() {
|
||||
return;
|
||||
}
|
||||
let mut cur = self.root.clone();
|
||||
let mut pre = None;
|
||||
@@ -171,7 +171,11 @@ fn main() {
|
||||
|
||||
/* 查找结点 */
|
||||
let node = bst.search(7);
|
||||
println!("\n查找到的节点对象为 {:?},节点值 = {}", node.clone().unwrap(), node.clone().unwrap().borrow().val);
|
||||
println!(
|
||||
"\n查找到的节点对象为 {:?},节点值 = {}",
|
||||
node.clone().unwrap(),
|
||||
node.clone().unwrap().borrow().val
|
||||
);
|
||||
|
||||
/* 插入节点 */
|
||||
bst.insert(16);
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
* Created Time: 2023-02-27
|
||||
* Author: xBLACKICEx (xBLACKICE@outlook.com)
|
||||
*/
|
||||
|
||||
use std::rc::Rc;
|
||||
include!("../include/include.rs");
|
||||
use tree_node::TreeNode;
|
||||
@@ -37,4 +36,4 @@ fn main() {
|
||||
n1.borrow_mut().left = Some(Rc::clone(&n2));
|
||||
println!("\n删除节点 P 后\n");
|
||||
print_util::print_tree(&n1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,13 +18,14 @@ fn level_order(root: &Rc<RefCell<TreeNode>>) -> Vec<i32> {
|
||||
// 初始化一个列表,用于保存遍历序列
|
||||
let mut vec = Vec::new();
|
||||
|
||||
while let Some(node) = que.pop_front() { // 队列出队
|
||||
vec.push(node.borrow().val); // 保存节点值
|
||||
while let Some(node) = que.pop_front() {
|
||||
// 队列出队
|
||||
vec.push(node.borrow().val); // 保存节点值
|
||||
if let Some(left) = node.borrow().left.as_ref() {
|
||||
que.push_back(Rc::clone(left)); // 左子节点入队
|
||||
que.push_back(Rc::clone(left)); // 左子节点入队
|
||||
}
|
||||
if let Some(right) = node.borrow().right.as_ref() {
|
||||
que.push_back(Rc::clone(right)); // 右子节点入队
|
||||
que.push_back(Rc::clone(right)); // 右子节点入队
|
||||
};
|
||||
}
|
||||
vec
|
||||
|
||||
@@ -68,4 +68,4 @@ fn main() {
|
||||
/* 后序遍历 */
|
||||
let vec = post_order(root.as_ref());
|
||||
print!("\n后序遍历的节点打印序列 = {:?}", vec);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user