mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-01 03:24:24 +08:00 
			
		
		
		
	 5f7385c8a3
			
		
	
	5f7385c8a3
	
	
	
		
			
			* First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology. * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * First commit * Update mkdocs.yml * Translate all the docs to traditional Chinese * Translate the code files. * Translate the docker file * Fix mkdocs.yml * Translate all the figures from SC to TC * 二叉搜尋樹 -> 二元搜尋樹 * Update terminology * 构造函数/构造方法 -> 建構子 异或 -> 互斥或 * 擴充套件 -> 擴展 * constant - 常量 - 常數 * 類 -> 類別 * AVL -> AVL 樹 * 數組 -> 陣列 * 係統 -> 系統 斐波那契數列 -> 費波那契數列 運算元量 -> 運算量 引數 -> 參數 * 聯絡 -> 關聯 * 麵試 -> 面試 * 面向物件 -> 物件導向 歸併排序 -> 合併排序 范式 -> 範式 * Fix 算法 -> 演算法 * 錶示 -> 表示 反碼 -> 一補數 補碼 -> 二補數 列列尾部 -> 佇列尾部 區域性性 -> 區域性 一摞 -> 一疊 * Synchronize with main branch * 賬號 -> 帳號 推匯 -> 推導 * Sync with main branch * Update terminology.md * 操作数量(num. of operations)-> 操作數量 * 字首和->前綴和 * Update figures * 歸 -> 迴 記憶體洩漏 -> 記憶體流失 * Fix the bug of the file filter * 支援 -> 支持 Add zh-Hant/README.md * Add the zh-Hant chapter covers. Bug fixes. * 外掛 -> 擴充功能 * Add the landing page for zh-Hant version * Unify the font of the chapter covers for the zh, en, and zh-Hant version * Move zh-Hant/ to zh-hant/ * Translate terminology.md to traditional Chinese
		
			
				
	
	
		
			196 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
			
		
		
	
	
			196 lines
		
	
	
		
			6.5 KiB
		
	
	
	
		
			Rust
		
	
	
	
	
	
| /*
 | |
|  * File: binary_search_tree.rs
 | |
|  * Created Time: 2023-04-20
 | |
|  * Author: xBLACKICEx (xBLACKICE@outlook.com)、night-cruise (2586447362@qq.com)
 | |
|  */
 | |
| 
 | |
| include!("../include/include.rs");
 | |
| 
 | |
| use std::cell::RefCell;
 | |
| use std::cmp::Ordering;
 | |
| use std::rc::Rc;
 | |
| 
 | |
| use tree_node::TreeNode;
 | |
| 
 | |
| type OptionTreeNodeRc = Option<Rc<RefCell<TreeNode>>>;
 | |
| 
 | |
| /* 二元搜尋樹 */
 | |
| pub struct BinarySearchTree {
 | |
|     root: OptionTreeNodeRc,
 | |
| }
 | |
| 
 | |
| impl BinarySearchTree {
 | |
|     /* 建構子 */
 | |
|     pub fn new() -> Self {
 | |
|         // 初始化空樹
 | |
|         Self { root: None }
 | |
|     }
 | |
| 
 | |
|     /* 獲取二元樹根節點 */
 | |
|     pub fn get_root(&self) -> OptionTreeNodeRc {
 | |
|         self.root.clone()
 | |
|     }
 | |
| 
 | |
|     /* 查詢節點 */
 | |
|     pub fn search(&self, num: i32) -> OptionTreeNodeRc {
 | |
|         let mut cur = self.root.clone();
 | |
|         // 迴圈查詢,越過葉節點後跳出
 | |
|         while let Some(node) = cur.clone() {
 | |
|             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,
 | |
|             }
 | |
|         }
 | |
| 
 | |
|         // 返回目標節點
 | |
|         cur
 | |
|     }
 | |
| 
 | |
|     /* 插入節點 */
 | |
|     pub fn insert(&mut self, num: i32) {
 | |
|         // 若樹為空,則初始化根節點
 | |
|         if self.root.is_none() {
 | |
|             self.root = Some(TreeNode::new(num));
 | |
|             return;
 | |
|         }
 | |
|         let mut cur = self.root.clone();
 | |
|         let mut pre = None;
 | |
|         // 迴圈查詢,越過葉節點後跳出
 | |
|         while let Some(node) = cur.clone() {
 | |
|             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();
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         // 插入節點
 | |
|         let pre = pre.unwrap();
 | |
|         let node = Some(TreeNode::new(num));
 | |
|         if num > pre.borrow().val {
 | |
|             pre.borrow_mut().right = node;
 | |
|         } else {
 | |
|             pre.borrow_mut().left = node;
 | |
|         }
 | |
|     }
 | |
| 
 | |
|     /* 刪除節點 */
 | |
|     pub fn remove(&mut self, num: i32) {
 | |
|         // 若樹為空,直接提前返回
 | |
|         if self.root.is_none() {
 | |
|             return;
 | |
|         }
 | |
|         let mut cur = self.root.clone();
 | |
|         let mut pre = None;
 | |
|         // 迴圈查詢,越過葉節點後跳出
 | |
|         while let Some(node) = cur.clone() {
 | |
|             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();
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         // 若無待刪除節點,則直接返回
 | |
|         if cur.is_none() {
 | |
|             return;
 | |
|         }
 | |
|         let cur = cur.unwrap();
 | |
|         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();
 | |
|                     if left.is_some() && Rc::ptr_eq(&left.as_ref().unwrap(), &cur) {
 | |
|                         pre.borrow_mut().left = child;
 | |
|                     } else {
 | |
|                         pre.borrow_mut().right = child;
 | |
|                     }
 | |
|                 } else {
 | |
|                     // 若刪除節點為根節點,則重新指定根節點
 | |
|                     self.root = child;
 | |
|                 }
 | |
|             }
 | |
|             // 子節點數量 = 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;
 | |
|                     }
 | |
|                 }
 | |
|                 let tmpval = tmp.unwrap().borrow().val;
 | |
|                 // 遞迴刪除節點 tmp
 | |
|                 self.remove(tmpval);
 | |
|                 // 用 tmp 覆蓋 cur
 | |
|                 cur.borrow_mut().val = tmpval;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 | |
| 
 | |
| /* Driver Code */
 | |
| fn main() {
 | |
|     /* 初始化二元搜尋樹 */
 | |
|     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());
 | |
| 
 | |
|     /* 查詢結點 */
 | |
|     let node = bst.search(7);
 | |
|     println!(
 | |
|         "\n查詢到的節點物件為 {:?},節點值 = {}",
 | |
|         node.clone().unwrap(),
 | |
|         node.clone().unwrap().borrow().val
 | |
|     );
 | |
| 
 | |
|     /* 插入節點 */
 | |
|     bst.insert(16);
 | |
|     println!("\n插入節點 16 後,二元樹為\n");
 | |
|     print_util::print_tree(bst.get_root().as_ref().unwrap());
 | |
| 
 | |
|     /* 刪除節點 */
 | |
|     bst.remove(1);
 | |
|     println!("\n刪除節點 1 後,二元樹為\n");
 | |
|     print_util::print_tree(bst.get_root().as_ref().unwrap());
 | |
|     bst.remove(2);
 | |
|     println!("\n刪除節點 2 後,二元樹為\n");
 | |
|     print_util::print_tree(bst.get_root().as_ref().unwrap());
 | |
|     bst.remove(4);
 | |
|     println!("\n刪除節點 4 後,二元樹為\n");
 | |
|     print_util::print_tree(bst.get_root().as_ref().unwrap());
 | |
| }
 |