From e5eb0ab49270d4c463d9c697b1a61a075010286c Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 25 Dec 2023 02:38:25 +0800 Subject: [PATCH] deploy --- chapter_tree/binary_search_tree/index.html | 195 ++++---- .../index.html | 4 +- .../iteration_and_recursion/index.html | 220 ++++----- .../performance_evaluation/index.html | 58 +-- .../space_complexity/index.html | 134 +++--- .../summary/index.html | 60 +-- .../time_complexity/index.html | 440 +++++++++--------- en/chapter_data_structure/index.html | 4 +- .../algorithms_are_everywhere/index.html | 46 +- .../what_is_dsa/index.html | 2 +- en/chapter_preface/about_the_book/index.html | 63 +-- en/chapter_preface/index.html | 2 +- en/chapter_preface/summary/index.html | 4 +- en/search/search_index.json | 2 +- en/sitemap.xml | 34 +- en/sitemap.xml.gz | Bin 384 -> 385 bytes search/search_index.json | 2 +- sitemap.xml | 208 ++++----- sitemap.xml.gz | Bin 998 -> 999 bytes 19 files changed, 734 insertions(+), 744 deletions(-) diff --git a/chapter_tree/binary_search_tree/index.html b/chapter_tree/binary_search_tree/index.html index cb77c419f..800c48315 100644 --- a/chapter_tree/binary_search_tree/index.html +++ b/chapter_tree/binary_search_tree/index.html @@ -3841,27 +3841,23 @@
binary_search_tree.rs
/* 查找节点 */
-pub fn search(&self, num: i32) -> Option<TreeNodeRc> {
+pub fn search(&self, num: i32) -> OptionTreeNodeRc {
     let mut cur = self.root.clone();
-
-    // 循环查找,越过叶节点后跳出
-    while let Some(node) = cur.clone() {
-        // 目标节点在 cur 的右子树中
-        if node.borrow().val < num {
-            cur = node.borrow().right.clone();
-        }
-        // 目标节点在 cur 的左子树中
-        else if node.borrow().val > num {
-            cur = node.borrow().left.clone();
-        }
-        // 找到目标节点,跳出循环
-        else {
-            break;
-        }
-    }
-    // 返回目标节点
-    cur
-}
+    // 循环查找,越过叶节点后跳出
+    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
+}
 
@@ -4216,29 +4212,30 @@ let mut pre = None; // 循环查找,越过叶节点后跳出 while let Some(node) = cur.clone() { - // 找到重复节点,直接返回 - if node.borrow().val == num { - return; - } - // 插入位置在 cur 的右子树中 - pre = cur.clone(); - if node.borrow().val < num { - cur = node.borrow().right.clone(); - } - // 插入位置在 cur 的左子树中 - else { - cur = node.borrow().left.clone(); - } - } - // 插入节点 - let node = TreeNode::new(num); + 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(); - if pre.borrow().val < num { - pre.borrow_mut().right = Some(Rc::clone(&node)); - } else { - pre.borrow_mut().left = Some(Rc::clone(&node)); - } -} + let node = Some(TreeNode::new(num)); + if num > pre.borrow().val { + pre.borrow_mut().right = node; + } else { + pre.borrow_mut().left = node; + } +}
@@ -4840,61 +4837,65 @@ let mut pre = None; // 循环查找,越过叶节点后跳出 while let Some(node) = cur.clone() { - // 找到待删除节点,跳出循环 - if node.borrow().val == num { - break; - } - // 待删除节点在 cur 的右子树中 - pre = cur.clone(); - if node.borrow().val < num { - cur = node.borrow().right.clone(); - } - // 待删除节点在 cur 的左子树中 - else { - cur = node.borrow().left.clone(); - } - } - // 若无待删除节点,则直接返回 - if cur.is_none() { - return; - } - let cur = cur.unwrap(); - // 子节点数量 = 0 or 1 - if cur.borrow().left.is_none() || cur.borrow().right.is_none() { - // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点 - let child = cur.borrow().left.clone().or_else(|| cur.borrow().right.clone()); - let pre = pre.unwrap(); - let left = pre.borrow().left.clone().unwrap(); - // 删除节点 cur - if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) { - if Rc::ptr_eq(&left, &cur) { - pre.borrow_mut().left = child; - } else { - pre.borrow_mut().right = child; - } - } else { - // 若删除节点为根节点,则重新指定根节点 - self.root = child; - } - } - // 子节点数量 = 2 - else { - // 获取中序遍历中 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; - } -} + 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; + } + } +}
diff --git a/en/chapter_computational_complexity/index.html b/en/chapter_computational_complexity/index.html index f27d3773e..427f0cdc5 100644 --- a/en/chapter_computational_complexity/index.html +++ b/en/chapter_computational_complexity/index.html @@ -812,8 +812,8 @@

Abstract

-

Complexity analysis is like a space-time guide in the vast universe of algorithms.

-

It leads us to explore deeply in the dimensions of time and space, in search of more elegant solutions.

+

Complexity analysis is like a space-time navigator in the vast universe of algorithms.

+

It guides us in exploring deeper within the the dimensions of time and space, seeking more elegant solutions.

本章内容