Skip to content

Commit

Permalink
fix some code
Browse files Browse the repository at this point in the history
  • Loading branch information
hunterhug committed Apr 29, 2022
1 parent 2a7eb94 commit 0b94864
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 61 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@

| 日期 | 组织/个人 | 说明 |
| ----- | ------- | ------- |
| 2022.04.21 | cy | 4.50 RMB |
| 2022.04.13 | cy | 1.50 RMB |
| 2022.04.02 | skywalker | 9.50 RMB |
| 2022.03.13 | 小桀 | 4.50 RMB |
| 2022.03.02 | 古寒飞 | 9.50 RMB |
| 2022.02.08 | 罗博贤 | 9.50 RMB |
| 2022.01.21 | 匿名 | 1.50 RMB |
Expand Down
3 changes: 2 additions & 1 deletion _coverpage.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- _coverpage.md -->

# [数据结构和算法(Golang实现) **v2.2.6**](http://www.lenggirl.com)
# [数据结构和算法(Golang实现) **v2.2.7**](http://www.lenggirl.com)

[![GitHub stars](https://img.shields.io/github/stars/hunterhug/goa.c.svg?style=social&label=Stars)](https://github.com/hunterhug/goa.c/stargazers)
[![GitHub last commit](https://img.shields.io/github/last-commit/hunterhug/goa.c.svg)](https://github.com/hunterhug/goa.c)
Expand All @@ -10,4 +10,5 @@
> 盛年不重来,一日难再晨,及时当勉励,岁月不待人。
[GitHub](https://github.com/hunterhug/goa.c)
[Blog](http://www.lenggirl.com)
<a href="/#/README" target="_blank">开始阅读</a>
6 changes: 5 additions & 1 deletion algorithm/search/rb_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (tree *RBTree) fixAfterInsertion(node *RBTNode) {

否则,需要找到叶子节点,方便新节点插进去:

```
```go
// 辅助变量 t,表示新元素要插入到该子树,t是该子树的根节点
t := tree.Root

Expand Down Expand Up @@ -2033,3 +2033,7 @@ is a rb tree
红黑树可以用来作为字典 `Map` 的基础数据结构,可以存储键值对,然后通过一个键,可以快速找到键对应的值,相比哈希表查找,不需要占用额外的空间。我们以上的代码实现只有 `value`,没有 `key:value`,可以简单改造实现字典。

`Java` 语言基础类库中的 `HashMap``TreeSet``TreeMap` 都有使用到,`C++` 语言的 `STL` 标准模板库中,`map``set` 类也有使用到。很多中间件也有使用到,比如 `Nginx`,但 `Golang` 语言标准库并没有它。

## 附录

代码下载:[https://github.com/hunterhug/goa.c/blob/master/code/rbt/main.go](https://github.com/hunterhug/goa.c/blob/master/code/rbt/main.go)
20 changes: 10 additions & 10 deletions code/bstree/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ import (
"fmt"
)

// 二叉查找树
// BinarySearchTree 二叉查找树
type BinarySearchTree struct {
Root *BinarySearchTreeNode // 树根节点
}

// 二叉查找树节点
// BinarySearchTreeNode 二叉查找树节点
type BinarySearchTreeNode struct {
Value int64 // 值
Times int64 // 值出现的次数
Left *BinarySearchTreeNode // 左子树
Right *BinarySearchTreeNode // 右字树
}

// 初始化一个二叉查找树
// NewBinarySearchTree 初始化一个二叉查找树
func NewBinarySearchTree() *BinarySearchTree {
return new(BinarySearchTree)
}

// 添加元素
// Add 添加元素
func (tree *BinarySearchTree) Add(value int64) {
// 如果没有树根,证明是棵空树,添加树根后返回
if tree.Root == nil {
Expand Down Expand Up @@ -59,7 +59,7 @@ func (node *BinarySearchTreeNode) Add(value int64) {
}
}

// 找出最小值的节点
// FindMinValue 找出最小值的节点
func (tree *BinarySearchTree) FindMinValue() *BinarySearchTreeNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand All @@ -79,7 +79,7 @@ func (node *BinarySearchTreeNode) FindMinValue() *BinarySearchTreeNode {
return node.Left.FindMinValue()
}

// 找出最大值的节点
// FindMaxValue 找出最大值的节点
func (tree *BinarySearchTree) FindMaxValue() *BinarySearchTreeNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand All @@ -99,7 +99,7 @@ func (node *BinarySearchTreeNode) FindMaxValue() *BinarySearchTreeNode {
return node.Right.FindMaxValue()
}

// 查找指定节点
// Find 查找指定节点
func (tree *BinarySearchTree) Find(value int64) *BinarySearchTreeNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand Down Expand Up @@ -130,7 +130,7 @@ func (node *BinarySearchTreeNode) Find(value int64) *BinarySearchTreeNode {
}
}

// 查找指定节点的父亲
// FindParent 查找指定节点的父亲
func (tree *BinarySearchTree) FindParent(value int64) *BinarySearchTreeNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand Down Expand Up @@ -178,7 +178,7 @@ func (node *BinarySearchTreeNode) FindParent(value int64) *BinarySearchTreeNode
}
}

// 删除指定的元素
// Delete 删除指定的元素
func (tree *BinarySearchTree) Delete(value int64) {
if tree.Root == nil {
// 如果是空树,直接返回
Expand Down Expand Up @@ -257,7 +257,7 @@ func (tree *BinarySearchTree) Delete(value int64) {
}
}

// 中序遍历
// MidOrder 中序遍历
func (tree *BinarySearchTree) MidOrder() {
tree.Root.MidOrder()
}
Expand Down
22 changes: 15 additions & 7 deletions code/interface/main2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,38 @@ import (
"reflect"
)

// 定义一个接口,有一个方法
// A 定义一个接口,有一个方法
type A interface {
Println()
}

// 定义一个接口,有两个方法
// B 定义一个接口,有两个方法
type B interface {
Println()
Printf() int
}

// 定义一个结构体
// A1Instance 定义一个结构体
type A1Instance struct {
Data string
}

// 结构体实现了Println()方法,现在它是一个 A 接口
// Println 结构体实现了Println()方法,现在它是一个 A 接口
func (a1 *A1Instance) Println() {
fmt.Println("a1:", a1.Data)
}

// 定义一个结构体
// A2Instance 定义一个结构体
type A2Instance struct {
Data string
}

// 结构体实现了Println()方法,现在它是一个 A 接口
// Println 结构体实现了Println()方法,现在它是一个 A 接口
func (a2 *A2Instance) Println() {
fmt.Println("a2:", a2.Data)
}

// 结构体实现了Printf()方法,现在它是一个 B 接口,它既是 A 又是 B 接口
// Printf 结构体实现了Printf()方法,现在它是一个 B 接口,它既是 A 又是 B 接口
func (a2 *A2Instance) Printf() int {
fmt.Println("a2:", a2.Data)
return 0
Expand All @@ -48,31 +48,39 @@ func main() {

// 将具体的结构体赋予该变量
a = &A1Instance{Data: "i love you"}

// 调用接口的方法
a.Println()

// 断言类型
if v, ok := a.(*A1Instance); ok {
fmt.Println(v)
} else {
fmt.Println("not a A1")
}

fmt.Println(reflect.TypeOf(a).String())

// 将具体的结构体赋予该变量
a = &A2Instance{Data: "i love you"}

// 调用接口的方法
a.Println()

// 断言类型
if v, ok := a.(*A1Instance); ok {
fmt.Println(v)
} else {
fmt.Println("not a A1")
}

fmt.Println(reflect.TypeOf(a).String())

// 定义一个B接口类型的变量
var b B

//b = &A1Instance{Data: "i love you"} // 不是 B 类型
b = &A2Instance{Data: "i love you"}

fmt.Println(b.Printf())
}
40 changes: 20 additions & 20 deletions code/rbt/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ const (
BLACK = false
)

// 普通红黑树
// RBTree 普通红黑树
type RBTree struct {
Root *RBTNode // 树根节点
}

// 新建一棵空树
// NewRBTree 新建一棵空树
func NewRBTree() *RBTree {
return &RBTree{}
}

// 普通红黑树节点
// RBTNode 普通红黑树节点
type RBTNode struct {
Value int64 // 值
Times int64 // 值出现的次数
Expand All @@ -31,15 +31,15 @@ type RBTNode struct {
Color bool // 父亲指向该节点的链接颜色
}

// 节点的颜色
// IsRed 节点的颜色
func IsRed(node *RBTNode) bool {
if node == nil {
return false
}
return node.Color == RED
}

// 返回节点的父亲节点
// ParentOf 返回节点的父亲节点
func ParentOf(node *RBTNode) *RBTNode {
if node == nil {
return nil
Expand All @@ -48,7 +48,7 @@ func ParentOf(node *RBTNode) *RBTNode {
return node.Parent
}

// 返回节点的左子节点
// LeftOf 返回节点的左子节点
func LeftOf(node *RBTNode) *RBTNode {
if node == nil {
return nil
Expand All @@ -57,7 +57,7 @@ func LeftOf(node *RBTNode) *RBTNode {
return node.Left
}

// 返回节点的右子节点
// RightOf 返回节点的右子节点
func RightOf(node *RBTNode) *RBTNode {
if node == nil {
return nil
Expand All @@ -66,14 +66,14 @@ func RightOf(node *RBTNode) *RBTNode {
return node.Right
}

// 设置节点颜色
// SetColor 设置节点颜色
func SetColor(node *RBTNode, color bool) {
if node != nil {
node.Color = color
}
}

// 对某节点左旋转
// RotateLeft 对某节点左旋转
func (tree *RBTree) RotateLeft(h *RBTNode) {
if h != nil {

Expand All @@ -98,7 +98,7 @@ func (tree *RBTree) RotateLeft(h *RBTNode) {
}
}

// 对某节点右旋转
// RotateRight 对某节点右旋转
func (tree *RBTree) RotateRight(h *RBTNode) {
if h != nil {

Expand All @@ -123,7 +123,7 @@ func (tree *RBTree) RotateRight(h *RBTNode) {
}
}

// 普通红黑树添加元素
// Add 普通红黑树添加元素
func (tree *RBTree) Add(value int64) {
// 根节点为空
if tree.Root == nil {
Expand Down Expand Up @@ -246,7 +246,7 @@ func (tree *RBTree) fixAfterInsertion(node *RBTNode) {
tree.Root.Color = BLACK
}

// 普通红黑树删除元素
// Delete 普通红黑树删除元素
func (tree *RBTree) Delete(value int64) {
// 查找元素是否存在,不存在则退出
p := tree.Find(value)
Expand Down Expand Up @@ -422,7 +422,7 @@ func (tree *RBTree) fixAfterDeletion(node *RBTNode) {
SetColor(node, BLACK)
}

// 找出最小值的节点
// FindMinValue 找出最小值的节点
func (tree *RBTree) FindMinValue() *RBTNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand All @@ -442,7 +442,7 @@ func (node *RBTNode) FindMinValue() *RBTNode {
return node.Left.FindMinValue()
}

// 找出最大值的节点
// FindMaxValue 找出最大值的节点
func (tree *RBTree) FindMaxValue() *RBTNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand All @@ -462,7 +462,7 @@ func (node *RBTNode) FindMaxValue() *RBTNode {
return node.Right.FindMaxValue()
}

// 查找指定节点
// Find 查找指定节点
func (tree *RBTree) Find(value int64) *RBTNode {
if tree.Root == nil {
// 如果是空树,返回空
Expand Down Expand Up @@ -493,7 +493,7 @@ func (node *RBTNode) Find(value int64) *RBTNode {
}
}

// 中序遍历
// MidOrder 中序遍历
func (tree *RBTree) MidOrder() {
tree.Root.MidOrder()
}
Expand All @@ -515,7 +515,7 @@ func (node *RBTNode) MidOrder() {
node.Right.MidOrder()
}

// 验证是不是棵红黑树
// IsRBTree 验证是不是棵红黑树
func (tree *RBTree) IsRBTree() bool {
if tree == nil || tree.Root == nil {
return true
Expand Down Expand Up @@ -548,7 +548,7 @@ func (tree *RBTree) IsRBTree() bool {
return true
}

// 节点所在的子树是否是一棵二分查找树
// IsBST 节点所在的子树是否是一棵二分查找树
func (node *RBTNode) IsBST() bool {
if node == nil {
return true
Expand Down Expand Up @@ -585,7 +585,7 @@ func (node *RBTNode) IsBST() bool {
return true
}

// 节点所在的子树是否遵循2-3-4树
// Is234 节点所在的子树是否遵循2-3-4树
func (node *RBTNode) Is234() bool {
if node == nil {
return true
Expand Down Expand Up @@ -615,7 +615,7 @@ func (node *RBTNode) Is234() bool {
return true
}

// 节点所在的子树是否平衡,是否有 blackNum 个黑链接
// IsBalanced 节点所在的子树是否平衡,是否有 blackNum 个黑链接
func (node *RBTNode) IsBalanced(blackNum int) bool {
if node == nil {
return blackNum == 0
Expand Down
2 changes: 1 addition & 1 deletion golang/basic.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 一、举个例子

现在我们来建立一个完整的程序 `main.go`
现在我们来建立一个完整的程序 `main.go`(代码较长,可以快速跳过)

```go
// Golang程序入口的包名必须为 main
Expand Down
Loading

0 comments on commit 0b94864

Please sign in to comment.