-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinarySearchTree.go
executable file
·395 lines (343 loc) · 9.16 KB
/
BinarySearchTree.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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
package main
import (
"fmt"
"sort"
)
type TreeNode struct{
Val int
Left *TreeNode
Right *TreeNode
}
var smallestFromLeaf string = string('z'+1)
// func BSTFromPreorder(preorder []int) *TreeNode{
// }
func ProfitableSchemesRecursion(G, p, P, i int, schemes *int){
if G == 0 && p >= P{
}
}
func ProfitableSchemes(G int, P int, group []int, profit []int) int{
var schemes int = 0
}
func DFSDiameterOfBinaryTree(root *TreeNode, max *int) int{
if root == nil{
return 0
}
left, right := 0, 0
left = DFSDiameterOfBinaryTree(root.Left, max)
right = DFSDiameterOfBinaryTree(root.Right, max)
*max = int(math.Max(float64(*max), float64(left+right+1)))
return int(math.Max(float64(left), float64(right))) + 1
}
func DiameterOfBinaryTree(root *TreeNode)int{
var max int = 0
DFSDiameterOfBinaryTree(root, &max)
return int(math.Max(float64(max-1), float64(0)))
}
// func TrimBST(root *TreeNode, L int, R int) *TreeNode{
// queue := []*TreeNode{root}
// for len(queue) > 0{
// }
// }
func InorderIncreasingBST(node *TreeNode, result *[]*TreeNode){
if node != nil{
InorderIncreasingBST(node.Left, result)
*result = append(*result, node)
InorderIncreasingBST(node.Right, result)
}
}
func IncreasingBST(root *TreeNode) *TreeNode{
var result []*TreeNode
InorderIncreasingBST(root, &result)
result = append(result, nil)
for i := range result{
if result[i] != nil{
result[i].Right = result[i+1]
result[i].Left = nil
}
}
return result[0]
}
func _SumRootToLeaf(node *TreeNode, result *int, path []int) int{
if node != nil{
path = append(path, node.Val)
if node.Left == nil && node.Right == nil{
curr := 0
for i:=0; i < len(path); i++{ curr += (path[i] * int(math.Pow(float64(2), float64(len(path)-1 - i)))) }
*result = (*result) + curr
} else{
_SumRootToLeaf(node.Left, result, path)
_SumRootToLeaf(node.Right, result, path)
}
}
}
func DFSAllPossibleFBT(node *TreeNode, N int, result []*TreeNode){
if N == 0{
}
}
func AllPossibleFBT(N int) []*TreeNode{
root := &TreeNode{
Val: 0,
Left: nil,
Right: nil,
}
var result []*TreeNode
DFSAllPossibleFBT(root, N-1, &result)
}
func _FindTilt(node *TreeNode, result *int) int{
if node == nil{
return 0
}
leftTilt := _FindTilt(node.Left, result)
rightTilt := _FindTilt(node.Right, result)
*result = (*result) + int(math.Abs(float64(leftTilt) - float64(rightTilt)))
return node.Val + leftTilt + rightTilt
}
func FindTilt(root *TreeNode) int{
var result int = 0
_FindTilt(root, &result)
return result
}
func SumRootToLeaf(root *TreeNode) int{
var result int = 0
_SumRootToLeaf(root, &result, []int{})
return result
}
func _ConvertBST(node *TreeNode, prev int) int{
if node != nil{
nodeTemp := node.Val
right := _ConvertBST(node.Right, prev)
node.Val += right + prev
left := _ConvertBST(node.Left, node.Val)
return nodeTemp + right + left
}
return 0
}
func ConvertBST(root *TreeNode) *TreeNode{
_ConvertBST(root, 0)
return root
}
func DFSDelNodes(root *TreeNode, result *[]*TreeNode, storeMap *map[int]int){
if root != nil{
DFSDelNodes(root.Left, result, storeMap)
DFSDelNodes(root.Right, result, storeMap)
if root.Left != nil{
if _, ok := (*storeMap)[root.Left.Val]; ok{
root.Left = nil
}
}
if root.Right != nil{
if _, ok := (*storeMap)[root.Right.Val]; ok{
root.Right = nil
}
}
if _, ok := (*storeMap)[root.Val]; ok{
if root.Left != nil{
*result = append(*result, root.Left)
}
if root.Right != nil{
*result = append(*result, root.Right)
}
}
}
}
func DelNodes(root *TreeNode, to_delete []int) []*TreeNode{
var result []*TreeNode
var storeMap map[int]int = make(map[int]int)
for i := range to_delete{
storeMap[to_delete[i]] = 1
}
dummy := &TreeNode{
Val: 0,
Left: root,
Right: nil,
}
DFSDelNodes(dummy, &result, storeMap)
if dummy.Left != nil{
result = append(result, root)
}
return result
}
func DFSBinaryTree(node *TreeNode) []int{
var stack []*TreeNode = []*TreeNode{node}
var currentNode *TreeNode
var result []int
for len(stack) != 0{
currentNode, stack = stack[len(stack)-1], stack[:len(stack)-1]
if currentNode.Right != nil{
stack = append(stack, currentNode.Right)
}
if currentNode.Left != nil{
stack = append(stack, currentNode.Left)
}
if currentNode.Left == nil && currentNode.Right == nil{
result = append(result, currentNode.Val)
}
}
return result
}
func LeafSimilar(root1, root2 *TreeNode) bool{
return DFSBinaryTree(root1) == DFSBinaryTree(root2)
}
func InsertNode(root, newNode *TreeNode) bool{
temp := root
for temp != nil{
if temp.Val < newNode.Val{
if temp.Right != nil{
temp = temp.Right
} else{
temp.Right = newNode
return true
}
} else{
if temp.Left != nil{
temp = temp.Left
} else{
temp.Left = newNode
return true
}
}
}
return false
}
func DeleteNode(root, node *TreeNode) bool{
return false
}
func VerticalTraversal(root *TreeNode) [][]int{
storeMap := make(map[int]map[int][]int)
queue := []*TreeNode{root}
var node *TreeNode
var coordinates []int
queue_coordinates := [][]int{{0,0}}
for len(queue) > 0{
node, queue = queue[0], queue[1:]
coordinates, queue_coordinates = queue_coordinates[0], queue_coordinates[1:]
// fmt.Println("Entering with queue: ", node)
// fmt.Println("The coordinates: ", coordinates)
if len(storeMap[coordinates[0]]) == 0{
// fmt.Println("init for x: ", coordinates[0])
storeMap[coordinates[0]] = make(map[int][]int)
}
storeMap[coordinates[0]][coordinates[1]] = append(storeMap[coordinates[0]][coordinates[1]], node.Val)
if node.Left != nil{
queue_coordinates = append(queue_coordinates, []int{ coordinates[0] - 1, coordinates[1] - 1 })
queue = append(queue, node.Left)
}
if node.Right != nil{
queue_coordinates = append(queue_coordinates, []int{ coordinates[0] + 1, coordinates[1] - 1 })
queue = append(queue, node.Right)
}
// fmt.Println("Exiting with queue: ", queue)
}
// fmt.Println(storeMap)
var sortedArray []int
for key := range storeMap{
sortedArray = append(sortedArray, key)
}
sort.Ints(sortedArray)
// fmt.Println("SortedX: ", sortedArray)
var result [][]int
for x := range sortedArray{
sortedY := []int{}
for y := range storeMap[sortedArray[x]]{
sortedY = append(sortedY, y)
}
sort.Ints(sortedY)
i := len(sortedY) - 1
temp := []int{}
for i >= 0{
// fmt.Printf("For x: %d, y: %d\n", sortedArray[x], sortedY[i])
sort.Ints(storeMap[sortedArray[x]][sortedY[i]])
for j := range storeMap[sortedArray[x]][sortedY[i]]{
temp = append(temp, storeMap[sortedArray[x]][sortedY[i]][j])
}
i--
}
result = append(result, temp[:])
}
return result
}
func CommonAncestor(root, p, q *TreeNode) *TreeNode{
var storeMap map[*TreeNode]*TreeNode = make(map[*TreeNode]*TreeNode)
storeMap[root] = nil
var queue []*TreeNode = []*TreeNode{root}
var currentNode *TreeNode
for len(queue) != 0{
currentNode, queue = queue[0], queue[1:]
fmt.Println("current node: ", currentNode)
if currentNode.Left != nil{
storeMap[currentNode.Left] = currentNode
queue = append(queue, currentNode.Left)
}
if currentNode.Right != nil{
queue = append(queue, currentNode.Right)
storeMap[currentNode.Right] = currentNode
}
}
fmt.Println(storeMap)
var ancestors map[*TreeNode]int = make(map[*TreeNode]int)
for p != nil{
fmt.Println(p)
ancestors[p] = 1
p = storeMap[p]
}
fmt.Println(ancestors)
for q != nil{
if _, ok := ancestors[q]; ok{
return q
}
q = storeMap[q]
}
return nil
}
func BSTLowestCommonAncestor(root, p, q *TreeNode) *TreeNode{
var currentNode *TreeNode = root
for currentNode != nil{
if (p.Val <= currentNode.Val && q.Val >= currentNode.Val) || (p.Val >= currentNode.Val && q.Val <= currentNode.Val){
return currentNode
} else{
if (p.Val < currentNode.Val && q.Val < currentNode.Val){
currentNode = currentNode.Left
} else{
currentNode = currentNode.Right
}
}
}
return nil
}
func _SmallestFromLeaf(root *TreeNode, s string){
curVal := string(root.Val+97)+s
if root.Left == nil && root.Right == nil{
if curVal < SmallestFromLeaf{
SmallestFromLeaf = curVal
}
fmt.Println("The value: ", curVal)
} else{
if root.Left != nil{
_SmallestFromLeaf(root.Left, curVal)
}
if root.Right != nil{
_SmallestFromLeaf(root.Right, curVal)
}
}
}
func SmallestFromLeaf(root *TreeNode) string{
_SmallestFromLeaf(root, "")
return smallestFromLeaf
}
func main(){
root := &TreeNode{
Val: 3,
}
fmt.Println(root.Val)
InsertNode(root, &TreeNode{Val:9,})
InsertNode(root, &TreeNode{Val:20,})
InsertNode(root, &TreeNode{Val:15,})
InsertNode(root, &TreeNode{Val:7,})
temp := root
for temp != nil{
fmt.Println(temp.Val)
temp = temp.Right
}
// fmt.Println(VerticalTraversal(root))
fmt.Println(CommonAncestor(root, root.Right.Right, root.Right))
}