This commit is contained in:
krahets
2024-03-31 03:53:04 +08:00
parent 87af663929
commit c23e576da4
68 changed files with 2139 additions and 22 deletions

View File

@ -123,6 +123,14 @@ comments: true
=== "Kotlin"
```kotlin title=""
/* 二叉树的数组表示 */
// 使用 null 来表示空位
val tree = mutableListOf( 1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15 )
```
=== "Ruby"
```ruby title=""
```
@ -1240,6 +1248,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="array_binary_tree.rb"
[class]{ArrayBinaryTree}-[func]{}
```
=== "Zig"
```zig title="array_binary_tree.zig"

View File

@ -211,6 +211,17 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
=== "Kotlin"
```kotlin title=""
/* AVL 树节点类 */
class TreeNode(val _val: Int) { // 节点值
val height: Int = 0 // 节点高度
val left: TreeNode? = null // 左子节点
val right: TreeNode? = null // 右子节点
}
```
=== "Ruby"
```ruby title=""
```
@ -441,6 +452,14 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{height}
[class]{AVLTree}-[func]{update_height}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -616,6 +635,12 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{balance_factor}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -885,6 +910,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{right_rotate}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -1140,6 +1171,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{left_rotate}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -1608,6 +1645,12 @@ AVL 树的特点在于“旋转”操作,它能够在不影响二叉树的中
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{rotate}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -1991,6 +2034,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{insert}
[class]{AVLTree}-[func]{insert_helper}
```
=== "Zig"
```zig title="avl_tree.zig"
@ -2576,6 +2627,14 @@ AVL 树的节点插入操作与二叉搜索树在主体上类似。唯一的区
}
```
=== "Ruby"
```ruby title="avl_tree.rb"
[class]{AVLTree}-[func]{remove}
[class]{AVLTree}-[func]{remove_helper}
```
=== "Zig"
```zig title="avl_tree.zig"

View File

@ -310,6 +310,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="binary_search_tree.rb"
[class]{BinarySearchTree}-[func]{search}
```
=== "Zig"
```zig title="binary_search_tree.zig"
@ -756,6 +762,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="binary_search_tree.rb"
[class]{BinarySearchTree}-[func]{insert}
```
=== "Zig"
```zig title="binary_search_tree.zig"
@ -1512,6 +1524,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="binary_search_tree.rb"
[class]{BinarySearchTree}-[func]{remove}
```
=== "Zig"
```zig title="binary_search_tree.zig"

View File

@ -183,6 +183,16 @@ comments: true
=== "Kotlin"
```kotlin title=""
/* 二叉树节点类 */
class TreeNode(val _val: Int) { // 节点值
val left: TreeNode? = null // 左子节点引用
val right: TreeNode? = null // 右子节点引用
}
```
=== "Ruby"
```ruby title=""
```
@ -414,6 +424,22 @@ comments: true
=== "Kotlin"
```kotlin title="binary_tree.kt"
// 初始化节点
val n1 = TreeNode(1)
val n2 = TreeNode(2)
val n3 = TreeNode(3)
val n4 = TreeNode(4)
val n5 = TreeNode(5)
// 构建节点之间的引用(指针)
n1.left = n2
n1.right = n3
n2.left = n4
n2.right = n5
```
=== "Ruby"
```ruby title="binary_tree.rb"
```
@ -568,6 +594,17 @@ comments: true
=== "Kotlin"
```kotlin title="binary_tree.kt"
val P = TreeNode(0)
// 在 n1 -> n2 中间插入节点 P
n1.left = P
P.left = n2
// 删除节点 P
n1.left = n2
```
=== "Ruby"
```ruby title="binary_tree.rb"
```

View File

@ -314,6 +314,12 @@ comments: true
}
```
=== "Ruby"
```ruby title="binary_tree_bfs.rb"
[class]{}-[func]{level_order}
```
=== "Zig"
```zig title="binary_tree_bfs.zig"
@ -781,6 +787,16 @@ comments: true
}
```
=== "Ruby"
```ruby title="binary_tree_dfs.rb"
[class]{}-[func]{pre_order}
[class]{}-[func]{in_order}
[class]{}-[func]{post_order}
```
=== "Zig"
```zig title="binary_tree_dfs.zig"