This commit is contained in:
krahets
2023-07-26 15:34:46 +08:00
parent f8f7086196
commit 7351ec70a6
53 changed files with 439 additions and 329 deletions
+99 -5
View File
@@ -62,7 +62,7 @@ comments: true
tree := []any{1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15}
```
=== "JavaScript"
=== "JS"
```javascript title=""
/* 二叉树的数组表示 */
@@ -70,7 +70,7 @@ comments: true
let tree = [1, 2, 3, 4, null, 6, 7, 8, 9, null, null, 12, null, null, 15];
```
=== "TypeScript"
=== "TS"
```typescript title=""
/* 二叉树的数组表示 */
@@ -403,16 +403,110 @@ comments: true
=== "Go"
```go title="array_binary_tree.go"
[class]{arrayBinaryTree}-[func]{}
/* 数组表示下的二叉树类 */
type arrayBinaryTree struct {
tree []any
}
/* 构造方法 */
func newArrayBinaryTree(arr []any) *arrayBinaryTree {
return &arrayBinaryTree{
tree: arr,
}
}
/* 节点数量 */
func (abt *arrayBinaryTree) size() int {
return len(abt.tree)
}
/* 获取索引为 i 节点的值 */
func (abt *arrayBinaryTree) val(i int) any {
// 若索引越界,则返回 null ,代表空位
if i < 0 || i >= abt.size() {
return nil
}
return abt.tree[i]
}
/* 获取索引为 i 节点的左子节点的索引 */
func (abt *arrayBinaryTree) left(i int) int {
return 2*i + 1
}
/* 获取索引为 i 节点的右子节点的索引 */
func (abt *arrayBinaryTree) right(i int) int {
return 2*i + 2
}
/* 获取索引为 i 节点的父节点的索引 */
func (abt *arrayBinaryTree) parent(i int) int {
return (i - 1) / 2
}
/* 层序遍历 */
func (abt *arrayBinaryTree) levelOrder() []any {
var res []any
// 直接遍历数组
for i := 0; i < abt.size(); i++ {
if abt.val(i) != nil {
res = append(res, abt.val(i))
}
}
return res
}
/* 深度优先遍历 */
func (abt *arrayBinaryTree) dfs(i int, order string, res *[]any) {
// 若为空位,则返回
if abt.val(i) == nil {
return
}
// 前序遍历
if order == "pre" {
*res = append(*res, abt.val(i))
}
abt.dfs(abt.left(i), order, res)
// 中序遍历
if order == "in" {
*res = append(*res, abt.val(i))
}
abt.dfs(abt.right(i), order, res)
// 后序遍历
if order == "post" {
*res = append(*res, abt.val(i))
}
}
/* 前序遍历 */
func (abt *arrayBinaryTree) preOrder() []any {
var res []any
abt.dfs(0, "pre", &res)
return res
}
/* 中序遍历 */
func (abt *arrayBinaryTree) inOrder() []any {
var res []any
abt.dfs(0, "in", &res)
return res
}
/* 后序遍历 */
func (abt *arrayBinaryTree) postOrder() []any {
var res []any
abt.dfs(0, "post", &res)
return res
}
```
=== "JavaScript"
=== "JS"
```javascript title="array_binary_tree.js"
[class]{ArrayBinaryTree}-[func]{}
```
=== "TypeScript"
=== "TS"
```typescript title="array_binary_tree.ts"
[class]{ArrayBinaryTree}-[func]{}
+21 -21
View File
@@ -79,7 +79,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "JavaScript"
=== "JS"
```javascript title=""
/* AVL 树节点类 */
@@ -97,7 +97,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "TypeScript"
=== "TS"
```typescript title=""
/* AVL 树节点类 */
@@ -271,7 +271,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 获取节点高度 */
@@ -288,7 +288,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 获取节点高度 */
@@ -473,7 +473,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 获取平衡因子 */
@@ -485,7 +485,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 获取平衡因子 */
@@ -680,7 +680,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 右旋操作 */
@@ -698,7 +698,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 右旋操作 */
@@ -917,7 +917,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 左旋操作 */
@@ -935,7 +935,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 左旋操作 */
@@ -1238,7 +1238,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 执行旋转操作,使该子树重新恢复平衡 */
@@ -1272,7 +1272,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 执行旋转操作,使该子树重新恢复平衡 */
@@ -1614,9 +1614,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
return NewTreeNode(val)
}
/* 1. 查找插入位置,并插入节点 */
if val < node.Val {
if val < node.Val.(int) {
node.Left = t.insertHelper(node.Left, val)
} else if val > node.Val {
} else if val > node.Val.(int) {
node.Right = t.insertHelper(node.Right, val)
} else {
// 重复节点不插入,直接返回
@@ -1631,7 +1631,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 插入节点 */
@@ -1655,7 +1655,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 插入节点 */
@@ -2018,9 +2018,9 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
return nil
}
/* 1. 查找节点,并删除之 */
if val < node.Val {
if val < node.Val.(int) {
node.Left = t.removeHelper(node.Left, val)
} else if val > node.Val {
} else if val > node.Val.(int) {
node.Right = t.removeHelper(node.Right, val)
} else {
if node.Left == nil || node.Right == nil {
@@ -2041,7 +2041,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
for temp.Left != nil {
temp = temp.Left
}
node.Right = t.removeHelper(node.Right, temp.Val)
node.Right = t.removeHelper(node.Right, temp.Val.(int))
node.Val = temp.Val
}
}
@@ -2054,7 +2054,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "JavaScript"
=== "JS"
```javascript title="avl_tree.js"
/* 删除节点 */
@@ -2094,7 +2094,7 @@ AVL 树的特点在于「旋转 Rotation」操作,它能够在不影响二叉
}
```
=== "TypeScript"
=== "TS"
```typescript title="avl_tree.ts"
/* 删除节点 */
+12 -12
View File
@@ -111,10 +111,10 @@ comments: true
node := bst.root
// 循环查找,越过叶节点后跳出
for node != nil {
if node.Val < num {
if node.Val.(int) < num {
// 目标节点在 cur 的右子树中
node = node.Right
} else if node.Val > num {
} else if node.Val.(int) > num {
// 目标节点在 cur 的左子树中
node = node.Left
} else {
@@ -127,7 +127,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 查找节点 */
@@ -147,7 +147,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 查找节点 */
@@ -422,7 +422,7 @@ comments: true
return
}
pre = cur
if cur.Val < num {
if cur.Val.(int) < num {
cur = cur.Right
} else {
cur = cur.Left
@@ -430,7 +430,7 @@ comments: true
}
// 插入节点
node := NewTreeNode(num)
if pre.Val < num {
if pre.Val.(int) < num {
pre.Right = node
} else {
pre.Left = node
@@ -438,7 +438,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 插入节点 */
@@ -464,7 +464,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 插入节点 */
@@ -900,7 +900,7 @@ comments: true
break
}
pre = cur
if cur.Val < num {
if cur.Val.(int) < num {
// 待删除节点在右子树中
cur = cur.Right
} else {
@@ -940,14 +940,14 @@ comments: true
tmp = tmp.Left
}
// 递归删除节点 tmp
bst.remove(tmp.Val)
bst.remove(tmp.Val.(int))
// 用 tmp 覆盖 cur
cur.Val = tmp.Val
}
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_search_tree.js"
/* 删除节点 */
@@ -996,7 +996,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_search_tree.ts"
/* 删除节点 */
+6 -6
View File
@@ -60,7 +60,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title=""
/* 二叉树节点类 */
@@ -71,7 +71,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title=""
/* 二叉树节点类 */
@@ -265,7 +265,7 @@ comments: true
n2.Right = n5
```
=== "JavaScript"
=== "JS"
```javascript title="binary_tree.js"
/* 初始化二叉树 */
@@ -282,7 +282,7 @@ comments: true
n2.right = n5;
```
=== "TypeScript"
=== "TS"
```typescript title="binary_tree.ts"
/* 初始化二叉树 */
@@ -431,7 +431,7 @@ comments: true
n1.Left = n2
```
=== "JavaScript"
=== "JS"
```javascript title="binary_tree.js"
/* 插入与删除节点 */
@@ -443,7 +443,7 @@ comments: true
n1.left = n2;
```
=== "TypeScript"
=== "TS"
```typescript title="binary_tree.ts"
/* 插入与删除节点 */
+6 -6
View File
@@ -89,12 +89,12 @@ comments: true
```go title="binary_tree_bfs.go"
/* 层序遍历 */
func levelOrder(root *TreeNode) []int {
func levelOrder(root *TreeNode) []any {
// 初始化队列,加入根节点
queue := list.New()
queue.PushBack(root)
// 初始化一个切片,用于保存遍历序列
nums := make([]int, 0)
nums := make([]any, 0)
for queue.Len() > 0 {
// 队列出队
node := queue.Remove(queue.Front()).(*TreeNode)
@@ -113,7 +113,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_tree_bfs.js"
/* 层序遍历 */
@@ -132,7 +132,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_tree_bfs.ts"
/* 层序遍历 */
@@ -474,7 +474,7 @@ comments: true
}
```
=== "JavaScript"
=== "JS"
```javascript title="binary_tree_dfs.js"
/* 前序遍历 */
@@ -505,7 +505,7 @@ comments: true
}
```
=== "TypeScript"
=== "TS"
```typescript title="binary_tree_dfs.ts"
/* 前序遍历 */