-
Notifications
You must be signed in to change notification settings - Fork 20
/
LinkBST.go
266 lines (234 loc) · 6.15 KB
/
LinkBST.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
/*
Binary Search Tree using Linked List
*/
package main
import "fmt"
type Node struct {
Data int
Left,Right *Node
}
// Return data: O(1)
func (n *Node) GetData() int {
return n.Data
}
// Set data: O(1)
func (n *Node) SetData(data int) {
n.Data = data
}
// Return left child: O(1)
func (n *Node) GetLeft() *Node {
return n.Left
}
// Set left child: O(1)
func (n *Node) SetLeft(left *Node) {
n.Left = left
}
// Test whether node has left child: O(1)
func (n *Node) HasLeft() bool {
if n.Left == nil { return false }
return true
}
// Return right child: O(1)
func (n *Node) GetRight() *Node {
return n.Right
}
// Set right child: O(1)
func (n *Node) SetRight(right *Node) {
n.Right = right
}
// Test whether node has right child: O(1)
func (n *Node) HasRight() bool {
if n.Right == nil { return false }
return true
}
type BST struct {
Root *Node
}
// Create a new Binary Search Tree: O(1)
func NewBST() *BST {
return &BST{}
}
// Search for a value in BST: O(log n)
func (b *BST) Search(data int) bool {
if b.Root == nil { return false }
current := b.Root
for {
if data < current.GetData() {
if current.HasLeft() {
current = current.GetLeft()
} else {
break
}
} else if data > current.GetData(){
if current.HasRight() {
current = current.GetRight()
} else {
break
}
} else {
return true
}
}
return false
}
// Return a minimum value from a subtree in BST: O(log n)
func (b *BST) SearchMin(current *Node) int {
if current.HasLeft() { return b.SearchMin(current.GetLeft()) }
return current.GetData()
}
// Insert a new value to BST: O(log n)
func (b *BST) Insert(data int) {
newnode := &Node{data,nil,nil}
if b.Root == nil { b.Root = newnode; return; }
current := b.Root
for {
if data < current.GetData() {
if current.HasLeft() {
current = current.GetLeft()
} else {
current.SetLeft(newnode)
break
}
} else if data > current.GetData() {
if current.HasRight() {
current = current.GetRight()
} else {
current.SetRight(newnode)
break
}
} else {
break
}
}
}
// Delete a value from BST: O(log n)
func (b *BST) Delete(data int) {
if b.Root == nil { return }
if b.Root.GetData() == data {
auxroot := &Node{0,b.Root,nil}
b.delete(b.Root,auxroot,data)
b.Root = auxroot.GetLeft()
} else {
b.delete(b.Root,nil,data)
}
}
func (b *BST) delete(current, parent *Node, data int) {
if data < current.GetData() {
if current.GetLeft() == nil { return }
b.delete(current.GetLeft(),current,data)
} else if data > current.GetData() {
if current.GetRight() == nil { return }
b.delete(current.GetRight(),current,data)
} else {
if current.HasLeft() && current.HasRight() {
current.SetData(b.SearchMin(current.GetRight()))
b.delete(current.GetRight(),current,current.GetData())
} else if parent.GetLeft() == current {
if current.HasLeft() {
parent.SetLeft(current.GetLeft())
} else {
parent.SetLeft(current.GetRight())
}
} else if parent.GetRight() == current {
if current.HasRight() {
parent.SetRight(current.GetRight())
} else {
parent.SetRight(current.GetLeft())
}
}
}
}
// Print BST with pre-order traversal: O(n)
func (b *BST) Preorder() {
if b.Root == nil { return }
b.preorder(b.Root)
fmt.Println()
}
func (b *BST) preorder(current *Node) {
fmt.Print(current.GetData()," ")
if current.HasLeft() { b.preorder(current.GetLeft()) }
if current.HasRight() { b.preorder(current.GetRight()) }
}
// Print BST with in-order traversal: O(n)
func (b *BST) Inorder() {
if b.Root == nil { return }
b.inorder(b.Root)
fmt.Println()
}
func (b *BST) inorder(current *Node) {
if current.HasLeft() { b.inorder(current.GetLeft()) }
fmt.Print(current.GetData()," ")
if current.HasRight() { b.inorder(current.GetRight()) }
}
// Print BST with post-order traversal: O(n)
func (b *BST) Postorder() {
if b.Root == nil { return }
b.postorder(b.Root)
fmt.Println()
}
func (b *BST) postorder(current *Node) {
if current.HasLeft() { b.postorder(current.GetLeft()) }
if current.HasRight() { b.postorder(current.GetRight()) }
fmt.Print(current.GetData()," ")
}
// Print BST with BFS: O(n)
func (b *BST) BFSPrinting() {
if b.Root == nil { return }
q := make([]*Node,0,0)
q = append(q,b.Root)
for len(q) != 0 {
front := q[0]
q = q[1:]
fmt.Print(front.GetData()," ")
if front.HasLeft() { q = append(q,front.GetLeft()) }
if front.HasRight() { q = append(q,front.GetRight()) }
}
fmt.Println()
}
func main() {
b := NewBST()
fmt.Println("Insert 8, 3, 10, 1, 6, 14, 4, 7, 13")
b.Insert(8)
b.Insert(3)
b.Insert(10)
b.Insert(1)
b.Insert(6)
b.Insert(14)
b.Insert(4)
b.Insert(7)
b.Insert(13)
fmt.Println()
fmt.Print("Preorder: ")
b.Preorder()
fmt.Print("Inorder: ")
b.Inorder()
fmt.Print("Postorder: ")
b.Postorder()
fmt.Print("BFS: ")
b.BFSPrinting()
fmt.Println()
for i := 1; i <= 20; i++ {
fmt.Printf("Searching for %d: ",i)
if b.Search(i) { fmt.Println("Found") } else { fmt.Println("Not found") }
}
fmt.Println()
fmt.Println("Delete 8, 6, 4, 13")
b.Delete(8)
b.Delete(6)
b.Delete(4)
b.Delete(13)
fmt.Println()
fmt.Print("Preorder: ")
b.Preorder()
fmt.Print("Inorder: ")
b.Inorder()
fmt.Print("Postorder: ")
b.Postorder()
fmt.Print("BFS: ")
b.BFSPrinting()
fmt.Println()
for i := 1; i <= 20; i++ {
fmt.Printf("Searching for %d: ",i)
if b.Search(i) { fmt.Println("Found") } else { fmt.Println("Not found") }
}
}