This commit is contained in:
krahets
2024-03-25 22:43:12 +08:00
parent 22017aa8e5
commit 87af663929
70 changed files with 7428 additions and 32 deletions

View File

@ -120,6 +120,12 @@ comments: true
int tree[] = {1, 2, 3, 4, INT_MAX, 6, 7, 8, 9, INT_MAX, INT_MAX, 12, INT_MAX, INT_MAX, 15};
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""
@ -1155,6 +1161,85 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="array_binary_tree.kt"
/* 数组表示下的二叉树类 */
class ArrayBinaryTree(val tree: List<Int?>) {
/* 列表容量 */
fun size(): Int {
return tree.size
}
/* 获取索引为 i 节点的值 */
fun value(i: Int): Int? {
// 若索引越界,则返回 null ,代表空位
if (i < 0 || i >= size()) return null
return tree[i]
}
/* 获取索引为 i 节点的左子节点的索引 */
fun left(i: Int): Int {
return 2 * i + 1
}
/* 获取索引为 i 节点的右子节点的索引 */
fun right(i: Int): Int {
return 2 * i + 2
}
/* 获取索引为 i 节点的父节点的索引 */
fun parent(i: Int): Int {
return (i - 1) / 2
}
/* 层序遍历 */
fun levelOrder(): List<Int?> {
val res = ArrayList<Int?>()
// 直接遍历数组
for (i in 0..<size()) {
if (value(i) != null) res.add(value(i))
}
return res
}
/* 深度优先遍历 */
fun dfs(i: Int, order: String, res: MutableList<Int?>) {
// 若为空位,则返回
if (value(i) == null) return
// 前序遍历
if ("pre" == order) res.add(value(i))
dfs(left(i), order, res)
// 中序遍历
if ("in" == order) res.add(value(i))
dfs(right(i), order, res)
// 后序遍历
if ("post" == order) res.add(value(i))
}
/* 前序遍历 */
fun preOrder(): List<Int?> {
val res = ArrayList<Int?>()
dfs(0, "pre", res)
return res
}
/* 中序遍历 */
fun inOrder(): List<Int?> {
val res = ArrayList<Int?>()
dfs(0, "in", res)
return res
}
/* 后序遍历 */
fun postOrder(): List<Int?> {
val res = ArrayList<Int?>()
dfs(0, "post", res)
return res
}
}
```
=== "Zig"
```zig title="array_binary_tree.zig"

View File

@ -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"

View File

@ -290,6 +290,26 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="binary_search_tree.kt"
/* 查找节点 */
fun search(num: Int): TreeNode? {
var cur = root
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 目标节点在 cur 的右子树中
cur = if (cur.value < num) cur.right
// 目标节点在 cur 的左子树中
else if (cur.value > num) cur.left
// 找到目标节点,跳出循环
else break
}
// 返回目标节点
return cur
}
```
=== "Zig"
```zig title="binary_search_tree.zig"
@ -707,6 +727,35 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="binary_search_tree.kt"
/* 插入节点 */
fun insert(num: Int) {
// 若树为空,则初始化根节点
if (root == null) {
root = TreeNode(num)
return
}
var cur = root
var pre: TreeNode? = null
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到重复节点,直接返回
if (cur.value == num) return
pre = cur
// 插入位置在 cur 的右子树中
cur = if (cur.value < num) cur.right
// 插入位置在 cur 的左子树中
else cur.left
}
// 插入节点
val node = TreeNode(num)
if (pre?.value!! < num) pre.right = node
else pre.left = node
}
```
=== "Zig"
```zig title="binary_search_tree.zig"
@ -1415,6 +1464,54 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="binary_search_tree.kt"
/* 删除节点 */
fun remove(num: Int) {
// 若树为空,直接提前返回
if (root == null) return
var cur = root
var pre: TreeNode? = null
// 循环查找,越过叶节点后跳出
while (cur != null) {
// 找到待删除节点,跳出循环
if (cur.value == num) break
pre = cur
// 待删除节点在 cur 的右子树中
cur = if (cur.value < num) cur.right
// 待删除节点在 cur 的左子树中
else cur.left
}
// 若无待删除节点,则直接返回
if (cur == null) return
// 子节点数量 = 0 or 1
if (cur.left == null || cur.right == null) {
// 当子节点数量 = 0 / 1 时, child = null / 该子节点
val child = if (cur.left != null) cur.left else cur.right
// 删除节点 cur
if (cur != root) {
if (pre!!.left == cur) pre.left = child
else pre.right = child
} else {
// 若删除节点为根节点,则重新指定根节点
root = child
}
// 子节点数量 = 2
} else {
// 获取中序遍历中 cur 的下一个节点
var tmp = cur.right
while (tmp!!.left != null) {
tmp = tmp.left
}
// 递归删除节点 tmp
remove(tmp.value)
// 用 tmp 覆盖 cur
cur.value = tmp.value
}
}
```
=== "Zig"
```zig title="binary_search_tree.zig"

View File

@ -180,6 +180,12 @@ comments: true
}
```
=== "Kotlin"
```kotlin title=""
```
=== "Zig"
```zig title=""
@ -405,6 +411,12 @@ comments: true
n2->right = n5;
```
=== "Kotlin"
```kotlin title="binary_tree.kt"
```
=== "Zig"
```zig title="binary_tree.zig"
@ -553,6 +565,12 @@ comments: true
n1->left = n2;
```
=== "Kotlin"
```kotlin title="binary_tree.kt"
```
=== "Zig"
```zig title="binary_tree.zig"

View File

@ -293,6 +293,27 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="binary_tree_bfs.kt"
/* 层序遍历 */
fun levelOrder(root: TreeNode?): MutableList<Int> {
// 初始化队列,加入根节点
val queue = LinkedList<TreeNode?>()
queue.add(root)
// 初始化一个列表,用于保存遍历序列
val list = ArrayList<Int>()
while (!queue.isEmpty()) {
val node = queue.poll() // 队列出队
list.add(node?.value!!) // 保存节点值
if (node.left != null) queue.offer(node.left) // 左子节点入队
if (node.right != null) queue.offer(node.right) // 右子节点入队
}
return list
}
```
=== "Zig"
```zig title="binary_tree_bfs.zig"
@ -729,6 +750,37 @@ comments: true
}
```
=== "Kotlin"
```kotlin title="binary_tree_dfs.kt"
/* 前序遍历 */
fun preOrder(root: TreeNode?) {
if (root == null) return
// 访问优先级:根节点 -> 左子树 -> 右子树
list.add(root.value)
preOrder(root.left)
preOrder(root.right)
}
/* 中序遍历 */
fun inOrder(root: TreeNode?) {
if (root == null) return
// 访问优先级:左子树 -> 根节点 -> 右子树
inOrder(root.left)
list.add(root.value)
inOrder(root.right)
}
/* 后序遍历 */
fun postOrder(root: TreeNode?) {
if (root == null) return
// 访问优先级:左子树 -> 右子树 -> 根节点
postOrder(root.left)
postOrder(root.right)
list.add(root.value)
}
```
=== "Zig"
```zig title="binary_tree_dfs.zig"