mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Add build script for Go and update Go codes.
This commit is contained in:
@@ -182,26 +182,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 获取结点高度 */
|
||||
func height(node *TreeNode) int {
|
||||
// 空结点高度为 -1 ,叶结点高度为 0
|
||||
if node != nil {
|
||||
return node.Height
|
||||
}
|
||||
return -1
|
||||
}
|
||||
[class]{aVLTree}-[func]{height}
|
||||
|
||||
/* 更新结点高度 */
|
||||
func updateHeight(node *TreeNode) {
|
||||
lh := height(node.Left)
|
||||
rh := height(node.Right)
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
if lh > rh {
|
||||
node.Height = lh + 1
|
||||
} else {
|
||||
node.Height = rh + 1
|
||||
}
|
||||
}
|
||||
[class]{aVLTree}-[func]{updateHeight}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -273,15 +256,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 获取平衡因子 */
|
||||
func balanceFactor(node *TreeNode) int {
|
||||
// 空结点平衡因子为 0
|
||||
if node == nil {
|
||||
return 0
|
||||
}
|
||||
// 结点平衡因子 = 左子树高度 - 右子树高度
|
||||
return height(node.Left) - height(node.Right)
|
||||
}
|
||||
[class]{aVLTree}-[func]{balanceFactor}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -375,19 +350,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 右旋操作 */
|
||||
func rightRotate(node *TreeNode) *TreeNode {
|
||||
child := node.Left
|
||||
grandChild := child.Right
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.Right = node
|
||||
node.Left = grandChild
|
||||
// 更新结点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
[class]{aVLTree}-[func]{rightRotate}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -459,19 +422,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 左旋操作 */
|
||||
func leftRotate(node *TreeNode) *TreeNode {
|
||||
child := node.Right
|
||||
grandChild := child.Left
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.Left = node
|
||||
node.Right = grandChild
|
||||
// 更新结点高度
|
||||
updateHeight(node)
|
||||
updateHeight(child)
|
||||
// 返回旋转后子树的根结点
|
||||
return child
|
||||
}
|
||||
[class]{aVLTree}-[func]{leftRotate}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -566,36 +517,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
func rotate(node *TreeNode) *TreeNode {
|
||||
// 获取结点 node 的平衡因子
|
||||
// Go 推荐短变量,这里 bf 指代 balanceFactor
|
||||
bf := balanceFactor(node)
|
||||
// 左偏树
|
||||
if bf > 1 {
|
||||
if balanceFactor(node.Left) >= 0 {
|
||||
// 右旋
|
||||
return rightRotate(node)
|
||||
} else {
|
||||
// 先左旋后右旋
|
||||
node.Left = leftRotate(node.Left)
|
||||
return rightRotate(node)
|
||||
}
|
||||
}
|
||||
// 右偏树
|
||||
if bf < -1 {
|
||||
if balanceFactor(node.Right) <= 0 {
|
||||
// 左旋
|
||||
return leftRotate(node)
|
||||
} else {
|
||||
// 先右旋后左旋
|
||||
node.Right = rightRotate(node.Right)
|
||||
return leftRotate(node)
|
||||
}
|
||||
}
|
||||
// 平衡树,无需旋转,直接返回
|
||||
return node
|
||||
}
|
||||
[class]{aVLTree}-[func]{rotate}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -667,32 +589,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 插入结点 */
|
||||
func (t *avlTree) insert(val int) *TreeNode {
|
||||
t.root = insertHelper(t.root, val)
|
||||
return t.root
|
||||
}
|
||||
/* 递归插入结点(辅助函数) */
|
||||
func insertHelper(node *TreeNode, val int) *TreeNode {
|
||||
if node == nil {
|
||||
return NewTreeNode(val)
|
||||
}
|
||||
/* 1. 查找插入位置,并插入结点 */
|
||||
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)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
[class]{aVLTree}-[func]{insert}
|
||||
|
||||
[class]{aVLTree}-[func]{insertHelper}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -778,61 +677,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影
|
||||
=== "Go"
|
||||
|
||||
```go title="avl_tree.go"
|
||||
/* 删除结点 */
|
||||
func (t *avlTree) remove(val int) *TreeNode {
|
||||
root := removeHelper(t.root, val)
|
||||
return root
|
||||
}
|
||||
[class]{aVLTree}-[func]{remove}
|
||||
|
||||
/* 递归删除结点(辅助函数) */
|
||||
func removeHelper(node *TreeNode, val int) *TreeNode {
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
/* 1. 查找结点,并删除之 */
|
||||
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 == nil || node.Right == nil {
|
||||
child := node.Left
|
||||
if node.Right != nil {
|
||||
child = node.Right
|
||||
}
|
||||
// 子结点数量 = 0 ,直接删除 node 并返回
|
||||
if child == nil {
|
||||
return nil
|
||||
} else {
|
||||
// 子结点数量 = 1 ,直接删除 node
|
||||
node = child
|
||||
}
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
temp := getInOrderNext(node.Right)
|
||||
node.Right = removeHelper(node.Right, temp.Val)
|
||||
node.Val = temp.Val
|
||||
}
|
||||
}
|
||||
// 更新结点高度
|
||||
updateHeight(node)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = rotate(node)
|
||||
// 返回子树的根结点
|
||||
return node
|
||||
}
|
||||
[class]{aVLTree}-[func]{removeHelper}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
func getInOrderNext(node *TreeNode) *TreeNode {
|
||||
if node == nil {
|
||||
return node
|
||||
}
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
for node.Left != nil {
|
||||
node = node.Left
|
||||
}
|
||||
return node
|
||||
}
|
||||
[class]{aVLTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
@@ -56,25 +56,7 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
/* 查找结点 */
|
||||
func (bst *binarySearchTree) search(num int) *TreeNode {
|
||||
node := bst.root
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for node != nil {
|
||||
if node.Val < num {
|
||||
// 目标结点在 cur 的右子树中
|
||||
node = node.Right
|
||||
} else if node.Val > num {
|
||||
// 目标结点在 cur 的左子树中
|
||||
node = node.Left
|
||||
} else {
|
||||
// 找到目标结点,跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
// 返回目标结点
|
||||
return node
|
||||
}
|
||||
[class]{binarySearchTree}-[func]{search}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -145,36 +127,7 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
/* 插入结点 */
|
||||
func (bst *binarySearchTree) insert(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 待插入结点之前的结点位置
|
||||
var pre *TreeNode = nil
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for cur != nil {
|
||||
if cur.Val == num {
|
||||
return nil
|
||||
}
|
||||
pre = cur
|
||||
if cur.Val < num {
|
||||
cur = cur.Right
|
||||
} else {
|
||||
cur = cur.Left
|
||||
}
|
||||
}
|
||||
// 插入结点
|
||||
node := NewTreeNode(num)
|
||||
if pre.Val < num {
|
||||
pre.Right = node
|
||||
} else {
|
||||
pre.Left = node
|
||||
}
|
||||
return cur
|
||||
}
|
||||
[class]{binarySearchTree}-[func]{insert}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -276,72 +229,9 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_search_tree.go"
|
||||
/* 删除结点 */
|
||||
func (bst *binarySearchTree) remove(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 待删除结点之前的结点位置
|
||||
var pre *TreeNode = nil
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for cur != nil {
|
||||
if cur.Val == num {
|
||||
break
|
||||
}
|
||||
pre = cur
|
||||
if cur.Val < num {
|
||||
// 待删除结点在右子树中
|
||||
cur = cur.Right
|
||||
} else {
|
||||
// 待删除结点在左子树中
|
||||
cur = cur.Left
|
||||
}
|
||||
}
|
||||
// 若无待删除结点,则直接返回
|
||||
if cur == nil {
|
||||
return nil
|
||||
}
|
||||
// 子结点数为 0 或 1
|
||||
if cur.Left == nil || cur.Right == nil {
|
||||
var child *TreeNode = nil
|
||||
// 取出待删除结点的子结点
|
||||
if cur.Left != nil {
|
||||
child = cur.Left
|
||||
} else {
|
||||
child = cur.Right
|
||||
}
|
||||
// 将子结点替换为待删除结点
|
||||
if pre.Left == cur {
|
||||
pre.Left = child
|
||||
} else {
|
||||
pre.Right = child
|
||||
}
|
||||
// 子结点数为 2
|
||||
} else {
|
||||
// 获取中序遍历中待删除结点 cur 的下一个结点
|
||||
next := bst.getInOrderNext(cur)
|
||||
temp := next.Val
|
||||
// 递归删除结点 next
|
||||
bst.remove(next.Val)
|
||||
// 将 next 的值复制给 cur
|
||||
cur.Val = temp
|
||||
}
|
||||
return cur
|
||||
}
|
||||
[class]{binarySearchTree}-[func]{remove}
|
||||
|
||||
/* 获取中序遍历的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
|
||||
if node == nil {
|
||||
return node
|
||||
}
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
for node.Left != nil {
|
||||
node = node.Left
|
||||
}
|
||||
return node
|
||||
}
|
||||
[class]{binarySearchTree}-[func]{getInOrderNext}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
@@ -39,29 +39,7 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree_bfs.go"
|
||||
/* 层序遍历 */
|
||||
func levelOrder(root *TreeNode) []int {
|
||||
// 初始化队列,加入根结点
|
||||
queue := list.New()
|
||||
queue.PushBack(root)
|
||||
// 初始化一个切片,用于保存遍历序列
|
||||
nums := make([]int, 0)
|
||||
for queue.Len() > 0 {
|
||||
// poll
|
||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||
// 保存结点值
|
||||
nums = append(nums, node.Val)
|
||||
if node.Left != nil {
|
||||
// 左子结点入队
|
||||
queue.PushBack(node.Left)
|
||||
}
|
||||
if node.Right != nil {
|
||||
// 右子结点入队
|
||||
queue.PushBack(node.Right)
|
||||
}
|
||||
}
|
||||
return nums
|
||||
}
|
||||
[class]{}-[func]{hierOrder}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
@@ -153,38 +131,11 @@ comments: true
|
||||
=== "Go"
|
||||
|
||||
```go title="binary_tree_dfs.go"
|
||||
/* 前序遍历 */
|
||||
func preOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:根结点 -> 左子树 -> 右子树
|
||||
nums = append(nums, node.Val)
|
||||
preOrder(node.Left)
|
||||
preOrder(node.Right)
|
||||
}
|
||||
|
||||
/* 中序遍历 */
|
||||
func inOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:左子树 -> 根结点 -> 右子树
|
||||
inOrder(node.Left)
|
||||
nums = append(nums, node.Val)
|
||||
inOrder(node.Right)
|
||||
}
|
||||
|
||||
/* 后序遍历 */
|
||||
func postOrder(node *TreeNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
// 访问优先级:左子树 -> 右子树 -> 根结点
|
||||
postOrder(node.Left)
|
||||
postOrder(node.Right)
|
||||
nums = append(nums, node.Val)
|
||||
}
|
||||
[class]{}-[func]{preOrder}
|
||||
|
||||
[class]{}-[func]{inOrder}
|
||||
|
||||
[class]{}-[func]{postOrder}
|
||||
```
|
||||
|
||||
=== "JavaScript"
|
||||
|
||||
Reference in New Issue
Block a user