-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary_search_tree.go
268 lines (239 loc) · 5.85 KB
/
binary_search_tree.go
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
///main package has examples shown
// in Go Data Structures and algorithms book
package main
// importing fmt package
import (
"fmt"
"sync"
)
// TreeNode class
type TreeNode struct {
key int
value int
leftNode *TreeNode
rightNode *TreeNode
}
// BinarySearchTree class
type BinarySearchTree struct {
rootNode *TreeNode
lock sync.RWMutex
}
// InsertElement method
func (tree *BinarySearchTree) InsertElement(key int, value int) {
tree.lock.Lock()
defer tree.lock.Unlock()
var treeNode *TreeNode
treeNode = &TreeNode{key, value, nil, nil}
if tree.rootNode == nil {
tree.rootNode = treeNode
} else {
insertTreeNode(tree.rootNode, treeNode)
}
}
// insertTreeNode method
func insertTreeNode(rootNode *TreeNode, newTreeNode *TreeNode) {
if newTreeNode.key < rootNode.key {
if rootNode.leftNode == nil {
rootNode.leftNode = newTreeNode
} else {
insertTreeNode(rootNode.leftNode, newTreeNode)
}
} else {
if rootNode.rightNode == nil {
rootNode.rightNode = newTreeNode
} else {
insertTreeNode(rootNode.rightNode, newTreeNode)
}
}
}
// InOrderTraverseTree method
func (tree *BinarySearchTree) InOrderTraverseTree(function func(int)) {
tree.lock.RLock()
defer tree.lock.RUnlock()
inOrderTraverseTree(tree.rootNode, function)
}
// inOrderTraverseTree method
func inOrderTraverseTree(treeNode *TreeNode, function func(int)) {
if treeNode != nil {
inOrderTraverseTree(treeNode.leftNode, function)
function(treeNode.value)
inOrderTraverseTree(treeNode.rightNode, function)
}
}
// PreOrderTraverse method
func (tree *BinarySearchTree) PreOrderTraverseTree(function func(int)) {
tree.lock.Lock()
defer tree.lock.Unlock()
preOrderTraverseTree(tree.rootNode, function)
}
// preOrderTraverseTree method
func preOrderTraverseTree(treeNode *TreeNode, function func(int)) {
if treeNode != nil {
function(treeNode.value)
preOrderTraverseTree(treeNode.leftNode, function)
preOrderTraverseTree(treeNode.rightNode, function)
}
}
// PostOrderTraverseTree method
func (tree *BinarySearchTree) PostOrderTraverseTree(function func(int)) {
tree.lock.Lock()
defer tree.lock.Unlock()
postOrderTraverseTree(tree.rootNode, function)
}
// postOrderTraverseTree method
func postOrderTraverseTree(treeNode *TreeNode, function func(int)) {
if treeNode != nil {
postOrderTraverseTree(treeNode.leftNode, function)
postOrderTraverseTree(treeNode.rightNode, function)
function(treeNode.value)
}
}
// MinNode method
func (tree *BinarySearchTree) MinNode() *int {
tree.lock.RLock()
defer tree.lock.RUnlock()
var treeNode *TreeNode
treeNode = tree.rootNode
if treeNode == nil {
return (*int)(nil)
}
for {
if treeNode.leftNode == nil {
return &treeNode.value
}
treeNode = treeNode.leftNode
}
}
// MaxNode method
func (tree *BinarySearchTree) MaxNode() *int {
tree.lock.RLock()
defer tree.lock.RUnlock()
var treeNode *TreeNode
treeNode = tree.rootNode
if treeNode == nil {
return (*int)(nil)
}
for {
if treeNode.rightNode == nil {
return &treeNode.value
}
treeNode = treeNode.rightNode
}
}
// SearchNode method
func (tree *BinarySearchTree) SearchNode(key int) bool {
tree.lock.RLock()
defer tree.lock.RUnlock()
return searchNode(tree.rootNode, key)
}
// searchNode method
func searchNode(treeNode *TreeNode, key int) bool {
if treeNode == nil {
return false
}
if key < treeNode.key {
return searchNode(treeNode.leftNode, key)
}
if key > treeNode.key {
return searchNode(treeNode.rightNode, key)
}
return true
}
// RemoveNode method
func (tree *BinarySearchTree) RemoveNode(key int) {
tree.lock.Lock()
defer tree.lock.Unlock()
removeNode(tree.rootNode, key)
}
// removeNode method
func removeNode(treeNode *TreeNode, key int) *TreeNode {
if treeNode == nil {
return nil
}
if key < treeNode.key {
treeNode.leftNode = removeNode(treeNode.leftNode, key)
return treeNode
}
if key > treeNode.key {
treeNode.rightNode = removeNode(treeNode.rightNode, key)
return treeNode
}
if treeNode.leftNode == nil && treeNode.rightNode == nil {
treeNode = nil
return nil
}
if treeNode.leftNode == nil {
treeNode = treeNode.rightNode
return treeNode
}
if treeNode.rightNode == nil {
treeNode = treeNode.leftNode
return treeNode
}
var leftmostrightNode *TreeNode
leftmostrightNode = treeNode.rightNode
for {
if leftmostrightNode != nil && leftmostrightNode.leftNode != nil {
leftmostrightNode = leftmostrightNode.leftNode
} else {
break
}
}
treeNode.key, treeNode.value = leftmostrightNode.key, leftmostrightNode.value
treeNode.rightNode = removeNode(treeNode.rightNode, treeNode.key)
return treeNode
}
// String method
func (tree *BinarySearchTree) String() {
tree.lock.Lock()
defer tree.lock.Unlock()
fmt.Println("************************************************")
stringify(tree.rootNode, 0)
fmt.Println("************************************************")
}
// stringify method
func stringify(treeNode *TreeNode, level int) {
if treeNode != nil {
format := ""
for i := 0; i < level; i++ {
format += " "
}
format += "***> "
level++
stringify(treeNode.leftNode, level)
fmt.Printf(format+"%d\n", treeNode.key)
stringify(treeNode.rightNode, level)
}
}
// print method
func print(tree *BinarySearchTree) {
if tree != nil {
fmt.Println(" Value", tree.rootNode.value)
fmt.Printf("Root Tree Node")
printTreeNode(tree.rootNode)
} else {
fmt.Printf("Nil\n")
}
}
//printTreeNode method
func printTreeNode(treeNode *TreeNode) {
if treeNode != nil {
fmt.Println(" Value", treeNode.value)
fmt.Printf("TreeNode Left")
printTreeNode(treeNode.leftNode)
fmt.Printf("TreeNode Right")
printTreeNode(treeNode.rightNode)
} else {
fmt.Printf("Nil\n")
}
}
// main method
func main() {
var tree *BinarySearchTree = &BinarySearchTree{}
tree.InsertElement(8, 8)
tree.InsertElement(3, 3)
tree.InsertElement(10, 10)
tree.InsertElement(1, 1)
tree.InsertElement(6, 6)
tree.String()
}