Add Go codes to docs, including

the chapter of stack and queue, the chapter of tree.
This commit is contained in:
Yudong Jin
2022-12-03 20:25:24 +08:00
parent 1d9a076cdd
commit ebf9024136
41 changed files with 898 additions and 486 deletions

View File

@@ -4,12 +4,6 @@
package pkg
import (
"fmt"
"strconv"
"strings"
)
// ListNode Definition for a singly-linked list node
type ListNode struct {
Next *ListNode
@@ -43,17 +37,3 @@ func GetListNode(node *ListNode, val int) *ListNode {
}
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())
}

View File

@@ -4,7 +4,10 @@
package pkg
import "testing"
import (
"fmt"
"testing"
)
func TestListNode(t *testing.T) {
arr := []int{2, 3, 5, 6, 7}
@@ -12,5 +15,5 @@ func TestListNode(t *testing.T) {
PrintLinkedList(head)
node := GetListNode(head, 5)
t.Log("Find node: ", node.Val)
fmt.Println("Find node: ", node.Val)
}

View File

@@ -0,0 +1,98 @@
// File: print_utils.go
// Created Time: 2022-12-03
// Author: Reanon (793584285@qq.com), Krahets (krahets@163.com)
package pkg
import (
"container/list"
"fmt"
"strconv"
"strings"
)
func PrintSlice(nums []int) {
fmt.Printf("%v", nums)
fmt.Println()
}
// PrintList Print a list
func PrintList(list *list.List) {
e := list.Front()
// 强转为 string, 会影响效率
fmt.Print("[")
for e.Next() != nil {
fmt.Print(e.Value, " ")
e = e.Next()
}
fmt.Print(e.Value, "]\n")
}
// 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())
}
// 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)
}

View File

@@ -6,7 +6,6 @@ package pkg
import (
"container/list"
"fmt"
)
type TreeNode struct {
@@ -71,58 +70,3 @@ func TreeToArray(root *TreeNode) []any {
}
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)
}

View File

@@ -4,7 +4,10 @@
package pkg
import "testing"
import (
"fmt"
"testing"
)
func TestTreeNode(t *testing.T) {
arr := []int{2, 3, 5, 6, 7}
@@ -14,5 +17,5 @@ func TestTreeNode(t *testing.T) {
PrintTree(node)
// tree to arr
t.Log(TreeToArray(node))
fmt.Println(TreeToArray(node))
}