This commit is contained in:
krahets
2024-04-11 01:11:20 +08:00
parent a6adc8e20a
commit 739f8a31bb
85 changed files with 1555 additions and 979 deletions

View File

@ -299,10 +299,10 @@ The search operation in a binary search tree works on the same principle as the
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 目标节点在 cur 的右子树中
cur = if (cur.value < num)
cur = if (cur._val < num)
cur.right
// 目标节点在 cur 的左子树中
else if (cur.value > num)
else if (cur._val > num)
cur.left
// 找到目标节点,跳出循环
else
@ -751,11 +751,11 @@ In the code implementation, note the following two points.
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到重复节点,直接返回
if (cur.value == num)
if (cur._val == num)
return
pre = cur
// 插入位置在 cur 的右子树中
cur = if (cur.value < num)
cur = if (cur._val < num)
cur.right
// 插入位置在 cur 的左子树中
else
@ -763,7 +763,7 @@ In the code implementation, note the following two points.
}
// 插入节点
val node = TreeNode(num)
if (pre?.value!! < num)
if (pre?._val!! < num)
pre.right = node
else
pre.left = node
@ -1497,11 +1497,11 @@ The operation of removing a node also uses $O(\log n)$ time, where finding the n
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到待删除节点,跳出循环
if (cur.value == num)
if (cur._val == num)
break
pre = cur
// 待删除节点在 cur 的右子树中
cur = if (cur.value < num)
cur = if (cur._val < num)
cur.right
// 待删除节点在 cur 的左子树中
else
@ -1535,9 +1535,9 @@ The operation of removing a node also uses $O(\log n)$ time, where finding the n
tmp = tmp.left
}
// 递归删除节点 tmp
remove(tmp.value)
remove(tmp._val)
// 用 tmp 覆盖 cur
cur.value = tmp.value
cur._val = tmp._val
}
}
```