From ee0cba022cd9f1433e0e7e350fd871b2d1a0486a Mon Sep 17 00:00:00 2001 From: Iain Date: Mon, 23 Nov 2026 17:26:08 +0000 Subject: [PATCH] Fix root deletion in C and Rust binary search trees (#2020) Co-authored-by: Iain <23277273+NotAFlightRisk@users.noreply.github.com> --- codes/c/chapter_tree/binary_search_tree.c | 4 +++- codes/rust/chapter_tree/binary_search_tree.rs | 2 +- en/codes/c/chapter_tree/binary_search_tree.c | 4 +++- en/codes/rust/chapter_tree/binary_search_tree.rs | 2 +- ja/codes/c/chapter_tree/binary_search_tree.c | 4 +++- ja/codes/rust/chapter_tree/binary_search_tree.rs | 2 +- ko/codes/c/chapter_tree/binary_search_tree.c | 4 +++- ko/codes/rust/chapter_tree/binary_search_tree.rs | 2 +- ru/codes/c/chapter_tree/binary_search_tree.c | 4 +++- ru/codes/rust/chapter_tree/binary_search_tree.rs | 2 +- zh-hant/codes/c/chapter_tree/binary_search_tree.c | 4 +++- zh-hant/codes/rust/chapter_tree/binary_search_tree.rs | 2 +- 12 files changed, 24 insertions(+), 12 deletions(-) diff --git a/codes/c/chapter_tree/binary_search_tree.c b/codes/c/chapter_tree/binary_search_tree.c index 751d63b9e..f99f14c49 100644 --- a/codes/c/chapter_tree/binary_search_tree.c +++ b/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点 TreeNode *child = cur->left != NULL ? cur->left : cur->right; // 删除节点 cur - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/codes/rust/chapter_tree/binary_search_tree.rs b/codes/rust/chapter_tree/binary_search_tree.rs index 720d9c642..cea03c580 100644 --- a/codes/rust/chapter_tree/binary_search_tree.rs +++ b/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (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 pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; diff --git a/en/codes/c/chapter_tree/binary_search_tree.c b/en/codes/c/chapter_tree/binary_search_tree.c index fd5efbb1b..d67adba69 100644 --- a/en/codes/c/chapter_tree/binary_search_tree.c +++ b/en/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // With 0 or 1 child, child is NULL or that child, respectively TreeNode *child = cur->left != NULL ? cur->left : cur->right; // Remove node cur - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/en/codes/rust/chapter_tree/binary_search_tree.rs b/en/codes/rust/chapter_tree/binary_search_tree.rs index 977dcd767..048693e14 100644 --- a/en/codes/rust/chapter_tree/binary_search_tree.rs +++ b/en/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (None, None) | (Some(_), None) | (None, Some(_)) => { // With 0 or 1 child, child is None or that child, respectively let child = left_child.or(right_child); - let pre = pre.unwrap(); // Remove node cur if !Rc::ptr_eq(&cur, self.root.as_ref().unwrap()) { + let pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; diff --git a/ja/codes/c/chapter_tree/binary_search_tree.c b/ja/codes/c/chapter_tree/binary_search_tree.c index 1afbc6295..d7c3c0010 100644 --- a/ja/codes/c/chapter_tree/binary_search_tree.c +++ b/ja/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // 子ノード数 = 0 / 1 の場合、child = nullptr / その子ノード TreeNode *child = cur->left != NULL ? cur->left : cur->right; // ノード cur を削除する - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/ja/codes/rust/chapter_tree/binary_search_tree.rs b/ja/codes/rust/chapter_tree/binary_search_tree.rs index 1dc225953..cb2140ac3 100644 --- a/ja/codes/rust/chapter_tree/binary_search_tree.rs +++ b/ja/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (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 pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; diff --git a/ko/codes/c/chapter_tree/binary_search_tree.c b/ko/codes/c/chapter_tree/binary_search_tree.c index 7b49de5a1..0c1a36375 100644 --- a/ko/codes/c/chapter_tree/binary_search_tree.c +++ b/ko/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // 자식 노드 수가 0 / 1이면 child = nullptr / 해당 자식 노드다 TreeNode *child = cur->left != NULL ? cur->left : cur->right; // 노드 cur를 삭제한다 - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/ko/codes/rust/chapter_tree/binary_search_tree.rs b/ko/codes/rust/chapter_tree/binary_search_tree.rs index 2aa03c217..06215a50d 100644 --- a/ko/codes/rust/chapter_tree/binary_search_tree.rs +++ b/ko/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (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 pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; diff --git a/ru/codes/c/chapter_tree/binary_search_tree.c b/ru/codes/c/chapter_tree/binary_search_tree.c index d7c7b4158..db8ce1ebd 100644 --- a/ru/codes/c/chapter_tree/binary_search_tree.c +++ b/ru/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // Когда число дочерних узлов = 0 / 1, child = nullptr / этот дочерний узел TreeNode *child = cur->left != NULL ? cur->left : cur->right; // Удалить узел cur - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/ru/codes/rust/chapter_tree/binary_search_tree.rs b/ru/codes/rust/chapter_tree/binary_search_tree.rs index fe76f4980..af154b00a 100644 --- a/ru/codes/rust/chapter_tree/binary_search_tree.rs +++ b/ru/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (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 pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child; diff --git a/zh-hant/codes/c/chapter_tree/binary_search_tree.c b/zh-hant/codes/c/chapter_tree/binary_search_tree.c index a20e5f4f0..fbafc30ac 100644 --- a/zh-hant/codes/c/chapter_tree/binary_search_tree.c +++ b/zh-hant/codes/c/chapter_tree/binary_search_tree.c @@ -112,7 +112,9 @@ void removeItem(BinarySearchTree *bst, int num) { // 當子節點數量 = 0 / 1 時, child = nullptr / 該子節點 TreeNode *child = cur->left != NULL ? cur->left : cur->right; // 刪除節點 cur - if (pre->left == cur) { + if (pre == NULL) { + bst->root = child; + } else if (pre->left == cur) { pre->left = child; } else { pre->right = child; diff --git a/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs b/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs index 1186f6bca..8b4c5c777 100644 --- a/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs +++ b/zh-hant/codes/rust/chapter_tree/binary_search_tree.rs @@ -122,9 +122,9 @@ impl BinarySearchTree { (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 pre = pre.unwrap(); let left = pre.borrow().left.clone(); if left.is_some() && Rc::ptr_eq(left.as_ref().unwrap(), &cur) { pre.borrow_mut().left = child;