二叉排序树定义

二叉排序树,又叫二叉查找树,它或者是一棵空树;或者是具有以下性质的二叉树:

  1. 若它的左子树不空,则左子树上所有节点的值均小于它的根节点的值;
  2. 若它的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
  3. 它的左右子树也分别为二叉排序树。

二叉排序树示例图

二叉排序树操作

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
package bst

type BST struct {
	value int
	left  *BST
	right *BST
}

func InitBST(v int) *BST {
	bstNode := BST{
		value: v,
		left:  nil,
		right: nil,
	}
	return &bstNode
}

查找

更加二叉树性质实现快速查找

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
func (root *BST) Find(target int) (*BST, bool) {
	if root == nil {
		return nil, false
	}
	if root.value == target {
		return root, true
	} else if root.value < target {
		return root.right.Find(target)
	} else {
		return root.left.Find(target)
	}
}

遍历

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
func (root *BST) Ftraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.value)
	ret = append(ret, root.left.Ftraverse()...)
	ret = append(ret, root.right.Ftraverse()...)
	return
}

func (root *BST) Mtraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.left.Mtraverse()...)
	ret = append(ret, root.value)
	ret = append(ret, root.right.Mtraverse()...)
	return
}

func (root *BST) Btraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.left.Btraverse()...)
	ret = append(ret, root.right.Btraverse()...)
	ret = append(ret, root.value)
	return
}

添加

二叉排序树的添加节点,即找到对应位置增加节点即可。

二叉排序树添加节点示例图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
func (root *BST) Insert(target int) bool {
	if root == nil {
		return false
	}
	if root.value == target {
		return true
	} else if root.value < target {
		if root.right == nil {
			NewNode := InitBST(target)
			root.right = NewNode
			return true
		}
		return root.right.Insert(target)
	} else {
		if root.left == nil {
			NewNode := InitBST(target)
			root.left = NewNode
			return true
		}
		return root.left.Insert(target)
	}
}

删除

二叉树删除主要考虑一下三点情况

  • 删除节点是叶子节点,则直接删除
  • 删除节点有左或者右节点的存在,则将其存在的节点继承该删除节点的位置,则满足二叉排序树性质
  • 删除节点存在左右节点,则找到其后继节点,替换后继节点的值,删除后继节点(也可使用前驱节点操作)

二叉排序树删除节点示例图

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
func (root *BST) getMin() *BST {
	if root == nil {
		return root
	} else {
		for root.left != nil {
			root = root.left
		}
	}
	return root
}

func Delete(root *BST, target int) *BST{
	if root == nil {
		return nil
	}
	if root.value < target{
		root.right = Delete(root.right,target)
		return root
	} else if root.value > target {
		root.left = Delete(root.left,target)
		return root
	} else {
		if root.left == nil && root.right == nil {
			return nil
		} else if root.left != nil && root.right == nil {
			return root.left
		} else if root.left == nil && root.right != nil {
			return root.right
		} else {
			minNode := root.right.getMin()
			root.value = minNode.value
			minNode.value = target
			root.right = Delete(root.right,target)
			return root
		}
	}
}

整体代码以及单测

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package bst

type BST struct {
	value int
	left  *BST
	right *BST
}

func InitBST(v int) *BST {
	bstNode := BST{
		value: v,
		left:  nil,
		right: nil,
	}
	return &bstNode
}

func (root *BST) Find(target int) (*BST, bool) {
	if root == nil {
		return nil, false
	}
	if root.value == target {
		return root, true
	} else if root.value < target {
		return root.right.Find(target)
	} else {
		return root.left.Find(target)
	}
}

func (root *BST) Ftraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.value)
	ret = append(ret, root.left.Ftraverse()...)
	ret = append(ret, root.right.Ftraverse()...)
	return
}

func (root *BST) Mtraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.left.Mtraverse()...)
	ret = append(ret, root.value)
	ret = append(ret, root.right.Mtraverse()...)
	return
}

func (root *BST) Btraverse() (ret []int) {
	if root == nil {
		return
	}
	ret = append(ret, root.left.Btraverse()...)
	ret = append(ret, root.right.Btraverse()...)
	ret = append(ret, root.value)
	return
}

func (root *BST) Insert(target int) bool {
	if root == nil {
		return false
	}
	if root.value == target {
		return true
	} else if root.value < target {
		if root.right == nil {
			NewNode := InitBST(target)
			root.right = NewNode
			return true
		}
		return root.right.Insert(target)
	} else {
		if root.left == nil {
			NewNode := InitBST(target)
			root.left = NewNode
			return true
		}
		return root.left.Insert(target)
	}
}

func (root *BST) getMin() *BST {
	if root == nil {
		return root
	} else {
		for root.left != nil {
			root = root.left
		}
	}
	return root
}

func Delete(root *BST, target int) *BST{
	if root == nil {
		return nil
	}
	if root.value < target{
		root.right = Delete(root.right,target)
		return root
	} else if root.value > target {
		root.left = Delete(root.left,target)
		return root
	} else {
		if root.left == nil && root.right == nil {
			return nil
		} else if root.left != nil && root.right == nil {
			return root.left
		} else if root.left == nil && root.right != nil {
			return root.right
		} else {
			minNode := root.right.getMin()
			root.value = minNode.value
			minNode.value = target
			root.right = Delete(root.right,target)
			return root } } } //===================单测=================
package bst

import (
	"math/rand"
	"testing"
	"time"
)

func buildBST() *BST {
	node := InitBST(8)
	vs := []int{5, 10, 3, 6, 9, 16}
	for _, i := range vs {
		node.Insert(i)
	}
	return node
}

func getRandomArray() []int {
	vs := []int{3, 5, 6, 9, 10, 16}
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < 6; i++ {
		ri := rand.Intn(6)
		tmp := vs[i]
		vs[i] = vs[ri]
		vs[ri] = tmp
	}
	return vs
}

func TestInitBST(t *testing.T) {
	node := InitBST(8)
	if node.value != 8 {
		t.Error("init value is error")
	}
	if node.left != nil {
		t.Error("init value is error")
	}
	if node.right != nil {
		t.Error("init value is error")
	}
}

func TestInsert(t *testing.T) {
	var node *BST
	ok := node.Insert(10)
	if ok {
		t.Error("nil BST insert fail")
	}
	node = InitBST(8)
	vs := []int{5, 10, 3, 6, 6, 9, 16}
	for _, i := range vs {
		ok = node.Insert(i)
		if !ok {
			t.Error("node insert fail")
		}
	}
	i := 0
	nodeSet := make([]*BST, 0)
	nodeSet = append(nodeSet, node)
	tl := len(nodeSet)
	for true {
		for ; i < tl; i++ {
			if nodeSet[i].left != nil {
				nodeSet = append(nodeSet, nodeSet[i].left)
			}
			if nodeSet[i].right != nil {
				nodeSet = append(nodeSet, nodeSet[i].right)
			}
		}
		tl = len(nodeSet)
		if i >= tl {
			break
		}
	}
	spv := []int{8, 5, 10, 3, 6, 9, 16}
	for i, nodei := range nodeSet {
		if nodei.value != spv[i] {
			t.Error("insert fail")
		}
	}
}

func TestFind(t *testing.T) {
	var node *BST
	node = buildBST()
	fn, ok := node.Find(20)
	if fn != nil || ok {
		t.Error("nil find faild")
	}
	fn, ok = node.Find(6)
	if fn == nil || !ok {
		t.Error("nil find faild")
	}
	if fn.value != 6 {
		t.Error("find wrang node")
	}
}

func TestFtraverse(t *testing.T) {
	node := buildBST()
	ret := node.Ftraverse()
	if len(ret) == 0 {
		t.Error("error ,can't get value")
	}
	spv := []int{8, 5, 3, 6, 10, 9, 16}
	for i, v := range ret {
		if v != spv[i] {
			t.Error("error, not f traverse")
		}
	}
}

func TestMtraverse(t *testing.T) {
	node := buildBST()
	ret := node.Mtraverse()
	if len(ret) == 0 {
		t.Error("error ,can't get value")
	}
	spv := []int{3, 5, 6, 8, 9, 10, 16}
	for i, v := range ret {
		if v != spv[i] {
			t.Error("error, not m traverse")
		}
	}
}

func TestBtraverse(t *testing.T) {
	node := buildBST()
	ret := node.Btraverse()
	if len(ret) == 0 {
		t.Error("error ,can't get value")
	}
	spv := []int{3, 6, 5, 9, 16, 10, 8}
	for i, v := range ret {
		if v != spv[i] {
			t.Error("error, not b traverse")
		}
	}
}

func TestDelete(t *testing.T) {
	node := buildBST()
	ok := Delete(node, 22)
	if ok == nil {
		t.Error("delete a wrong value")
	}
	rv := getRandomArray()
	for _, v := range rv {
		ok := Delete(node, v)
		if ok == nil {
			t.Error("delete faild")
		}
		isOk := isBST(node,-1,999)
		if isOk == false {
			t.Error("delete faild")
		}
	}
	ok = Delete(node, 8)
	if ok != nil {
		t.Error("can't delete root")
	}
}

func isBST(root *BST, minV int, maxV int) bool{
	if root == nil {
		return true
	}
	if root.value < minV || root.value > maxV {
		return false
	}
	return isBST(root.left,minV,root.value) && isBST(root.right,root.value,maxV)
}