mirror of
https://github.com/krahets/hello-algo.git
synced 2026-03-30 08:41:47 +08:00
refactor: Replace 结点 with 节点 (#452)
* Replace 结点 with 节点 Update the footnotes in the figures * Update mindmap * Reduce the size of the mindmap.png
This commit is contained in:
@@ -8,7 +8,7 @@ import . "github.com/krahets/hello-algo/pkg"
|
||||
|
||||
/* AVL 树 */
|
||||
type aVLTree struct {
|
||||
// 根结点
|
||||
// 根节点
|
||||
root *TreeNode
|
||||
}
|
||||
|
||||
@@ -16,20 +16,20 @@ func newAVLTree() *aVLTree {
|
||||
return &aVLTree{root: nil}
|
||||
}
|
||||
|
||||
/* 获取结点高度 */
|
||||
/* 获取节点高度 */
|
||||
func (t *aVLTree) height(node *TreeNode) int {
|
||||
// 空结点高度为 -1 ,叶结点高度为 0
|
||||
// 空节点高度为 -1 ,叶节点高度为 0
|
||||
if node != nil {
|
||||
return node.Height
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/* 更新结点高度 */
|
||||
/* 更新节点高度 */
|
||||
func (t *aVLTree) updateHeight(node *TreeNode) {
|
||||
lh := t.height(node.Left)
|
||||
rh := t.height(node.Right)
|
||||
// 结点高度等于最高子树高度 + 1
|
||||
// 节点高度等于最高子树高度 + 1
|
||||
if lh > rh {
|
||||
node.Height = lh + 1
|
||||
} else {
|
||||
@@ -39,11 +39,11 @@ func (t *aVLTree) updateHeight(node *TreeNode) {
|
||||
|
||||
/* 获取平衡因子 */
|
||||
func (t *aVLTree) balanceFactor(node *TreeNode) int {
|
||||
// 空结点平衡因子为 0
|
||||
// 空节点平衡因子为 0
|
||||
if node == nil {
|
||||
return 0
|
||||
}
|
||||
// 结点平衡因子 = 左子树高度 - 右子树高度
|
||||
// 节点平衡因子 = 左子树高度 - 右子树高度
|
||||
return t.height(node.Left) - t.height(node.Right)
|
||||
}
|
||||
|
||||
@@ -54,10 +54,10 @@ func (t *aVLTree) rightRotate(node *TreeNode) *TreeNode {
|
||||
// 以 child 为原点,将 node 向右旋转
|
||||
child.Right = node
|
||||
node.Left = grandChild
|
||||
// 更新结点高度
|
||||
// 更新节点高度
|
||||
t.updateHeight(node)
|
||||
t.updateHeight(child)
|
||||
// 返回旋转后子树的根结点
|
||||
// 返回旋转后子树的根节点
|
||||
return child
|
||||
}
|
||||
|
||||
@@ -68,16 +68,16 @@ func (t *aVLTree) leftRotate(node *TreeNode) *TreeNode {
|
||||
// 以 child 为原点,将 node 向左旋转
|
||||
child.Left = node
|
||||
node.Right = grandChild
|
||||
// 更新结点高度
|
||||
// 更新节点高度
|
||||
t.updateHeight(node)
|
||||
t.updateHeight(child)
|
||||
// 返回旋转后子树的根结点
|
||||
// 返回旋转后子树的根节点
|
||||
return child
|
||||
}
|
||||
|
||||
/* 执行旋转操作,使该子树重新恢复平衡 */
|
||||
func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
|
||||
// 获取结点 node 的平衡因子
|
||||
// 获取节点 node 的平衡因子
|
||||
// Go 推荐短变量,这里 bf 指代 t.balanceFactor
|
||||
bf := t.balanceFactor(node)
|
||||
// 左偏树
|
||||
@@ -106,46 +106,46 @@ func (t *aVLTree) rotate(node *TreeNode) *TreeNode {
|
||||
return node
|
||||
}
|
||||
|
||||
/* 插入结点 */
|
||||
/* 插入节点 */
|
||||
func (t *aVLTree) insert(val int) *TreeNode {
|
||||
t.root = t.insertHelper(t.root, val)
|
||||
return t.root
|
||||
}
|
||||
|
||||
/* 递归插入结点(辅助方法) */
|
||||
/* 递归插入节点(辅助方法) */
|
||||
func (t *aVLTree) insertHelper(node *TreeNode, val int) *TreeNode {
|
||||
if node == nil {
|
||||
return NewTreeNode(val)
|
||||
}
|
||||
/* 1. 查找插入位置,并插入结点 */
|
||||
/* 1. 查找插入位置,并插入节点 */
|
||||
if val < node.Val {
|
||||
node.Left = t.insertHelper(node.Left, val)
|
||||
} else if val > node.Val {
|
||||
node.Right = t.insertHelper(node.Right, val)
|
||||
} else {
|
||||
// 重复结点不插入,直接返回
|
||||
// 重复节点不插入,直接返回
|
||||
return node
|
||||
}
|
||||
// 更新结点高度
|
||||
// 更新节点高度
|
||||
t.updateHeight(node)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = t.rotate(node)
|
||||
// 返回子树的根结点
|
||||
// 返回子树的根节点
|
||||
return node
|
||||
}
|
||||
|
||||
/* 删除结点 */
|
||||
/* 删除节点 */
|
||||
func (t *aVLTree) remove(val int) *TreeNode {
|
||||
root := t.removeHelper(t.root, val)
|
||||
return root
|
||||
}
|
||||
|
||||
/* 递归删除结点(辅助方法) */
|
||||
/* 递归删除节点(辅助方法) */
|
||||
func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
||||
if node == nil {
|
||||
return nil
|
||||
}
|
||||
/* 1. 查找结点,并删除之 */
|
||||
/* 1. 查找节点,并删除之 */
|
||||
if val < node.Val {
|
||||
node.Left = t.removeHelper(node.Left, val)
|
||||
} else if val > node.Val {
|
||||
@@ -156,56 +156,56 @@ func (t *aVLTree) removeHelper(node *TreeNode, val int) *TreeNode {
|
||||
if node.Right != nil {
|
||||
child = node.Right
|
||||
}
|
||||
// 子结点数量 = 0 ,直接删除 node 并返回
|
||||
// 子节点数量 = 0 ,直接删除 node 并返回
|
||||
if child == nil {
|
||||
return nil
|
||||
} else {
|
||||
// 子结点数量 = 1 ,直接删除 node
|
||||
// 子节点数量 = 1 ,直接删除 node
|
||||
node = child
|
||||
}
|
||||
} else {
|
||||
// 子结点数量 = 2 ,则将中序遍历的下个结点删除,并用该结点替换当前结点
|
||||
// 子节点数量 = 2 ,则将中序遍历的下个节点删除,并用该节点替换当前节点
|
||||
temp := t.getInOrderNext(node.Right)
|
||||
node.Right = t.removeHelper(node.Right, temp.Val)
|
||||
node.Val = temp.Val
|
||||
}
|
||||
}
|
||||
// 更新结点高度
|
||||
// 更新节点高度
|
||||
t.updateHeight(node)
|
||||
/* 2. 执行旋转操作,使该子树重新恢复平衡 */
|
||||
node = t.rotate(node)
|
||||
// 返回子树的根结点
|
||||
// 返回子树的根节点
|
||||
return node
|
||||
}
|
||||
|
||||
/* 获取中序遍历中的下一个结点(仅适用于 root 有左子结点的情况) */
|
||||
/* 获取中序遍历中的下一个节点(仅适用于 root 有左子节点的情况) */
|
||||
func (t *aVLTree) getInOrderNext(node *TreeNode) *TreeNode {
|
||||
if node == nil {
|
||||
return node
|
||||
}
|
||||
// 循环访问左子结点,直到叶结点时为最小结点,跳出
|
||||
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
||||
for node.Left != nil {
|
||||
node = node.Left
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
/* 查找结点 */
|
||||
/* 查找节点 */
|
||||
func (t *aVLTree) search(val int) *TreeNode {
|
||||
cur := t.root
|
||||
// 循环查找,越过叶结点后跳出
|
||||
// 循环查找,越过叶节点后跳出
|
||||
for cur != nil {
|
||||
if cur.Val < val {
|
||||
// 目标结点在 cur 的右子树中
|
||||
// 目标节点在 cur 的右子树中
|
||||
cur = cur.Right
|
||||
} else if cur.Val > val {
|
||||
// 目标结点在 cur 的左子树中
|
||||
// 目标节点在 cur 的左子树中
|
||||
cur = cur.Left
|
||||
} else {
|
||||
// 找到目标结点,跳出循环
|
||||
// 找到目标节点,跳出循环
|
||||
break
|
||||
}
|
||||
}
|
||||
// 返回目标结点
|
||||
// 返回目标节点
|
||||
return cur
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user