mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-24 18:43:59 +08:00
build
This commit is contained in:
@@ -208,6 +208,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title=""
|
||||
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title=""
|
||||
@@ -419,6 +425,22 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 获取节点高度 */
|
||||
fun height(node: TreeNode?): Int {
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
return node?.height ?: -1
|
||||
}
|
||||
|
||||
/* 更新节点高度 */
|
||||
fun updateHeight(node: TreeNode?) {
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
node?.height = (max(height(node?.left).toDouble(), height(node?.right).toDouble()) + 1).toInt()
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -582,6 +604,18 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 获取平衡因子 */
|
||||
fun balanceFactor(node: TreeNode?): Int {
|
||||
// 空节点平衡因子为 0
|
||||
if (node == null) return 0
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node.left) - height(node.right)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -833,6 +867,24 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 右旋操作 */
|
||||
fun rightRotate(node: TreeNode?): TreeNode {
|
||||
val child = node!!.left
|
||||
val grandChild = child!!.right
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.right = node
|
||||
node.left = grandChild
|
||||
// 更新节点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根节点
|
||||
return child
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -1070,6 +1122,24 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 左旋操作 */
|
||||
fun leftRotate(node: TreeNode?): TreeNode {
|
||||
val child = node!!.right
|
||||
val grandChild = child!!.left
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.left = node
|
||||
node.right = grandChild
|
||||
// 更新节点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根节点
|
||||
return child
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -1504,6 +1574,40 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
fun rotate(node: TreeNode): TreeNode {
|
||||
// 获取节点 node 的平衡因子
|
||||
val balanceFactor = balanceFactor(node)
|
||||
// 左偏树
|
||||
if (balanceFactor > 1) {
|
||||
if (balanceFactor(node.left) >= 0) {
|
||||
// 右旋
|
||||
return rightRotate(node)
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.left = leftRotate(node.left)
|
||||
return rightRotate(node)
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if (balanceFactor < -1) {
|
||||
if (balanceFactor(node.right) <= 0) {
|
||||
// 左旋
|
||||
return leftRotate(node)
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.right = rightRotate(node.right)
|
||||
return leftRotate(node)
|
||||
}
|
||||
}
|
||||
// 平衡树,无须旋转,直接返回
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -1861,6 +1965,32 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 插入节点 */
|
||||
fun insert(value: Int) {
|
||||
root = insertHelper(root, value)
|
||||
}
|
||||
|
||||
/* 递归插入节点(辅助方法) */
|
||||
fun insertHelper(n: TreeNode?, value: Int): TreeNode {
|
||||
if (n == null)
|
||||
return TreeNode(value)
|
||||
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)
|
||||
else return node // 重复节点不插入,直接返回
|
||||
|
||||
updateHeight(node) // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根节点
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
@@ -2408,6 +2538,44 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
|
||||
}
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="avl_tree.kt"
|
||||
/* 删除节点 */
|
||||
fun remove(value: Int) {
|
||||
root = removeHelper(root, value)
|
||||
}
|
||||
|
||||
/* 递归删除节点(辅助方法) */
|
||||
fun removeHelper(n: TreeNode?, value: 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)
|
||||
else {
|
||||
if (node.left == null || node.right == null) {
|
||||
val child = if (node.left != null) node.left else node.right
|
||||
// 子节点数量 = 0 ,直接删除 node 并返回
|
||||
if (child == null) return null
|
||||
else node = child
|
||||
} else {
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
var temp = node.right
|
||||
while (temp!!.left != null) {
|
||||
temp = temp.left
|
||||
}
|
||||
node.right = removeHelper(node.right, temp.value)
|
||||
node.value = temp.value
|
||||
}
|
||||
}
|
||||
updateHeight(node) // 更新节点高度
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根节点
|
||||
return node
|
||||
}
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
||||
```zig title="avl_tree.zig"
|
||||
|
||||
Reference in New Issue
Block a user