2023-07-24 22:27:26 +08:00
|
|
|
/*
|
2023-05-23 15:42:08 +08:00
|
|
|
* File: binary_search_tree.rs
|
|
|
|
* Created Time: 2023-04-20
|
2023-12-23 23:20:26 +08:00
|
|
|
* Author: xBLACKICEx (xBLACKICE@outlook.com)、night-cruise (2586447362@qq.com)
|
2023-05-23 15:42:08 +08:00
|
|
|
*/
|
|
|
|
|
2024-10-23 18:49:47 +08:00
|
|
|
use hello_algo_rust::include::print_util;
|
2023-07-24 22:27:26 +08:00
|
|
|
|
2023-12-23 23:20:26 +08:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::cmp::Ordering;
|
2024-03-16 02:13:41 +08:00
|
|
|
use std::rc::Rc;
|
2023-12-23 23:20:26 +08:00
|
|
|
|
2024-10-23 18:49:47 +08:00
|
|
|
use hello_algo_rust::include::TreeNode;
|
2023-07-24 22:27:26 +08:00
|
|
|
|
2023-12-23 23:20:26 +08:00
|
|
|
type OptionTreeNodeRc = Option<Rc<RefCell<TreeNode>>>;
|
2023-05-23 15:42:08 +08:00
|
|
|
|
|
|
|
/* 二叉搜索树 */
|
|
|
|
pub struct BinarySearchTree {
|
2023-12-23 23:20:26 +08:00
|
|
|
root: OptionTreeNodeRc,
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BinarySearchTree {
|
2023-07-24 22:27:26 +08:00
|
|
|
/* 构造方法 */
|
2023-12-23 23:20:26 +08:00
|
|
|
pub fn new() -> Self {
|
|
|
|
// 初始化空树
|
|
|
|
Self { root: None }
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 获取二叉树根节点 */
|
2023-12-23 23:20:26 +08:00
|
|
|
pub fn get_root(&self) -> OptionTreeNodeRc {
|
|
|
|
self.root.clone()
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 查找节点 */
|
2023-12-23 23:20:26 +08:00
|
|
|
pub fn search(&self, num: i32) -> OptionTreeNodeRc {
|
2023-05-23 15:42:08 +08:00
|
|
|
let mut cur = self.root.clone();
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while let Some(node) = cur.clone() {
|
2023-12-23 23:20:26 +08:00
|
|
|
match num.cmp(&node.borrow().val) {
|
|
|
|
// 目标节点在 cur 的右子树中
|
|
|
|
Ordering::Greater => cur = node.borrow().right.clone(),
|
|
|
|
// 目标节点在 cur 的左子树中
|
|
|
|
Ordering::Less => cur = node.borrow().left.clone(),
|
|
|
|
// 找到目标节点,跳出循环
|
|
|
|
Ordering::Equal => break,
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-23 23:20:26 +08:00
|
|
|
|
2023-05-23 15:42:08 +08:00
|
|
|
// 返回目标节点
|
|
|
|
cur
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 插入节点 */
|
2023-07-24 22:27:26 +08:00
|
|
|
pub fn insert(&mut self, num: i32) {
|
2023-08-31 02:31:31 +08:00
|
|
|
// 若树为空,则初始化根节点
|
2023-05-23 15:42:08 +08:00
|
|
|
if self.root.is_none() {
|
2023-09-02 16:08:52 +08:00
|
|
|
self.root = Some(TreeNode::new(num));
|
2023-07-24 22:27:26 +08:00
|
|
|
return;
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
let mut cur = self.root.clone();
|
|
|
|
let mut pre = None;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while let Some(node) = cur.clone() {
|
2023-12-23 23:20:26 +08:00
|
|
|
match num.cmp(&node.borrow().val) {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
Ordering::Equal => return,
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
Ordering::Greater => {
|
|
|
|
pre = cur.clone();
|
|
|
|
cur = node.borrow().right.clone();
|
|
|
|
}
|
|
|
|
// 插入位置在 cur 的左子树中
|
|
|
|
Ordering::Less => {
|
|
|
|
pre = cur.clone();
|
|
|
|
cur = node.borrow().left.clone();
|
|
|
|
}
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 插入节点
|
|
|
|
let pre = pre.unwrap();
|
2023-12-23 23:20:26 +08:00
|
|
|
let node = Some(TreeNode::new(num));
|
|
|
|
if num > pre.borrow().val {
|
|
|
|
pre.borrow_mut().right = node;
|
2023-05-23 15:42:08 +08:00
|
|
|
} else {
|
2023-12-23 23:20:26 +08:00
|
|
|
pre.borrow_mut().left = node;
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 删除节点 */
|
2023-07-24 22:27:26 +08:00
|
|
|
pub fn remove(&mut self, num: i32) {
|
2023-05-23 15:42:08 +08:00
|
|
|
// 若树为空,直接提前返回
|
2024-03-16 02:13:41 +08:00
|
|
|
if self.root.is_none() {
|
|
|
|
return;
|
2023-07-24 22:27:26 +08:00
|
|
|
}
|
2023-05-23 15:42:08 +08:00
|
|
|
let mut cur = self.root.clone();
|
|
|
|
let mut pre = None;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while let Some(node) = cur.clone() {
|
2023-12-23 23:20:26 +08:00
|
|
|
match num.cmp(&node.borrow().val) {
|
|
|
|
// 找到待删除节点,跳出循环
|
|
|
|
Ordering::Equal => break,
|
|
|
|
// 待删除节点在 cur 的右子树中
|
|
|
|
Ordering::Greater => {
|
|
|
|
pre = cur.clone();
|
|
|
|
cur = node.borrow().right.clone();
|
|
|
|
}
|
|
|
|
// 待删除节点在 cur 的左子树中
|
|
|
|
Ordering::Less => {
|
|
|
|
pre = cur.clone();
|
|
|
|
cur = node.borrow().left.clone();
|
|
|
|
}
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
2023-07-24 22:27:26 +08:00
|
|
|
if cur.is_none() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let cur = cur.unwrap();
|
2023-12-23 23:20:26 +08:00
|
|
|
let (left_child, right_child) = (cur.borrow().left.clone(), cur.borrow().right.clone());
|
|
|
|
match (left_child.clone(), right_child.clone()) {
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
(None, None) | (Some(_), None) | (None, Some(_)) => {
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
|
|
|
let child = left_child.or(right_child);
|
|
|
|
let pre = pre.unwrap();
|
|
|
|
// 删除节点 cur
|
|
|
|
if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) {
|
|
|
|
let left = pre.borrow().left.clone();
|
2024-10-31 21:24:56 +08:00
|
|
|
if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) {
|
2023-12-23 23:20:26 +08:00
|
|
|
pre.borrow_mut().left = child;
|
|
|
|
} else {
|
|
|
|
pre.borrow_mut().right = child;
|
|
|
|
}
|
2023-05-23 15:42:08 +08:00
|
|
|
} else {
|
2023-12-23 23:20:26 +08:00
|
|
|
// 若删除节点为根节点,则重新指定根节点
|
|
|
|
self.root = child;
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-23 23:20:26 +08:00
|
|
|
// 子节点数量 = 2
|
|
|
|
(Some(_), Some(_)) => {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
let mut tmp = cur.borrow().right.clone();
|
|
|
|
while let Some(node) = tmp.clone() {
|
|
|
|
if node.borrow().left.is_some() {
|
|
|
|
tmp = node.borrow().left.clone();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
2024-10-31 21:24:56 +08:00
|
|
|
let tmp_val = tmp.unwrap().borrow().val;
|
2023-12-23 23:20:26 +08:00
|
|
|
// 递归删除节点 tmp
|
2024-10-31 21:24:56 +08:00
|
|
|
self.remove(tmp_val);
|
2023-12-23 23:20:26 +08:00
|
|
|
// 用 tmp 覆盖 cur
|
2024-10-31 21:24:56 +08:00
|
|
|
cur.borrow_mut().val = tmp_val;
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Driver Code */
|
|
|
|
fn main() {
|
2023-07-24 22:27:26 +08:00
|
|
|
/* 初始化二叉搜索树 */
|
2023-12-23 23:20:26 +08:00
|
|
|
let mut bst = BinarySearchTree::new();
|
|
|
|
// 请注意,不同的插入顺序会生成不同的二叉树,该序列可以生成一个完美二叉树
|
|
|
|
let nums = [8, 4, 12, 2, 6, 10, 14, 1, 3, 5, 7, 9, 11, 13, 15];
|
|
|
|
for &num in &nums {
|
|
|
|
bst.insert(num);
|
|
|
|
}
|
|
|
|
println!("\n初始化的二叉树为\n");
|
|
|
|
print_util::print_tree(bst.get_root().as_ref().unwrap());
|
2023-05-23 15:42:08 +08:00
|
|
|
|
2023-12-23 23:20:26 +08:00
|
|
|
/* 查找结点 */
|
|
|
|
let node = bst.search(7);
|
2024-03-16 02:13:41 +08:00
|
|
|
println!(
|
|
|
|
"\n查找到的节点对象为 {:?},节点值 = {}",
|
|
|
|
node.clone().unwrap(),
|
|
|
|
node.clone().unwrap().borrow().val
|
|
|
|
);
|
2023-05-23 15:42:08 +08:00
|
|
|
|
2023-07-24 22:27:26 +08:00
|
|
|
/* 插入节点 */
|
|
|
|
bst.insert(16);
|
2023-12-23 23:20:26 +08:00
|
|
|
println!("\n插入节点 16 后,二叉树为\n");
|
|
|
|
print_util::print_tree(bst.get_root().as_ref().unwrap());
|
2023-05-23 15:42:08 +08:00
|
|
|
|
2023-07-24 22:27:26 +08:00
|
|
|
/* 删除节点 */
|
2023-05-23 15:42:08 +08:00
|
|
|
bst.remove(1);
|
|
|
|
println!("\n删除节点 1 后,二叉树为\n");
|
2023-12-23 23:20:26 +08:00
|
|
|
print_util::print_tree(bst.get_root().as_ref().unwrap());
|
2023-05-23 15:42:08 +08:00
|
|
|
bst.remove(2);
|
|
|
|
println!("\n删除节点 2 后,二叉树为\n");
|
2023-12-23 23:20:26 +08:00
|
|
|
print_util::print_tree(bst.get_root().as_ref().unwrap());
|
2023-05-23 15:42:08 +08:00
|
|
|
bst.remove(4);
|
|
|
|
println!("\n删除节点 4 后,二叉树为\n");
|
2023-12-23 23:20:26 +08:00
|
|
|
print_util::print_tree(bst.get_root().as_ref().unwrap());
|
2023-05-23 15:42:08 +08:00
|
|
|
}
|