2022-11-25 20:24:51 +08:00
|
|
|
// File: binary_search_tree.go
|
2022-11-26 13:25:13 +08:00
|
|
|
// Created Time: 2022-11-26
|
2022-11-25 20:24:51 +08:00
|
|
|
// Author: Reanon (793584285@qq.com)
|
|
|
|
|
|
|
|
package chapter_tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
2022-11-29 00:48:01 +08:00
|
|
|
|
|
|
|
. "github.com/krahets/hello-algo/pkg"
|
2022-11-25 20:24:51 +08:00
|
|
|
)
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
type binarySearchTree struct {
|
2022-11-25 20:24:51 +08:00
|
|
|
root *TreeNode
|
|
|
|
}
|
|
|
|
|
2023-01-03 14:39:31 +08:00
|
|
|
func newBinarySearchTree(nums []int) *binarySearchTree {
|
2023-02-09 04:45:06 +08:00
|
|
|
// 排序数组
|
2022-11-25 20:24:51 +08:00
|
|
|
sort.Ints(nums)
|
2023-02-09 04:45:06 +08:00
|
|
|
// 构建二叉搜索树
|
|
|
|
bst := &binarySearchTree{}
|
|
|
|
bst.root = bst.buildTree(nums, 0, len(nums)-1)
|
|
|
|
return bst
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 构建二叉搜索树 */
|
|
|
|
func (bst *binarySearchTree) buildTree(nums []int, left, right int) *TreeNode {
|
|
|
|
if left > right {
|
|
|
|
return nil
|
2022-11-26 13:25:13 +08:00
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 将数组中间节点作为根节点
|
2023-02-09 04:45:06 +08:00
|
|
|
middle := left + (right-left)>>1
|
|
|
|
root := NewTreeNode(nums[middle])
|
|
|
|
// 递归构建左子树和右子树
|
|
|
|
root.Left = bst.buildTree(nums, left, middle-1)
|
|
|
|
root.Right = bst.buildTree(nums, middle+1, right)
|
|
|
|
return root
|
2022-11-26 13:25:13 +08:00
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取根节点 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) getRoot() *TreeNode {
|
2022-11-26 13:25:13 +08:00
|
|
|
return bst.root
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 获取中序遍历的下一个节点(仅适用于 root 有左子节点的情况) */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
|
2022-11-26 13:25:13 +08:00
|
|
|
if node == nil {
|
|
|
|
return node
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 循环访问左子节点,直到叶节点时为最小节点,跳出
|
2022-11-26 13:25:13 +08:00
|
|
|
for node.Left != nil {
|
|
|
|
node = node.Left
|
|
|
|
}
|
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 查找节点 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) search(num int) *TreeNode {
|
2022-11-26 13:25:13 +08:00
|
|
|
node := bst.root
|
2023-04-09 04:32:17 +08:00
|
|
|
// 循环查找,越过叶节点后跳出
|
2022-11-26 13:25:13 +08:00
|
|
|
for node != nil {
|
|
|
|
if node.Val < num {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 目标节点在 cur 的右子树中
|
2022-11-26 13:25:13 +08:00
|
|
|
node = node.Right
|
|
|
|
} else if node.Val > num {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 目标节点在 cur 的左子树中
|
2022-11-26 13:25:13 +08:00
|
|
|
node = node.Left
|
|
|
|
} else {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 找到目标节点,跳出循环
|
2022-11-26 13:25:13 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 返回目标节点
|
2022-11-26 13:25:13 +08:00
|
|
|
return node
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 插入节点 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) insert(num int) *TreeNode {
|
2022-11-26 13:25:13 +08:00
|
|
|
cur := bst.root
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 待插入节点之前的节点位置
|
2023-02-04 15:03:51 +08:00
|
|
|
var pre *TreeNode = nil
|
2023-04-09 04:32:17 +08:00
|
|
|
// 循环查找,越过叶节点后跳出
|
2022-11-26 13:25:13 +08:00
|
|
|
for cur != nil {
|
|
|
|
if cur.Val == num {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-04 15:03:51 +08:00
|
|
|
pre = cur
|
2022-11-26 13:25:13 +08:00
|
|
|
if cur.Val < num {
|
|
|
|
cur = cur.Right
|
|
|
|
} else {
|
|
|
|
cur = cur.Left
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 插入节点
|
2022-11-26 13:25:13 +08:00
|
|
|
node := NewTreeNode(num)
|
2023-02-04 15:03:51 +08:00
|
|
|
if pre.Val < num {
|
|
|
|
pre.Right = node
|
2022-11-26 13:25:13 +08:00
|
|
|
} else {
|
2023-02-04 15:03:51 +08:00
|
|
|
pre.Left = node
|
2022-11-26 13:25:13 +08:00
|
|
|
}
|
|
|
|
return cur
|
|
|
|
}
|
|
|
|
|
2023-04-09 04:32:17 +08:00
|
|
|
/* 删除节点 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) remove(num int) *TreeNode {
|
2022-11-26 13:25:13 +08:00
|
|
|
cur := bst.root
|
|
|
|
// 若树为空,直接提前返回
|
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 待删除节点之前的节点位置
|
2023-02-04 15:03:51 +08:00
|
|
|
var pre *TreeNode = nil
|
2023-04-09 04:32:17 +08:00
|
|
|
// 循环查找,越过叶节点后跳出
|
2022-11-26 13:25:13 +08:00
|
|
|
for cur != nil {
|
|
|
|
if cur.Val == num {
|
|
|
|
break
|
|
|
|
}
|
2023-02-04 15:03:51 +08:00
|
|
|
pre = cur
|
2022-11-26 13:25:13 +08:00
|
|
|
if cur.Val < num {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 待删除节点在右子树中
|
2022-11-26 13:25:13 +08:00
|
|
|
cur = cur.Right
|
|
|
|
} else {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 待删除节点在左子树中
|
2022-11-26 13:25:13 +08:00
|
|
|
cur = cur.Left
|
|
|
|
}
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 若无待删除节点,则直接返回
|
2022-11-26 13:25:13 +08:00
|
|
|
if cur == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 子节点数为 0 或 1
|
2022-11-26 13:25:13 +08:00
|
|
|
if cur.Left == nil || cur.Right == nil {
|
|
|
|
var child *TreeNode = nil
|
2023-04-09 04:32:17 +08:00
|
|
|
// 取出待删除节点的子节点
|
2022-11-26 13:25:13 +08:00
|
|
|
if cur.Left != nil {
|
|
|
|
child = cur.Left
|
|
|
|
} else {
|
|
|
|
child = cur.Right
|
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 将子节点替换为待删除节点
|
2023-02-04 15:03:51 +08:00
|
|
|
if pre.Left == cur {
|
|
|
|
pre.Left = child
|
2022-11-26 13:25:13 +08:00
|
|
|
} else {
|
2023-02-04 15:03:51 +08:00
|
|
|
pre.Right = child
|
2022-11-26 13:25:13 +08:00
|
|
|
}
|
2023-04-09 04:32:17 +08:00
|
|
|
// 子节点数为 2
|
2022-12-03 20:25:24 +08:00
|
|
|
} else {
|
2023-04-09 04:32:17 +08:00
|
|
|
// 获取中序遍历中待删除节点 cur 的下一个节点
|
2023-01-03 14:39:31 +08:00
|
|
|
next := bst.getInOrderNext(cur)
|
2022-11-26 13:25:13 +08:00
|
|
|
temp := next.Val
|
2023-04-09 04:32:17 +08:00
|
|
|
// 递归删除节点 next
|
2023-01-03 14:39:31 +08:00
|
|
|
bst.remove(next.Val)
|
2022-11-26 13:25:13 +08:00
|
|
|
// 将 next 的值复制给 cur
|
|
|
|
cur.Val = temp
|
|
|
|
}
|
|
|
|
return cur
|
|
|
|
}
|
|
|
|
|
2023-02-09 04:45:06 +08:00
|
|
|
/* 打印二叉搜索树 */
|
2023-01-03 14:39:31 +08:00
|
|
|
func (bst *binarySearchTree) print() {
|
2022-11-26 13:25:13 +08:00
|
|
|
PrintTree(bst.root)
|
2022-11-25 20:24:51 +08:00
|
|
|
}
|