Skip to content

Commit 18962e9

Browse files
committed
二叉树
1 parent 3a9fffe commit 18962e9

File tree

4 files changed

+259
-19
lines changed

4 files changed

+259
-19
lines changed

data-structures/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
## 数据结构
2+
3+
[概念](https://blog.csdn.net/qq_31196849/article/details/78529724)
4+
5+
[数据结构汇总](https://www.jianshu.com/p/2469a4d9708e)
6+
7+
[算法时间复杂度的计算](http://univasity.iteye.com/blog/1164707)
8+
9+
### 逻辑结构:
10+
11+
具体问题抽象出来的数学模型
12+
13+
1. 集合(数据元素之间无关系)
14+
2. 线性结构(1:1)
15+
3. 树形结构(1:n)
16+
4. 图状结构(n:n)
17+
18+
### 存储结构/物理结构:
19+
20+
数据在计算机内存中的存储(顺序结构 链式结构 索引结构 哈希结构)
21+
22+
### 抽象数据类型(ADT):
23+
24+
ADT=(D,S, P) D是数据对象 S是D上的关系集 P是对D的基本操作集
25+
类似面向对象程序中的类
26+
27+
28+
### 算法和其5个特性:
29+
30+
算法是描述计算机解决给定问题的操作过程,即为决解某一特定问题而由若干条指令组成的有穷序列
31+
32+
有穷行 确定性 可行行 输入 输出
33+
34+
### 线性表
35+
36+
线性表是n(n>0)个相同类型数据元素构成的有限序列,其中n为线性表的长度
37+
38+
1. 顺序存储(顺序表):数组(连续的存储单元)
39+
2. 链式存储:每个元素(data + next指针,不连续的存储单元,链表不是随机存取结构,只能顺序存取
40+
41+
#### 链表
42+
43+
1. 单向链表:
44+
2. 循环链表:表中的尾节点指向头节点,形成一个环,操作和线性链表基本一致,没有NULL指针,故终止条件为,是否等于某一个指定的指针
45+
3. 双向链表:双向链表是在单链表的每个结点里再增加一个指向其直接前驱的指针域prior。这样就形成了链表中有两个方向不同的链
46+
47+
### 栈和队列
48+
49+
#### 栈(LIFO)
50+
51+
限制在表的一端进行插入和删除运算的特殊线性表
52+
53+
##### 栈的存储
54+
55+
顺序栈:利用一组地址连续的存储单元依次存放自栈底到栈顶的数据元素
56+
57+
#### 队列(FIFO)
58+
59+
60+
61+

data-structures/binaryTree/bst.go

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
package binaryTree
2+
3+
//二叉树
4+
//二叉树是每个节点最多有两个子树的树结构
5+
type ElementType int//节点数据
6+
//结点
7+
type Node struct {
8+
Value ElementType
9+
Parent *Node
10+
Left *Node
11+
Right *Node
12+
}
13+
14+
//创建节点
15+
func NewNode(v ElementType) *Node {
16+
return &Node{Value:v}
17+
}
18+
19+
/*
20+
*节点比较
21+
* n>m:1 n<m:-1 n=m:0
22+
*/
23+
func (n *Node) Compare(m *Node) int {
24+
if n.Value < m.Value {
25+
return -1
26+
} else if n.Value > m.Value {
27+
return 1
28+
} else {
29+
return 0
30+
}
31+
}
32+
33+
//树
34+
type Tree struct {
35+
Head *Node
36+
Size int
37+
}
38+
39+
//创建树
40+
func NewTree(n *Node) *Tree {
41+
if n == nil {
42+
return &Tree{}
43+
}
44+
return &Tree{Head:n,Size:1}
45+
}
46+
47+
//插入,相同的节点值,放到右子树
48+
func (t *Tree) Insert(i ElementType) {
49+
n := NewNode(i)//创建节点
50+
if t.Head == nil {//判断树的根节点
51+
t.Head = n
52+
t.Size++
53+
return
54+
}
55+
56+
h := t.Head
57+
58+
for {
59+
if n.Compare(h) == -1 {//小于parent,到左子节点
60+
if h.Left == nil {//无左子节点
61+
h.Left = n
62+
n.Parent = h
63+
break
64+
} else {
65+
h = h.Left
66+
}
67+
} else {//大于parent
68+
if h.Right == nil {
69+
h.Right = n
70+
n.Parent = h
71+
break
72+
} else {
73+
h = h.Right
74+
}
75+
}
76+
}
77+
t.Size++
78+
}
79+
80+
//搜索
81+
func (t *Tree) Search(i ElementType) *Node {
82+
h := t.Head
83+
n := NewNode(i)
84+
for h != nil {
85+
switch h.Compare(n) {
86+
case -1:
87+
h = h.Right
88+
case 1:
89+
h = h.Left
90+
case 0:
91+
return h
92+
default:
93+
panic("Node not found")
94+
}
95+
}
96+
panic("Node not found")
97+
}
98+
99+
//删除
100+
func (t *Tree) Delete(i ElementType) bool {
101+
var parent *Node
102+
103+
h := t.Head
104+
n := NewNode(i)
105+
106+
for h != nil {
107+
switch n.Compare(h) {
108+
case -1:
109+
parent = h
110+
h = h.Left
111+
case 1:
112+
parent = h
113+
h = h.Right
114+
case 0:
115+
if h.Left != nil {
116+
right := h.Right
117+
h.Value = h.Left.Value
118+
h.Left = h.Left.Left
119+
h.Right = h.Left.Right
120+
121+
if right != nil {
122+
subTree := &Tree{Head: h}
123+
IterOnTree(right, func(n *Node) {
124+
subTree.Insert(n.Value)
125+
})
126+
}
127+
t.Size--
128+
return true
129+
}
130+
131+
if h.Right != nil {
132+
h.Value = h.Right.Value
133+
h.Left = h.Right.Left
134+
h.Right = h.Right.Right
135+
136+
t.Size--
137+
return true
138+
}
139+
140+
if parent == nil {
141+
t.Head = nil
142+
t.Size--
143+
return true
144+
}
145+
146+
if parent.Left == n {
147+
parent.Left = nil
148+
} else {
149+
parent.Right = nil
150+
}
151+
t.Size--
152+
return true
153+
}
154+
}
155+
return false
156+
}
157+
158+
func IterOnTree(n *Node, f func(*Node)) bool {
159+
if n == nil {
160+
return true
161+
}
162+
if !IterOnTree(n.Left, f) {
163+
return false
164+
}
165+
166+
f(n)
167+
168+
return IterOnTree(n.Right, f)
169+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package binaryTree
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
9+
func TestNode_Compare(t *testing.T) {
10+
n := NewNode(1)
11+
m := NewNode(2)
12+
k := NewNode(1)
13+
assert.Equal(t,-1,n.Compare(m))
14+
assert.Equal(t,0,n.Compare(k))
15+
assert.Equal(t,1,m.Compare(k))
16+
}
17+
18+
func TestTree(t *testing.T) {
19+
max_size := 10
20+
tree := NewTree(nil)
21+
for i := 0; i < max_size ; i++ {
22+
tree.Insert(ElementType(i))
23+
}
24+
assert.Equal(t,10,tree.Size)
25+
assert.Equal(t,ElementType(5),tree.Search(5).Value)
26+
assert.Equal(t,true,tree.Delete(5))
27+
assert.Equal(t,9,tree.Size)
28+
29+
}

structures/README.md

Lines changed: 0 additions & 19 deletions
This file was deleted.

0 commit comments

Comments
 (0)