mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:16:31 +08:00
Merge pull request #20 from Reanon/feature/suppor-go-include
feat(go): support basic pkg
This commit is contained in:
commit
89df49f637
9 changed files with 359 additions and 0 deletions
35
codes/go/chapter_searching/linear_search.go
Normal file
35
codes/go/chapter_searching/linear_search.go
Normal file
|
@ -0,0 +1,35 @@
|
|||
// File: linear_search.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
// linerSearchArray 线性查找(数组)
|
||||
func linerSearchArray(nums []int, target int) int {
|
||||
// 遍历数组
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if nums[i] == target {
|
||||
// 找到目标元素,返回其索引
|
||||
return i
|
||||
}
|
||||
}
|
||||
// 未找到目标元素,返回 -1
|
||||
return -1
|
||||
}
|
||||
|
||||
// linerSearchLinkedList 线性查找(链表)
|
||||
func linerSearchLinkedList(node *ListNode, target int) *ListNode {
|
||||
// 遍历链表
|
||||
for node != nil {
|
||||
if node.Val == target {
|
||||
return node
|
||||
}
|
||||
node = node.Next
|
||||
}
|
||||
// 未找到目标元素,返回 nil
|
||||
return nil
|
||||
}
|
24
codes/go/chapter_searching/linear_search_test.go
Normal file
24
codes/go/chapter_searching/linear_search_test.go
Normal file
|
@ -0,0 +1,24 @@
|
|||
// File: linear_search_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_searching
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLinearSearch(t *testing.T) {
|
||||
target := 3
|
||||
nums := []int{1, 5, 3, 2, 4, 7, 5, 9, 10, 8}
|
||||
|
||||
// 在数组中执行线性查找
|
||||
index := linerSearchArray(nums, target)
|
||||
t.Log("目标元素 3 的索引 = ", index)
|
||||
|
||||
// 在链表中执行线性查找
|
||||
head := ArrayToLinkedList(nums)
|
||||
node := linerSearchLinkedList(head, target)
|
||||
t.Log("目标结点值 3 的对应结点对象为 ", node)
|
||||
}
|
20
codes/go/chapter_tree/binary_search_tree.go
Normal file
20
codes/go/chapter_tree/binary_search_tree.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
// File: binary_search_tree.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_tree
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
"sort"
|
||||
)
|
||||
|
||||
type BinarySearchTree struct {
|
||||
root *TreeNode
|
||||
}
|
||||
|
||||
func NewBinarySearchTree(nums []int) *BinarySearchTree {
|
||||
// 排序数组
|
||||
sort.Ints(nums)
|
||||
return nil
|
||||
}
|
23
codes/go/chapter_tree/binary_tree.go
Normal file
23
codes/go/chapter_tree/binary_tree.go
Normal file
|
@ -0,0 +1,23 @@
|
|||
// File: binary_tree.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_tree
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
)
|
||||
|
||||
type BinaryTree struct {
|
||||
root *TreeNode
|
||||
}
|
||||
|
||||
func NewBinaryTree(node *TreeNode) *BinaryTree {
|
||||
return &BinaryTree{
|
||||
root: node,
|
||||
}
|
||||
}
|
||||
|
||||
func (tree *BinaryTree) Print() {
|
||||
PrintTree(tree.root)
|
||||
}
|
40
codes/go/chapter_tree/binary_tree_test.go
Normal file
40
codes/go/chapter_tree/binary_tree_test.go
Normal file
|
@ -0,0 +1,40 @@
|
|||
// File: binary_tree_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package chapter_tree
|
||||
|
||||
import (
|
||||
. "github.com/krahets/hello-algo/pkg"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBinaryTree(t *testing.T) {
|
||||
/* 初始化二叉树 */
|
||||
// 初始化节点
|
||||
n1 := NewTreeNode(1)
|
||||
n2 := NewTreeNode(2)
|
||||
n3 := NewTreeNode(3)
|
||||
n4 := NewTreeNode(4)
|
||||
n5 := NewTreeNode(5)
|
||||
|
||||
tree := NewBinaryTree(n1)
|
||||
n1.Left = n2
|
||||
n1.Right = n3
|
||||
n2.Left = n4
|
||||
n2.Right = n5
|
||||
t.Log("初始化二叉树")
|
||||
tree.Print()
|
||||
|
||||
/* 插入与删除结点 */
|
||||
p := NewTreeNode(0)
|
||||
n1.Left = p
|
||||
p.Left = n2
|
||||
t.Log("插入结点 P 后")
|
||||
tree.Print()
|
||||
|
||||
// 删除结点
|
||||
n1.Left = n2
|
||||
t.Log("删除结点 P 后")
|
||||
tree.Print()
|
||||
}
|
59
codes/go/pkg/list_node.go
Normal file
59
codes/go/pkg/list_node.go
Normal file
|
@ -0,0 +1,59 @@
|
|||
// File: list_node.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ListNode Definition for a singly-linked list node
|
||||
type ListNode struct {
|
||||
Next *ListNode
|
||||
Val int
|
||||
}
|
||||
|
||||
// NewListNode Generate a list node with an val
|
||||
func NewListNode(v int) *ListNode {
|
||||
return &ListNode{
|
||||
Next: nil,
|
||||
Val: v,
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayToLinkedList Generate a linked list with an array
|
||||
func ArrayToLinkedList(arr []int) *ListNode {
|
||||
// dummy header of linked list
|
||||
dummy := NewListNode(0)
|
||||
node := dummy
|
||||
for _, val := range arr {
|
||||
node.Next = NewListNode(val)
|
||||
node = node.Next
|
||||
}
|
||||
return dummy.Next
|
||||
}
|
||||
|
||||
// GetListNode Get a list node with specific value from a linked list
|
||||
func GetListNode(node *ListNode, val int) *ListNode {
|
||||
for node != nil && node.Val != val {
|
||||
node = node.Next
|
||||
}
|
||||
return node
|
||||
}
|
||||
|
||||
// PrintLinkedList Print a linked list
|
||||
func PrintLinkedList(node *ListNode) {
|
||||
if node == nil {
|
||||
return
|
||||
}
|
||||
var builder strings.Builder
|
||||
for node.Next != nil {
|
||||
builder.WriteString(strconv.Itoa(node.Val) + " -> ")
|
||||
node = node.Next
|
||||
}
|
||||
builder.WriteString(strconv.Itoa(node.Val))
|
||||
fmt.Println(builder.String())
|
||||
}
|
16
codes/go/pkg/list_node_test.go
Normal file
16
codes/go/pkg/list_node_test.go
Normal file
|
@ -0,0 +1,16 @@
|
|||
// File: list_node_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestListNode(t *testing.T) {
|
||||
arr := []int{2, 3, 5, 6, 7}
|
||||
head := ArrayToLinkedList(arr)
|
||||
|
||||
PrintLinkedList(head)
|
||||
node := GetListNode(head, 5)
|
||||
t.Log("Find node: ", node.Val)
|
||||
}
|
127
codes/go/pkg/tree_node.go
Normal file
127
codes/go/pkg/tree_node.go
Normal file
|
@ -0,0 +1,127 @@
|
|||
// File: tree_node.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type TreeNode struct {
|
||||
Val int
|
||||
Left *TreeNode
|
||||
Right *TreeNode
|
||||
}
|
||||
|
||||
func NewTreeNode(v int) *TreeNode {
|
||||
return &TreeNode{
|
||||
Left: nil,
|
||||
Right: nil,
|
||||
Val: v,
|
||||
}
|
||||
}
|
||||
|
||||
// ArrayToTree Generate a binary tree with an array
|
||||
func ArrayToTree(arr []int) *TreeNode {
|
||||
if len(arr) <= 0 {
|
||||
return nil
|
||||
}
|
||||
root := NewTreeNode(arr[0])
|
||||
// Let container.list as queue
|
||||
queue := list.New()
|
||||
queue.PushBack(root)
|
||||
i := 1
|
||||
for queue.Len() > 0 {
|
||||
// poll
|
||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||
if i < len(arr) {
|
||||
node.Left = NewTreeNode(arr[i])
|
||||
queue.PushBack(node.Left)
|
||||
i++
|
||||
}
|
||||
if i < len(arr) {
|
||||
node.Right = NewTreeNode(arr[i])
|
||||
queue.PushBack(node.Right)
|
||||
i++
|
||||
}
|
||||
}
|
||||
return root
|
||||
}
|
||||
|
||||
// TreeToArray Serialize a binary tree to a list
|
||||
func TreeToArray(root *TreeNode) []int {
|
||||
if root == nil {
|
||||
return []int{}
|
||||
}
|
||||
arr := make([]int, 16)
|
||||
queue := list.New()
|
||||
queue.PushBack(root)
|
||||
for queue.Len() > 0 {
|
||||
node := queue.Remove(queue.Front()).(*TreeNode)
|
||||
if node != nil {
|
||||
arr = append(arr, node.Val)
|
||||
queue.PushBack(node.Left)
|
||||
queue.PushBack(node.Right)
|
||||
} else {
|
||||
arr = append(arr, -1)
|
||||
}
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// PrintTree Print a binary tree
|
||||
func PrintTree(root *TreeNode) {
|
||||
PrintTreeHelper(root, nil, false)
|
||||
}
|
||||
|
||||
// PrintTreeHelper Help to print a binary tree, hide more details
|
||||
// This tree printer is borrowed from TECHIE DELIGHT
|
||||
// https://www.techiedelight.com/c-program-print-binary-tree/
|
||||
func PrintTreeHelper(root *TreeNode, prev *trunk, isLeft bool) {
|
||||
if root == nil {
|
||||
return
|
||||
}
|
||||
prevStr := " "
|
||||
trunk := newTrunk(prev, prevStr)
|
||||
PrintTreeHelper(root.Right, trunk, true)
|
||||
if prev == nil {
|
||||
trunk.str = "———"
|
||||
} else if isLeft {
|
||||
trunk.str = "/———"
|
||||
prevStr = " |"
|
||||
} else {
|
||||
trunk.str = "\\———"
|
||||
prev.str = prevStr
|
||||
}
|
||||
showTrunk(trunk)
|
||||
fmt.Println(root.Val)
|
||||
if prev != nil {
|
||||
prev.str = prevStr
|
||||
}
|
||||
trunk.str = " |"
|
||||
PrintTreeHelper(root.Left, trunk, false)
|
||||
}
|
||||
|
||||
// trunk Help to Print tree structure
|
||||
type trunk struct {
|
||||
prev *trunk
|
||||
str string
|
||||
}
|
||||
|
||||
func newTrunk(prev *trunk, str string) *trunk {
|
||||
return &trunk{
|
||||
prev: prev,
|
||||
str: str,
|
||||
}
|
||||
}
|
||||
|
||||
func showTrunk(t *trunk) {
|
||||
if t == nil {
|
||||
return
|
||||
}
|
||||
|
||||
showTrunk(t.prev)
|
||||
fmt.Print(t.str)
|
||||
}
|
15
codes/go/pkg/tree_node_test.go
Normal file
15
codes/go/pkg/tree_node_test.go
Normal file
|
@ -0,0 +1,15 @@
|
|||
// File: tree_node_test.go
|
||||
// Created Time: 2022-11-25
|
||||
// Author: Reanon (793584285@qq.com)
|
||||
|
||||
package pkg
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestTreeNode(t *testing.T) {
|
||||
arr := []int{2, 3, 5, 6, 7}
|
||||
node := ArrayToTree(arr)
|
||||
|
||||
// print tree
|
||||
PrintTree(node)
|
||||
}
|
Loading…
Reference in a new issue