mirror of
https://github.com/krahets/hello-algo.git
synced 2026-05-12 11:37:08 +08:00
Merge pull request #220 from Reanon/bugfix/fix-go-code-style
style(go): fix go code style
This commit is contained in:
@@ -10,26 +10,26 @@ import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
type BinarySearchTree struct {
|
||||
type binarySearchTree struct {
|
||||
root *TreeNode
|
||||
}
|
||||
|
||||
func NewBinarySearchTree(nums []int) *BinarySearchTree {
|
||||
func newBinarySearchTree(nums []int) *binarySearchTree {
|
||||
// sorting array
|
||||
sort.Ints(nums)
|
||||
root := buildBinarySearchTree(nums, 0, len(nums)-1)
|
||||
return &BinarySearchTree{
|
||||
return &binarySearchTree{
|
||||
root: root,
|
||||
}
|
||||
}
|
||||
|
||||
/* 获取根结点 */
|
||||
func (bst *BinarySearchTree) GetRoot() *TreeNode {
|
||||
func (bst *binarySearchTree) getRoot() *TreeNode {
|
||||
return bst.root
|
||||
}
|
||||
|
||||
/* 获取中序遍历的下一个结点 */
|
||||
func (bst *BinarySearchTree) GetInOrderNext(node *TreeNode) *TreeNode {
|
||||
func (bst *binarySearchTree) getInOrderNext(node *TreeNode) *TreeNode {
|
||||
if node == nil {
|
||||
return node
|
||||
}
|
||||
@@ -41,7 +41,7 @@ func (bst *BinarySearchTree) GetInOrderNext(node *TreeNode) *TreeNode {
|
||||
}
|
||||
|
||||
/* 查找结点 */
|
||||
func (bst *BinarySearchTree) Search(num int) *TreeNode {
|
||||
func (bst *binarySearchTree) search(num int) *TreeNode {
|
||||
node := bst.root
|
||||
// 循环查找,越过叶结点后跳出
|
||||
for node != nil {
|
||||
@@ -61,7 +61,7 @@ func (bst *BinarySearchTree) Search(num int) *TreeNode {
|
||||
}
|
||||
|
||||
/* 插入结点 */
|
||||
func (bst *BinarySearchTree) Insert(num int) *TreeNode {
|
||||
func (bst *binarySearchTree) insert(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
@@ -92,7 +92,7 @@ func (bst *BinarySearchTree) Insert(num int) *TreeNode {
|
||||
}
|
||||
|
||||
/* 删除结点 */
|
||||
func (bst *BinarySearchTree) Remove(num int) *TreeNode {
|
||||
func (bst *binarySearchTree) remove(num int) *TreeNode {
|
||||
cur := bst.root
|
||||
// 若树为空,直接提前返回
|
||||
if cur == nil {
|
||||
@@ -136,10 +136,10 @@ func (bst *BinarySearchTree) Remove(num int) *TreeNode {
|
||||
// 子结点数为 2
|
||||
} else {
|
||||
// 获取中序遍历中待删除结点 cur 的下一个结点
|
||||
next := bst.GetInOrderNext(cur)
|
||||
next := bst.getInOrderNext(cur)
|
||||
temp := next.Val
|
||||
// 递归删除结点 next
|
||||
bst.Remove(next.Val)
|
||||
bst.remove(next.Val)
|
||||
// 将 next 的值复制给 cur
|
||||
cur.Val = temp
|
||||
}
|
||||
@@ -160,7 +160,7 @@ func buildBinarySearchTree(nums []int, left, right int) *TreeNode {
|
||||
return root
|
||||
}
|
||||
|
||||
// Print binary search tree
|
||||
func (bst *BinarySearchTree) Print() {
|
||||
// print binary search tree
|
||||
func (bst *binarySearchTree) print() {
|
||||
PrintTree(bst.root)
|
||||
}
|
||||
|
||||
@@ -11,31 +11,31 @@ import (
|
||||
|
||||
func TestBinarySearchTree(t *testing.T) {
|
||||
nums := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}
|
||||
bst := NewBinarySearchTree(nums)
|
||||
bst := newBinarySearchTree(nums)
|
||||
fmt.Println("\n初始化的二叉树为:")
|
||||
bst.Print()
|
||||
bst.print()
|
||||
|
||||
// 获取根结点
|
||||
node := bst.GetRoot()
|
||||
node := bst.getRoot()
|
||||
fmt.Println("\n二叉树的根结点为:", node.Val)
|
||||
|
||||
// 查找结点
|
||||
node = bst.Search(5)
|
||||
node = bst.search(5)
|
||||
fmt.Println("\n查找到的结点对象为", node, ",结点值 =", node.Val)
|
||||
|
||||
// 插入结点
|
||||
node = bst.Insert(16)
|
||||
node = bst.insert(16)
|
||||
fmt.Println("\n插入结点后 16 的二叉树为:")
|
||||
bst.Print()
|
||||
bst.print()
|
||||
|
||||
// 删除结点
|
||||
bst.Remove(1)
|
||||
bst.remove(1)
|
||||
fmt.Println("\n删除结点 1 后的二叉树为:")
|
||||
bst.Print()
|
||||
bst.Remove(2)
|
||||
bst.print()
|
||||
bst.remove(2)
|
||||
fmt.Println("\n删除结点 2 后的二叉树为:")
|
||||
bst.Print()
|
||||
bst.Remove(4)
|
||||
bst.print()
|
||||
bst.remove(4)
|
||||
fmt.Println("\n删除结点 4 后的二叉树为:")
|
||||
bst.Print()
|
||||
bst.print()
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
func TestLevelOrder(t *testing.T) {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
root := ArrToTree([]int{1, 2, 3, 4, 5, 6, 7})
|
||||
root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7})
|
||||
fmt.Println("\n初始化二叉树: ")
|
||||
PrintTree(root)
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
func TestPreInPostOrderTraversal(t *testing.T) {
|
||||
/* 初始化二叉树 */
|
||||
// 这里借助了一个从数组直接生成二叉树的函数
|
||||
root := ArrToTree([]int{1, 2, 3, 4, 5, 6, 7})
|
||||
root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7})
|
||||
fmt.Println("\n初始化二叉树: ")
|
||||
PrintTree(root)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user