mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-28 04:42:48 +08:00
build
This commit is contained in:
@ -1253,7 +1253,7 @@ comments: true
|
||||
var index = 0
|
||||
var h = head
|
||||
while (h != null) {
|
||||
if (h.value == target)
|
||||
if (h._val == target)
|
||||
return index
|
||||
h = h.next
|
||||
index++
|
||||
|
@ -2309,7 +2309,7 @@ comments: true
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
extend_capacity if size == capacity
|
||||
@arr[size] = num
|
||||
|
||||
|
||||
# 更新元素数量
|
||||
@size += 1
|
||||
end
|
||||
@ -2323,7 +2323,7 @@ comments: true
|
||||
|
||||
# 将索引 index 以及之后的元素都向后移动一位
|
||||
for j in (size - 1).downto(index)
|
||||
@arr[j + 1] = @arr[j]
|
||||
@arr[j + 1] = @arr[j]
|
||||
end
|
||||
@arr[index] = num
|
||||
|
||||
|
@ -208,7 +208,7 @@ comments: true
|
||||
if (root == null) {
|
||||
return
|
||||
}
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 记录解
|
||||
res!!.add(root)
|
||||
}
|
||||
@ -508,7 +508,7 @@ comments: true
|
||||
}
|
||||
// 尝试
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 记录解
|
||||
res!!.add(path!!.toMutableList())
|
||||
}
|
||||
@ -847,12 +847,12 @@ comments: true
|
||||
/* 前序遍历:例题三 */
|
||||
fun preOrder(root: TreeNode?) {
|
||||
// 剪枝
|
||||
if (root == null || root.value == 3) {
|
||||
if (root == null || root._val == 3) {
|
||||
return
|
||||
}
|
||||
// 尝试
|
||||
path!!.add(root)
|
||||
if (root.value == 7) {
|
||||
if (root._val == 7) {
|
||||
// 记录解
|
||||
res!!.add(path!!.toMutableList())
|
||||
}
|
||||
@ -1791,7 +1791,7 @@ comments: true
|
||||
```kotlin title="preorder_traversal_iii_template.kt"
|
||||
/* 判断当前状态是否为解 */
|
||||
fun isSolution(state: MutableList<TreeNode?>): Boolean {
|
||||
return state.isNotEmpty() && state[state.size - 1]?.value == 7
|
||||
return state.isNotEmpty() && state[state.size - 1]?._val == 7
|
||||
}
|
||||
|
||||
/* 记录解 */
|
||||
@ -1801,7 +1801,7 @@ comments: true
|
||||
|
||||
/* 判断在当前状态下,该选择是否合法 */
|
||||
fun isValid(state: MutableList<TreeNode?>?, choice: TreeNode?): Boolean {
|
||||
return choice != null && choice.value != 3
|
||||
return choice != null && choice._val != 3
|
||||
}
|
||||
|
||||
/* 更新状态 */
|
||||
|
@ -301,7 +301,7 @@ $$
|
||||
/* 0-1 背包:暴力搜索 */
|
||||
fun knapsackDFS(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
_val: IntArray,
|
||||
i: Int,
|
||||
c: Int
|
||||
): Int {
|
||||
@ -311,11 +311,11 @@ $$
|
||||
}
|
||||
// 若超过背包容量,则只能选择不放入背包
|
||||
if (wgt[i - 1] > c) {
|
||||
return knapsackDFS(wgt, value, i - 1, c)
|
||||
return knapsackDFS(wgt, _val, i - 1, c)
|
||||
}
|
||||
// 计算不放入和放入物品 i 的最大价值
|
||||
val no = knapsackDFS(wgt, value, i - 1, c)
|
||||
val yes = knapsackDFS(wgt, value, i - 1, c - wgt[i - 1]) + value[i - 1]
|
||||
val no = knapsackDFS(wgt, _val, i - 1, c)
|
||||
val yes = knapsackDFS(wgt, _val, i - 1, c - wgt[i - 1]) + _val[i - 1]
|
||||
// 返回两种方案中价值更大的那一个
|
||||
return max(no, yes)
|
||||
}
|
||||
@ -671,7 +671,7 @@ $$
|
||||
/* 0-1 背包:记忆化搜索 */
|
||||
fun knapsackDFSMem(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
_val: IntArray,
|
||||
mem: Array<IntArray>,
|
||||
i: Int,
|
||||
c: Int
|
||||
@ -686,11 +686,11 @@ $$
|
||||
}
|
||||
// 若超过背包容量,则只能选择不放入背包
|
||||
if (wgt[i - 1] > c) {
|
||||
return knapsackDFSMem(wgt, value, mem, i - 1, c)
|
||||
return knapsackDFSMem(wgt, _val, mem, i - 1, c)
|
||||
}
|
||||
// 计算不放入和放入物品 i 的最大价值
|
||||
val no = knapsackDFSMem(wgt, value, mem, i - 1, c)
|
||||
val yes = knapsackDFSMem(wgt, value, mem, i - 1, c - wgt[i - 1]) + value[i - 1]
|
||||
val no = knapsackDFSMem(wgt, _val, mem, i - 1, c)
|
||||
val yes = knapsackDFSMem(wgt, _val, mem, i - 1, c - wgt[i - 1]) + _val[i - 1]
|
||||
// 记录并返回两种方案中价值更大的那一个
|
||||
mem[i][c] = max(no, yes)
|
||||
return mem[i][c]
|
||||
@ -1038,7 +1038,7 @@ $$
|
||||
/* 0-1 背包:动态规划 */
|
||||
fun knapsackDP(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
_val: IntArray,
|
||||
cap: Int
|
||||
): Int {
|
||||
val n = wgt.size
|
||||
@ -1052,7 +1052,7 @@ $$
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + value[i - 1])
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i - 1][c - wgt[i - 1]] + _val[i - 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1431,7 +1431,7 @@ $$
|
||||
/* 0-1 背包:空间优化后的动态规划 */
|
||||
fun knapsackDPComp(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
_val: IntArray,
|
||||
cap: Int
|
||||
): Int {
|
||||
val n = wgt.size
|
||||
@ -1444,7 +1444,7 @@ $$
|
||||
if (wgt[i - 1] <= c) {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] =
|
||||
max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
|
||||
max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -327,7 +327,7 @@ $$
|
||||
|
||||
```kotlin title="unbounded_knapsack.kt"
|
||||
/* 完全背包:动态规划 */
|
||||
fun unboundedKnapsackDP(wgt: IntArray, value: IntArray, cap: Int): Int {
|
||||
fun unboundedKnapsackDP(wgt: IntArray, _val: IntArray, cap: Int): Int {
|
||||
val n = wgt.size
|
||||
// 初始化 dp 表
|
||||
val dp = Array(n + 1) { IntArray(cap + 1) }
|
||||
@ -339,7 +339,7 @@ $$
|
||||
dp[i][c] = dp[i - 1][c]
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + value[i - 1])
|
||||
dp[i][c] = max(dp[i - 1][c], dp[i][c - wgt[i - 1]] + _val[i - 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -684,7 +684,7 @@ $$
|
||||
/* 完全背包:空间优化后的动态规划 */
|
||||
fun unboundedKnapsackDPComp(
|
||||
wgt: IntArray,
|
||||
value: IntArray,
|
||||
_val: IntArray,
|
||||
cap: Int
|
||||
): Int {
|
||||
val n = wgt.size
|
||||
@ -698,7 +698,7 @@ $$
|
||||
dp[c] = dp[c]
|
||||
} else {
|
||||
// 不选和选物品 i 这两种方案的较大值
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + value[i - 1])
|
||||
dp[c] = max(dp[c], dp[c - wgt[i - 1]] + _val[i - 1])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1066,10 +1066,10 @@ comments: true
|
||||
}
|
||||
|
||||
/* 添加顶点 */
|
||||
fun addVertex(value: Int) {
|
||||
fun addVertex(_val: Int) {
|
||||
val n = size()
|
||||
// 向顶点列表中添加新顶点的值
|
||||
vertices.add(value)
|
||||
vertices.add(_val)
|
||||
// 在邻接矩阵中添加一行
|
||||
val newRow = mutableListOf<Int>()
|
||||
for (j in 0..<n) {
|
||||
@ -2222,9 +2222,9 @@ comments: true
|
||||
for (pair in adjList.entries) {
|
||||
val tmp = mutableListOf<Int>()
|
||||
for (vertex in pair.value) {
|
||||
tmp.add(vertex.value)
|
||||
tmp.add(vertex._val)
|
||||
}
|
||||
println("${pair.key.value}: $tmp,")
|
||||
println("${pair.key._val}: $tmp,")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1347,14 +1347,14 @@ comments: true
|
||||
val bucket = buckets[index]
|
||||
// 遍历桶,若找到 key ,则返回对应 val
|
||||
for (pair in bucket) {
|
||||
if (pair.key == key) return pair.value
|
||||
if (pair.key == key) return pair._val
|
||||
}
|
||||
// 若未找到 key ,则返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
fun put(key: Int, _val: String) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor() > loadThres) {
|
||||
extend()
|
||||
@ -1364,12 +1364,12 @@ comments: true
|
||||
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||
for (pair in bucket) {
|
||||
if (pair.key == key) {
|
||||
pair.value = value
|
||||
pair._val = _val
|
||||
return
|
||||
}
|
||||
}
|
||||
// 若无该 key ,则将键值对添加至尾部
|
||||
val pair = Pair(key, value)
|
||||
val pair = Pair(key, _val)
|
||||
bucket.add(pair)
|
||||
size++
|
||||
}
|
||||
@ -1403,7 +1403,7 @@ comments: true
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
for (bucket in bucketsTmp) {
|
||||
for (pair in bucket) {
|
||||
put(pair.key, pair.value)
|
||||
put(pair.key, pair._val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1414,7 +1414,7 @@ comments: true
|
||||
val res = mutableListOf<String>()
|
||||
for (pair in bucket) {
|
||||
val k = pair.key
|
||||
val v = pair.value
|
||||
val v = pair._val
|
||||
res.add("$k -> $v")
|
||||
}
|
||||
println(res)
|
||||
@ -3017,14 +3017,14 @@ comments: true
|
||||
val index = findBucket(key)
|
||||
// 若找到键值对,则返回对应 val
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
return buckets[index]?.value
|
||||
return buckets[index]?._val
|
||||
}
|
||||
// 若键值对不存在,则返回 null
|
||||
return null
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
fun put(key: Int, _val: String) {
|
||||
// 当负载因子超过阈值时,执行扩容
|
||||
if (loadFactor() > loadThres) {
|
||||
extend()
|
||||
@ -3033,11 +3033,11 @@ comments: true
|
||||
val index = findBucket(key)
|
||||
// 若找到键值对,则覆盖 val 并返回
|
||||
if (buckets[index] != null && buckets[index] != TOMBSTONE) {
|
||||
buckets[index]!!.value = value
|
||||
buckets[index]!!._val = _val
|
||||
return
|
||||
}
|
||||
// 若键值对不存在,则添加该键值对
|
||||
buckets[index] = Pair(key, value)
|
||||
buckets[index] = Pair(key, _val)
|
||||
size++
|
||||
}
|
||||
|
||||
@ -3063,7 +3063,7 @@ comments: true
|
||||
// 将键值对从原哈希表搬运至新哈希表
|
||||
for (pair in bucketsTmp) {
|
||||
if (pair != null && pair != TOMBSTONE) {
|
||||
put(pair.key, pair.value)
|
||||
put(pair.key, pair._val)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -3076,7 +3076,7 @@ comments: true
|
||||
} else if (pair == TOMBSTONE) {
|
||||
println("TOMESTOME")
|
||||
} else {
|
||||
println("${pair.key} -> ${pair.value}")
|
||||
println("${pair.key} -> ${pair._val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1584,7 +1584,7 @@ index = hash(key) % capacity
|
||||
/* 键值对 */
|
||||
class Pair(
|
||||
var key: Int,
|
||||
var value: String
|
||||
var _val: String
|
||||
)
|
||||
|
||||
/* 基于数组实现的哈希表 */
|
||||
@ -1602,12 +1602,12 @@ index = hash(key) % capacity
|
||||
fun get(key: Int): String? {
|
||||
val index = hashFunc(key)
|
||||
val pair = buckets[index] ?: return null
|
||||
return pair.value
|
||||
return pair._val
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
val pair = Pair(key, value)
|
||||
fun put(key: Int, _val: String) {
|
||||
val pair = Pair(key, _val)
|
||||
val index = hashFunc(key)
|
||||
buckets[index] = pair
|
||||
}
|
||||
@ -1643,7 +1643,7 @@ index = hash(key) % capacity
|
||||
fun valueSet(): MutableList<String> {
|
||||
val valueSet = mutableListOf<String>()
|
||||
for (pair in buckets) {
|
||||
pair?.let { valueSet.add(it.value) }
|
||||
pair?.let { valueSet.add(it._val) }
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
@ -1652,8 +1652,8 @@ index = hash(key) % capacity
|
||||
fun print() {
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val value = kv.value
|
||||
println("${key} -> ${value}")
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1673,12 +1673,12 @@ index = hash(key) % capacity
|
||||
fun get(key: Int): String? {
|
||||
val index = hashFunc(key)
|
||||
val pair = buckets[index] ?: return null
|
||||
return pair.value
|
||||
return pair._val
|
||||
}
|
||||
|
||||
/* 添加操作 */
|
||||
fun put(key: Int, value: String) {
|
||||
val pair = Pair(key, value)
|
||||
fun put(key: Int, _val: String) {
|
||||
val pair = Pair(key, _val)
|
||||
val index = hashFunc(key)
|
||||
buckets[index] = pair
|
||||
}
|
||||
@ -1714,7 +1714,7 @@ index = hash(key) % capacity
|
||||
fun valueSet(): MutableList<String> {
|
||||
val valueSet = mutableListOf<String>()
|
||||
for (pair in buckets) {
|
||||
pair?.let { valueSet.add(it.value) }
|
||||
pair?.let { valueSet.add(it._val) }
|
||||
}
|
||||
return valueSet
|
||||
}
|
||||
@ -1723,8 +1723,8 @@ index = hash(key) % capacity
|
||||
fun print() {
|
||||
for (kv in pairSet()) {
|
||||
val key = kv.key
|
||||
val value = kv.value
|
||||
println("${key} -> ${value}")
|
||||
val _val = kv._val
|
||||
println("${key} -> ${_val}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -240,9 +240,9 @@ comments: true
|
||||
}
|
||||
|
||||
/* 元素入堆 */
|
||||
fun push(value: Int) {
|
||||
fun push(_val: Int) {
|
||||
// 添加节点
|
||||
maxHeap.add(value)
|
||||
maxHeap.add(_val)
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1)
|
||||
}
|
||||
@ -270,11 +270,11 @@ comments: true
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
swap(0, size() - 1)
|
||||
// 删除节点
|
||||
val value = maxHeap.removeAt(size() - 1)
|
||||
val _val = maxHeap.removeAt(size() - 1)
|
||||
// 从顶至底堆化
|
||||
siftDown(0)
|
||||
// 返回堆顶元素
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
|
@ -1184,9 +1184,9 @@ comments: true
|
||||
|
||||
```kotlin title="my_heap.kt"
|
||||
/* 元素入堆 */
|
||||
fun push(value: Int) {
|
||||
fun push(_val: Int) {
|
||||
// 添加节点
|
||||
maxHeap.add(value)
|
||||
maxHeap.add(_val)
|
||||
// 从底至顶堆化
|
||||
siftUp(size() - 1)
|
||||
}
|
||||
@ -1736,11 +1736,11 @@ comments: true
|
||||
// 交换根节点与最右叶节点(交换首元素与尾元素)
|
||||
swap(0, size() - 1)
|
||||
// 删除节点
|
||||
val value = maxHeap.removeAt(size() - 1)
|
||||
val _val = maxHeap.removeAt(size() - 1)
|
||||
// 从顶至底堆化
|
||||
siftDown(0)
|
||||
// 返回堆顶元素
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 从节点 i 开始,从顶至底堆化 */
|
||||
|
@ -176,7 +176,12 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### 标题注释,用于标注函数、类、测试样例等 ###
|
||||
|
||||
# 内容注释,用于详解代码
|
||||
|
||||
# 多行
|
||||
# 注释
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
@ -170,12 +170,13 @@ $$
|
||||
if (num > m)
|
||||
m = num;
|
||||
// 按照从低位到高位的顺序遍历
|
||||
for (int exp = 1; exp <= m; exp *= 10)
|
||||
for (int exp = 1; exp <= m; exp *= 10) {
|
||||
// 对数组元素的第 k 位执行计数排序
|
||||
// k = 1 -> exp = 1
|
||||
// k = 2 -> exp = 10
|
||||
// 即 exp = 10^(k-1)
|
||||
countingSortDigit(nums, exp);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -1958,10 +1958,10 @@ comments: true
|
||||
fun pop(isFront: Boolean): Int {
|
||||
if (isEmpty())
|
||||
throw IndexOutOfBoundsException()
|
||||
val value: Int
|
||||
val _val: Int
|
||||
// 队首出队操作
|
||||
if (isFront) {
|
||||
value = front!!._val // 暂存头节点值
|
||||
_val = front!!._val // 暂存头节点值
|
||||
// 删除头节点
|
||||
val fNext = front!!.next
|
||||
if (fNext != null) {
|
||||
@ -1971,7 +1971,7 @@ comments: true
|
||||
front = fNext // 更新头节点
|
||||
// 队尾出队操作
|
||||
} else {
|
||||
value = rear!!._val // 暂存尾节点值
|
||||
_val = rear!!._val // 暂存尾节点值
|
||||
// 删除尾节点
|
||||
val rPrev = rear!!.prev
|
||||
if (rPrev != null) {
|
||||
@ -1981,7 +1981,7 @@ comments: true
|
||||
rear = rPrev // 更新尾节点
|
||||
}
|
||||
queSize-- // 更新队列长度
|
||||
return value
|
||||
return _val
|
||||
}
|
||||
|
||||
/* 队首出队 */
|
||||
|
@ -1180,7 +1180,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 获取索引为 i 节点的值 */
|
||||
fun value(i: Int): Int? {
|
||||
fun _val(i: Int): Int? {
|
||||
// 若索引越界,则返回 null ,代表空位
|
||||
if (i < 0 || i >= size()) return null
|
||||
return tree[i]
|
||||
@ -1206,8 +1206,8 @@ comments: true
|
||||
val res = mutableListOf<Int?>()
|
||||
// 直接遍历数组
|
||||
for (i in 0..<size()) {
|
||||
if (value(i) != null)
|
||||
res.add(value(i))
|
||||
if (_val(i) != null)
|
||||
res.add(_val(i))
|
||||
}
|
||||
return res
|
||||
}
|
||||
@ -1215,19 +1215,19 @@ comments: true
|
||||
/* 深度优先遍历 */
|
||||
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
|
||||
// 若为空位,则返回
|
||||
if (value(i) == null)
|
||||
if (_val(i) == null)
|
||||
return
|
||||
// 前序遍历
|
||||
if ("pre" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(left(i), order, res)
|
||||
// 中序遍历
|
||||
if ("in" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
dfs(right(i), order, res)
|
||||
// 后序遍历
|
||||
if ("post" == order)
|
||||
res.add(value(i))
|
||||
res.add(_val(i))
|
||||
}
|
||||
|
||||
/* 前序遍历 */
|
||||
|
@ -2012,20 +2012,20 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 插入节点 */
|
||||
fun insert(value: Int) {
|
||||
root = insertHelper(root, value)
|
||||
fun insert(_val: Int) {
|
||||
root = insertHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
fun insertHelper(n: TreeNode?, value: Int): TreeNode {
|
||||
fun insertHelper(n: TreeNode?, _val: Int): TreeNode {
|
||||
if (n == null)
|
||||
return TreeNode(value)
|
||||
return TreeNode(_val)
|
||||
var node = n
|
||||
/* 1. 查找插入位置并插入节点 */
|
||||
if (value < node.value)
|
||||
node.left = insertHelper(node.left, value)
|
||||
else if (value > node.value)
|
||||
node.right = insertHelper(node.right, value)
|
||||
if (_val < node._val)
|
||||
node.left = insertHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = insertHelper(node.right, _val)
|
||||
else
|
||||
return node // 重复节点不插入,直接返回
|
||||
updateHeight(node) // 更新节点高度
|
||||
@ -2595,18 +2595,18 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 删除节点 */
|
||||
fun remove(value: Int) {
|
||||
root = removeHelper(root, value)
|
||||
fun remove(_val: Int) {
|
||||
root = removeHelper(root, _val)
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
fun removeHelper(n: TreeNode?, value: Int): TreeNode? {
|
||||
fun removeHelper(n: TreeNode?, _val: Int): TreeNode? {
|
||||
var node = n ?: return null
|
||||
/* 1. 查找节点并删除 */
|
||||
if (value < node.value)
|
||||
node.left = removeHelper(node.left, value)
|
||||
else if (value > node.value)
|
||||
node.right = removeHelper(node.right, value)
|
||||
if (_val < node._val)
|
||||
node.left = removeHelper(node.left, _val)
|
||||
else if (_val > node._val)
|
||||
node.right = removeHelper(node.right, _val)
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
val child = if (node.left != null)
|
||||
@ -2625,8 +2625,8 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
while (temp!!.left != null) {
|
||||
temp = temp.left
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.value)
|
||||
node.value = temp.value
|
||||
node.right = removeHelper(node.right, temp._val)
|
||||
node._val = temp._val
|
||||
}
|
||||
}
|
||||
updateHeight(node) // 更新节点高度
|
||||
|
@ -299,10 +299,10 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
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 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
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 @@ comments: true
|
||||
}
|
||||
// 插入节点
|
||||
val node = TreeNode(num)
|
||||
if (pre?.value!! < num)
|
||||
if (pre?._val!! < num)
|
||||
pre.right = node
|
||||
else
|
||||
pre.left = node
|
||||
@ -1497,11 +1497,11 @@ comments: true
|
||||
// 循环查找,越过叶节点后跳出
|
||||
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 @@ comments: true
|
||||
tmp = tmp.left
|
||||
}
|
||||
// 递归删除节点 tmp
|
||||
remove(tmp.value)
|
||||
remove(tmp._val)
|
||||
// 用 tmp 覆盖 cur
|
||||
cur.value = tmp.value
|
||||
cur._val = tmp._val
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -305,7 +305,7 @@ comments: true
|
||||
val list = mutableListOf<Int>()
|
||||
while (queue.isNotEmpty()) {
|
||||
val node = queue.poll() // 队列出队
|
||||
list.add(node?.value!!) // 保存节点值
|
||||
list.add(node?._val!!) // 保存节点值
|
||||
if (node.left != null)
|
||||
queue.offer(node.left) // 左子节点入队
|
||||
if (node.right != null)
|
||||
@ -764,7 +764,7 @@ comments: true
|
||||
fun preOrder(root: TreeNode?) {
|
||||
if (root == null) return
|
||||
// 访问优先级:根节点 -> 左子树 -> 右子树
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
preOrder(root.left)
|
||||
preOrder(root.right)
|
||||
}
|
||||
@ -774,7 +774,7 @@ comments: true
|
||||
if (root == null) return
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
inOrder(root.right)
|
||||
}
|
||||
|
||||
@ -784,7 +784,7 @@ comments: true
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left)
|
||||
postOrder(root.right)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user