mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-27 20:32:46 +08:00
build
This commit is contained in:
@ -1180,7 +1180,7 @@ The following code implements a binary tree based on array representation, inclu
|
||||
}
|
||||
|
||||
/* 获取索引为 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 @@ The following code implements a binary tree based on array representation, inclu
|
||||
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 @@ The following code implements a binary tree based on array representation, inclu
|
||||
/* 深度优先遍历 */
|
||||
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 @@ The node insertion operation in AVL trees is similar to that in binary search tr
|
||||
|
||||
```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 @@ Similarly, based on the method of removing nodes in binary search trees, rotatio
|
||||
|
||||
```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 @@ Similarly, based on the method of removing nodes in binary search trees, rotatio
|
||||
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 @@ 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
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -305,7 +305,7 @@ Breadth-first traversal is usually implemented with the help of a "queue". The q
|
||||
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 @@ Depth-first search is usually implemented based on recursion:
|
||||
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 @@ Depth-first search is usually implemented based on recursion:
|
||||
if (root == null) return
|
||||
// 访问优先级:左子树 -> 根节点 -> 右子树
|
||||
inOrder(root.left)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
inOrder(root.right)
|
||||
}
|
||||
|
||||
@ -784,7 +784,7 @@ Depth-first search is usually implemented based on recursion:
|
||||
// 访问优先级:左子树 -> 右子树 -> 根节点
|
||||
postOrder(root.left)
|
||||
postOrder(root.right)
|
||||
list.add(root.value)
|
||||
list.add(root._val)
|
||||
}
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user