|
|
@ -263,16 +263,16 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```java title="binary_search_tree.java"
|
|
|
|
```java title="binary_search_tree.java"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
TreeNode insert(int num) {
|
|
|
|
void insert(int num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == null)
|
|
|
|
if (root == null)
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
TreeNode cur = root, pre = null;
|
|
|
|
TreeNode cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != null) {
|
|
|
|
while (cur != null) {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if (cur.val == num)
|
|
|
|
if (cur.val == num)
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
if (cur.val < num)
|
|
|
|
if (cur.val < num)
|
|
|
@ -287,7 +287,6 @@ comments: true
|
|
|
|
pre.right = node;
|
|
|
|
pre.right = node;
|
|
|
|
else
|
|
|
|
else
|
|
|
|
pre.left = node;
|
|
|
|
pre.left = node;
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -295,16 +294,16 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
TreeNode *insert(int num) {
|
|
|
|
void insert(int num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == nullptr)
|
|
|
|
if (root == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return;
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != nullptr) {
|
|
|
|
while (cur != nullptr) {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if (cur->val == num)
|
|
|
|
if (cur->val == num)
|
|
|
|
return nullptr;
|
|
|
|
return;
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
if (cur->val < num)
|
|
|
|
if (cur->val < num)
|
|
|
@ -319,25 +318,24 @@ comments: true
|
|
|
|
pre->right = node;
|
|
|
|
pre->right = node;
|
|
|
|
else
|
|
|
|
else
|
|
|
|
pre->left = node;
|
|
|
|
pre->left = node;
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="binary_search_tree.py"
|
|
|
|
```python title="binary_search_tree.py"
|
|
|
|
def insert(self, num: int) -> TreeNode | None:
|
|
|
|
def insert(self, num: int) -> None:
|
|
|
|
"""插入节点"""
|
|
|
|
"""插入节点"""
|
|
|
|
# 若树为空,直接提前返回
|
|
|
|
# 若树为空,直接提前返回
|
|
|
|
if self.__root is None:
|
|
|
|
if self.__root is None:
|
|
|
|
return None
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 循环查找,越过叶节点后跳出
|
|
|
|
# 循环查找,越过叶节点后跳出
|
|
|
|
cur, pre = self.__root, None
|
|
|
|
cur, pre = self.__root, None
|
|
|
|
while cur is not None:
|
|
|
|
while cur is not None:
|
|
|
|
# 找到重复节点,直接返回
|
|
|
|
# 找到重复节点,直接返回
|
|
|
|
if cur.val == num:
|
|
|
|
if cur.val == num:
|
|
|
|
return None
|
|
|
|
return
|
|
|
|
pre = cur
|
|
|
|
pre = cur
|
|
|
|
# 插入位置在 cur 的右子树中
|
|
|
|
# 插入位置在 cur 的右子树中
|
|
|
|
if cur.val < num:
|
|
|
|
if cur.val < num:
|
|
|
@ -352,25 +350,24 @@ comments: true
|
|
|
|
pre.right = node
|
|
|
|
pre.right = node
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
pre.left = node
|
|
|
|
pre.left = node
|
|
|
|
return node
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_tree.go"
|
|
|
|
```go title="binary_search_tree.go"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
func (bst *binarySearchTree) insert(num int) *TreeNode {
|
|
|
|
func (bst *binarySearchTree) insert(num int) {
|
|
|
|
cur := bst.root
|
|
|
|
cur := bst.root
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if cur == nil {
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 待插入节点之前的节点位置
|
|
|
|
// 待插入节点之前的节点位置
|
|
|
|
var pre *TreeNode = nil
|
|
|
|
var pre *TreeNode = nil
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
for cur != nil {
|
|
|
|
for cur != nil {
|
|
|
|
if cur.Val == num {
|
|
|
|
if cur.Val == num {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pre = cur
|
|
|
|
pre = cur
|
|
|
|
if cur.Val < num {
|
|
|
|
if cur.Val < num {
|
|
|
@ -386,7 +383,6 @@ comments: true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pre.Left = node
|
|
|
|
pre.Left = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cur
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -396,12 +392,12 @@ comments: true
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
function insert(num) {
|
|
|
|
function insert(num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root === null) return null;
|
|
|
|
if (root === null) return;
|
|
|
|
let cur = root, pre = null;
|
|
|
|
let cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur !== null) {
|
|
|
|
while (cur !== null) {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if (cur.val === num) return null;
|
|
|
|
if (cur.val === num) return;
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
@ -412,7 +408,6 @@ comments: true
|
|
|
|
let node = new TreeNode(num);
|
|
|
|
let node = new TreeNode(num);
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
else pre.left = node;
|
|
|
|
else pre.left = node;
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -420,17 +415,17 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_tree.ts"
|
|
|
|
```typescript title="binary_search_tree.ts"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
function insert(num: number): TreeNode | null {
|
|
|
|
function insert(num: number): void {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root === null) {
|
|
|
|
if (root === null) {
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let cur = root,
|
|
|
|
let cur = root,
|
|
|
|
pre: TreeNode | null = null;
|
|
|
|
pre: TreeNode | null = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur !== null) {
|
|
|
|
while (cur !== null) {
|
|
|
|
if (cur.val === num) {
|
|
|
|
if (cur.val === num) {
|
|
|
|
return null; // 找到重复节点,直接返回
|
|
|
|
return; // 找到重复节点,直接返回
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
if (cur.val < num) {
|
|
|
|
if (cur.val < num) {
|
|
|
@ -446,7 +441,6 @@ comments: true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pre!.left = node;
|
|
|
|
pre!.left = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -460,16 +454,16 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_tree.cs"
|
|
|
|
```csharp title="binary_search_tree.cs"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
TreeNode? insert(int num)
|
|
|
|
void insert(int num)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == null) return null;
|
|
|
|
if (root == null) return;
|
|
|
|
TreeNode? cur = root, pre = null;
|
|
|
|
TreeNode? cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != null)
|
|
|
|
while (cur != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if (cur.val == num) return null;
|
|
|
|
if (cur.val == num) return;
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
|
if (cur.val < num) cur = cur.right;
|
|
|
@ -484,7 +478,6 @@ comments: true
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
if (pre.val < num) pre.right = node;
|
|
|
|
else pre.left = node;
|
|
|
|
else pre.left = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -492,10 +485,10 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```swift title="binary_search_tree.swift"
|
|
|
|
```swift title="binary_search_tree.swift"
|
|
|
|
/* 插入节点 */
|
|
|
|
/* 插入节点 */
|
|
|
|
func insert(num: Int) -> TreeNode? {
|
|
|
|
func insert(num: Int) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if root == nil {
|
|
|
|
if root == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var cur = root
|
|
|
|
var cur = root
|
|
|
|
var pre: TreeNode?
|
|
|
|
var pre: TreeNode?
|
|
|
@ -503,7 +496,7 @@ comments: true
|
|
|
|
while cur != nil {
|
|
|
|
while cur != nil {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if cur!.val == num {
|
|
|
|
if cur!.val == num {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pre = cur
|
|
|
|
pre = cur
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
@ -522,7 +515,6 @@ comments: true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pre?.left = node
|
|
|
|
pre?.left = node
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -530,15 +522,15 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_tree.zig"
|
|
|
|
```zig title="binary_search_tree.zig"
|
|
|
|
// 插入节点
|
|
|
|
// 插入节点
|
|
|
|
fn insert(self: *Self, num: T) !?*inc.TreeNode(T) {
|
|
|
|
fn insert(self: *Self, num: T) !void {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (self.root == null) return null;
|
|
|
|
if (self.root == null) return;
|
|
|
|
var cur = self.root;
|
|
|
|
var cur = self.root;
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != null) {
|
|
|
|
while (cur != null) {
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
// 找到重复节点,直接返回
|
|
|
|
if (cur.?.val == num) return null;
|
|
|
|
if (cur.?.val == num) return;
|
|
|
|
pre = cur;
|
|
|
|
pre = cur;
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
// 插入位置在 cur 的右子树中
|
|
|
|
if (cur.?.val < num) {
|
|
|
|
if (cur.?.val < num) {
|
|
|
@ -556,7 +548,6 @@ comments: true
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
pre.?.left = node;
|
|
|
|
pre.?.left = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -582,9 +573,9 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
当待删除节点的子节点数量 $= 2$ 时,删除操作分为三步:
|
|
|
|
当待删除节点的子节点数量 $= 2$ 时,删除操作分为三步:
|
|
|
|
|
|
|
|
|
|
|
|
1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 nex;
|
|
|
|
1. 找到待删除节点在“中序遍历序列”中的下一个节点,记为 `tmp` ;
|
|
|
|
2. 在树中递归删除节点 `nex` ;
|
|
|
|
2. 在树中递归删除节点 `tmp` ;
|
|
|
|
3. 使用 `nex` 替换待删除节点;
|
|
|
|
3. 用 `tmp` 的值覆盖待删除节点的值;
|
|
|
|
|
|
|
|
|
|
|
|
=== "<1>"
|
|
|
|
=== "<1>"
|
|
|
|
![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png)
|
|
|
|
![bst_remove_case3_step1](binary_search_tree.assets/bst_remove_case3_step1.png)
|
|
|
@ -604,10 +595,10 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```java title="binary_search_tree.java"
|
|
|
|
```java title="binary_search_tree.java"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
TreeNode remove(int num) {
|
|
|
|
void remove(int num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == null)
|
|
|
|
if (root == null)
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
TreeNode cur = root, pre = null;
|
|
|
|
TreeNode cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != null) {
|
|
|
|
while (cur != null) {
|
|
|
@ -624,7 +615,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur == null)
|
|
|
|
if (cur == null)
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur.left == null || cur.right == null) {
|
|
|
|
if (cur.left == null || cur.right == null) {
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
@ -638,25 +629,15 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
TreeNode nex = getInOrderNext(cur.right);
|
|
|
|
TreeNode tmp = cur.right;
|
|
|
|
int tmp = nex.val;
|
|
|
|
while (tmp.left != null) {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp.left;
|
|
|
|
remove(nex.val);
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
cur.val = tmp;
|
|
|
|
remove(tmp.val);
|
|
|
|
}
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
return cur;
|
|
|
|
cur.val = tmp.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
TreeNode getInOrderNext(TreeNode root) {
|
|
|
|
|
|
|
|
if (root == null)
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (root.left != null) {
|
|
|
|
|
|
|
|
root = root.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -664,10 +645,10 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
|
|
|
```cpp title="binary_search_tree.cpp"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
TreeNode *remove(int num) {
|
|
|
|
void remove(int num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == nullptr)
|
|
|
|
if (root == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return;
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
|
|
|
TreeNode *cur = root, *pre = nullptr;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != nullptr) {
|
|
|
|
while (cur != nullptr) {
|
|
|
@ -684,7 +665,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur == nullptr)
|
|
|
|
if (cur == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return;
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur->left == nullptr || cur->right == nullptr) {
|
|
|
|
if (cur->left == nullptr || cur->right == nullptr) {
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = nullptr / 该子节点
|
|
|
@ -700,36 +681,27 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
TreeNode *nex = getInOrderNext(cur->right);
|
|
|
|
TreeNode *tmp = cur->right;
|
|
|
|
int tmp = nex->val;
|
|
|
|
while (tmp->left != nullptr) {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp->left;
|
|
|
|
remove(nex->val);
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
int tmpVal = tmp->val;
|
|
|
|
cur->val = tmp;
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
}
|
|
|
|
remove(tmp->val);
|
|
|
|
return cur;
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
}
|
|
|
|
cur->val = tmpVal;
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
TreeNode *getInOrderNext(TreeNode *root) {
|
|
|
|
|
|
|
|
if (root == nullptr)
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (root->left != nullptr) {
|
|
|
|
|
|
|
|
root = root->left;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Python"
|
|
|
|
=== "Python"
|
|
|
|
|
|
|
|
|
|
|
|
```python title="binary_search_tree.py"
|
|
|
|
```python title="binary_search_tree.py"
|
|
|
|
def remove(self, num: int) -> TreeNode | None:
|
|
|
|
def remove(self, num: int) -> None:
|
|
|
|
"""删除节点"""
|
|
|
|
"""删除节点"""
|
|
|
|
# 若树为空,直接提前返回
|
|
|
|
# 若树为空,直接提前返回
|
|
|
|
if self.__root is None:
|
|
|
|
if self.__root is None:
|
|
|
|
return None
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 循环查找,越过叶节点后跳出
|
|
|
|
# 循环查找,越过叶节点后跳出
|
|
|
|
cur, pre = self.__root, None
|
|
|
|
cur, pre = self.__root, None
|
|
|
@ -738,13 +710,15 @@ comments: true
|
|
|
|
if cur.val == num:
|
|
|
|
if cur.val == num:
|
|
|
|
break
|
|
|
|
break
|
|
|
|
pre = cur
|
|
|
|
pre = cur
|
|
|
|
if cur.val < num: # 待删除节点在 cur 的右子树中
|
|
|
|
# 待删除节点在 cur 的右子树中
|
|
|
|
|
|
|
|
if cur.val < num:
|
|
|
|
cur = cur.right
|
|
|
|
cur = cur.right
|
|
|
|
else: # 待删除节点在 cur 的左子树中
|
|
|
|
# 待删除节点在 cur 的左子树中
|
|
|
|
|
|
|
|
else:
|
|
|
|
cur = cur.left
|
|
|
|
cur = cur.left
|
|
|
|
# 若无待删除节点,则直接返回
|
|
|
|
# 若无待删除节点,则直接返回
|
|
|
|
if cur is None:
|
|
|
|
if cur is None:
|
|
|
|
return None
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# 子节点数量 = 0 or 1
|
|
|
|
# 子节点数量 = 0 or 1
|
|
|
|
if cur.left is None or cur.right is None:
|
|
|
|
if cur.left is None or cur.right is None:
|
|
|
@ -758,33 +732,24 @@ comments: true
|
|
|
|
# 子节点数量 = 2
|
|
|
|
# 子节点数量 = 2
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
# 获取中序遍历中 cur 的下一个节点
|
|
|
|
# 获取中序遍历中 cur 的下一个节点
|
|
|
|
nex: TreeNode = self.get_inorder_next(cur.right)
|
|
|
|
tmp: TreeNode = cur.right
|
|
|
|
tmp: int = nex.val
|
|
|
|
while tmp.left is not None:
|
|
|
|
# 递归删除节点 nex
|
|
|
|
tmp = tmp.left
|
|
|
|
self.remove(nex.val)
|
|
|
|
# 递归删除节点 tmp
|
|
|
|
# 将 nex 的值复制给 cur
|
|
|
|
self.remove(tmp.val)
|
|
|
|
cur.val = tmp
|
|
|
|
# 用 tmp 覆盖 cur
|
|
|
|
return cur
|
|
|
|
cur.val = tmp.val
|
|
|
|
|
|
|
|
|
|
|
|
def get_inorder_next(self, root: TreeNode | None) -> TreeNode | None:
|
|
|
|
|
|
|
|
"""获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)"""
|
|
|
|
|
|
|
|
if root is None:
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
# 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while root.left is not None:
|
|
|
|
|
|
|
|
root = root.left
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "Go"
|
|
|
|
=== "Go"
|
|
|
|
|
|
|
|
|
|
|
|
```go title="binary_search_tree.go"
|
|
|
|
```go title="binary_search_tree.go"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
func (bst *binarySearchTree) remove(num int) *TreeNode {
|
|
|
|
func (bst *binarySearchTree) remove(num int) {
|
|
|
|
cur := bst.root
|
|
|
|
cur := bst.root
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if cur == nil {
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 待删除节点之前的节点位置
|
|
|
|
// 待删除节点之前的节点位置
|
|
|
|
var pre *TreeNode = nil
|
|
|
|
var pre *TreeNode = nil
|
|
|
@ -804,7 +769,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if cur == nil {
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 子节点数为 0 或 1
|
|
|
|
// 子节点数为 0 或 1
|
|
|
|
if cur.Left == nil || cur.Right == nil {
|
|
|
|
if cur.Left == nil || cur.Right == nil {
|
|
|
@ -824,26 +789,15 @@ comments: true
|
|
|
|
// 子节点数为 2
|
|
|
|
// 子节点数为 2
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 获取中序遍历中待删除节点 cur 的下一个节点
|
|
|
|
// 获取中序遍历中待删除节点 cur 的下一个节点
|
|
|
|
next := bst.getInOrderNext(cur)
|
|
|
|
tmp := cur.Right
|
|
|
|
temp := next.Val
|
|
|
|
for tmp.Left != nil {
|
|
|
|
// 递归删除节点 next
|
|
|
|
tmp = tmp.Left
|
|
|
|
bst.remove(next.Val)
|
|
|
|
}
|
|
|
|
// 将 next 的值复制给 cur
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
cur.Val = temp
|
|
|
|
bst.remove(tmp.Val)
|
|
|
|
}
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
return cur
|
|
|
|
cur.Val = tmp.Val
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
|
|
|
|
|
|
|
|
if node == nil {
|
|
|
|
|
|
|
|
return node
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
for node.Left != nil {
|
|
|
|
|
|
|
|
node = node.Left
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -853,7 +807,7 @@ comments: true
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
function remove(num) {
|
|
|
|
function remove(num) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root === null) return null;
|
|
|
|
if (root === null) return;
|
|
|
|
let cur = root, pre = null;
|
|
|
|
let cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur !== null) {
|
|
|
|
while (cur !== null) {
|
|
|
@ -866,7 +820,7 @@ comments: true
|
|
|
|
else cur = cur.left;
|
|
|
|
else cur = cur.left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur === null) return null;
|
|
|
|
if (cur === null) return;
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur.left === null || cur.right === null) {
|
|
|
|
if (cur.left === null || cur.right === null) {
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
@ -878,24 +832,15 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
let nex = getInOrderNext(cur.right);
|
|
|
|
let tmp = cur.right;
|
|
|
|
let tmp = nex.val;
|
|
|
|
while (tmp.left !== null) {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp.left;
|
|
|
|
remove(nex.val);
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
cur.val = tmp;
|
|
|
|
remove(tmp.val);
|
|
|
|
}
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
return cur;
|
|
|
|
cur.val = tmp.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
function getInOrderNext(root) {
|
|
|
|
|
|
|
|
if (root === null) return root;
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (root.left !== null) {
|
|
|
|
|
|
|
|
root = root.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -903,10 +848,10 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```typescript title="binary_search_tree.ts"
|
|
|
|
```typescript title="binary_search_tree.ts"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
function remove(num: number): TreeNode | null {
|
|
|
|
function remove(num: number): void {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root === null) {
|
|
|
|
if (root === null) {
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let cur = root,
|
|
|
|
let cur = root,
|
|
|
|
pre: TreeNode | null = null;
|
|
|
|
pre: TreeNode | null = null;
|
|
|
@ -925,7 +870,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur === null) {
|
|
|
|
if (cur === null) {
|
|
|
|
return null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur.left === null || cur.right === null) {
|
|
|
|
if (cur.left === null || cur.right === null) {
|
|
|
@ -941,26 +886,15 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
let next = getInOrderNext(cur.right);
|
|
|
|
let tmp = cur.right;
|
|
|
|
let tmp = next!.val;
|
|
|
|
while (tmp.left !== null) {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp.left;
|
|
|
|
remove(next!.val);
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
cur.val = tmp;
|
|
|
|
remove(tmp!.val);
|
|
|
|
}
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
return cur;
|
|
|
|
cur.val = tmp.val;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
function getInOrderNext(root: TreeNode | null): TreeNode | null {
|
|
|
|
|
|
|
|
if (root === null) {
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (root.left !== null) {
|
|
|
|
|
|
|
|
root = root.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -968,18 +902,16 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```c title="binary_search_tree.c"
|
|
|
|
```c title="binary_search_tree.c"
|
|
|
|
[class]{binarySearchTree}-[func]{remove}
|
|
|
|
[class]{binarySearchTree}-[func]{remove}
|
|
|
|
|
|
|
|
|
|
|
|
[class]{binarySearchTree}-[func]{getInOrderNext}
|
|
|
|
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
=== "C#"
|
|
|
|
=== "C#"
|
|
|
|
|
|
|
|
|
|
|
|
```csharp title="binary_search_tree.cs"
|
|
|
|
```csharp title="binary_search_tree.cs"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
TreeNode? remove(int num)
|
|
|
|
void remove(int num)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (root == null) return null;
|
|
|
|
if (root == null) return;
|
|
|
|
TreeNode? cur = root, pre = null;
|
|
|
|
TreeNode? cur = root, pre = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
while (cur != null)
|
|
|
|
while (cur != null)
|
|
|
@ -993,7 +925,7 @@ comments: true
|
|
|
|
else cur = cur.left;
|
|
|
|
else cur = cur.left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur == null || pre == null) return null;
|
|
|
|
if (cur == null || pre == null) return;
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur.left == null || cur.right == null)
|
|
|
|
if (cur.left == null || cur.right == null)
|
|
|
|
{
|
|
|
|
{
|
|
|
@ -1013,29 +945,16 @@ comments: true
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
TreeNode? nex = getInOrderNext(cur.right);
|
|
|
|
TreeNode? tmp = cur.right;
|
|
|
|
if (nex != null)
|
|
|
|
while (tmp.left != null)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
int tmp = nex.val;
|
|
|
|
tmp = tmp.left;
|
|
|
|
// 递归删除节点 nex
|
|
|
|
|
|
|
|
remove(nex.val);
|
|
|
|
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
|
|
|
|
cur.val = tmp;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
|
|
|
|
remove(tmp.val);
|
|
|
|
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
|
|
|
|
cur.val = tmp.val;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return cur;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
TreeNode? getInOrderNext(TreeNode? root)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (root == null) return root;
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (root.left != null)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
root = root.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return root;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -1044,10 +963,10 @@ comments: true
|
|
|
|
```swift title="binary_search_tree.swift"
|
|
|
|
```swift title="binary_search_tree.swift"
|
|
|
|
/* 删除节点 */
|
|
|
|
/* 删除节点 */
|
|
|
|
@discardableResult
|
|
|
|
@discardableResult
|
|
|
|
func remove(num: Int) -> TreeNode? {
|
|
|
|
func remove(num: Int) {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if root == nil {
|
|
|
|
if root == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var cur = root
|
|
|
|
var cur = root
|
|
|
|
var pre: TreeNode?
|
|
|
|
var pre: TreeNode?
|
|
|
@ -1069,7 +988,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if cur == nil {
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if cur?.left == nil || cur?.right == nil {
|
|
|
|
if cur?.left == nil || cur?.right == nil {
|
|
|
@ -1085,27 +1004,15 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
else {
|
|
|
|
else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
let nex = getInOrderNext(root: cur?.right)
|
|
|
|
let tmp = cur?.right
|
|
|
|
let tmp = nex!.val
|
|
|
|
while tmp?.left != nil {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp?.left
|
|
|
|
remove(num: nex!.val)
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
cur?.val = tmp
|
|
|
|
remove(num: tmp!.val)
|
|
|
|
}
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
return cur
|
|
|
|
cur?.val = tmp!.val
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
|
|
|
|
|
|
|
func getInOrderNext(root: TreeNode?) -> TreeNode? {
|
|
|
|
|
|
|
|
var root = root
|
|
|
|
|
|
|
|
if root == nil {
|
|
|
|
|
|
|
|
return root
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while root?.left != nil {
|
|
|
|
|
|
|
|
root = root?.left
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return root
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
@ -1113,9 +1020,9 @@ comments: true
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="binary_search_tree.zig"
|
|
|
|
```zig title="binary_search_tree.zig"
|
|
|
|
// 删除节点
|
|
|
|
// 删除节点
|
|
|
|
fn remove(self: *Self, num: T) ?*inc.TreeNode(T) {
|
|
|
|
fn remove(self: *Self, num: T) !void {
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if (self.root == null) return null;
|
|
|
|
if (self.root == null) return;
|
|
|
|
var cur = self.root;
|
|
|
|
var cur = self.root;
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
|
|
|
var pre: ?*inc.TreeNode(T) = null;
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
|
// 循环查找,越过叶节点后跳出
|
|
|
@ -1132,7 +1039,7 @@ comments: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
// 若无待删除节点,则直接返回
|
|
|
|
if (cur == null) return null;
|
|
|
|
if (cur == null) return;
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
// 子节点数量 = 0 or 1
|
|
|
|
if (cur.?.left == null or cur.?.right == null) {
|
|
|
|
if (cur.?.left == null or cur.?.right == null) {
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
|
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
|
|
|
@ -1146,26 +1053,16 @@ comments: true
|
|
|
|
// 子节点数量 = 2
|
|
|
|
// 子节点数量 = 2
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
// 获取中序遍历中 cur 的下一个节点
|
|
|
|
var nex = self.getInOrderNext(cur.?.right);
|
|
|
|
var tmp = cur.?.right;
|
|
|
|
var tmp = nex.?.val;
|
|
|
|
while (tmp.?.left != null) {
|
|
|
|
// 递归删除节点 nex
|
|
|
|
tmp = tmp.?.left;
|
|
|
|
_ = self.remove(nex.?.val);
|
|
|
|
}
|
|
|
|
// 将 nex 的值复制给 cur
|
|
|
|
var tmpVal = tmp.?.val;
|
|
|
|
cur.?.val = tmp;
|
|
|
|
// 递归删除节点 tmp
|
|
|
|
}
|
|
|
|
_ = self.remove(tmp.?.val);
|
|
|
|
return cur;
|
|
|
|
// 用 tmp 覆盖 cur
|
|
|
|
}
|
|
|
|
cur.?.val = tmpVal;
|
|
|
|
|
|
|
|
|
|
|
|
// 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况)
|
|
|
|
|
|
|
|
fn getInOrderNext(self: *Self, node: ?*inc.TreeNode(T)) ?*inc.TreeNode(T) {
|
|
|
|
|
|
|
|
_ = self;
|
|
|
|
|
|
|
|
var node_tmp = node;
|
|
|
|
|
|
|
|
if (node_tmp == null) return null;
|
|
|
|
|
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
|
|
|
|
|
|
|
while (node_tmp.?.left != null) {
|
|
|
|
|
|
|
|
node_tmp = node_tmp.?.left;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return node_tmp;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|