-
Notifications
You must be signed in to change notification settings - Fork 2
/
binary_tree.go
237 lines (194 loc) · 4.44 KB
/
binary_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
package main
import "fmt"
type node struct {
value int
left *node
right *node
}
type BinaryTree struct {
head *node
}
func (b *BinaryTree) Add(value int) {
focus := b.head
if focus == nil {
b.head = &node{value: value}
return
}
for {
if focus.value > value {
if focus.left == nil {
focus.left = &node{value: value}
return
}
focus = focus.left
} else {
if focus.right == nil {
focus.right = &node{value: value}
return
}
focus = focus.right
}
}
}
// The minimum of the right child or the first parent on the right (larger value parent)
func (b *BinaryTree) Successor(value int) int {
var parents []*node
focus := b.head
for focus != nil {
if focus.value == value {
if focus.right != nil {
return Minimum(focus.right)
}
break
}
parents = append(parents, focus)
if focus.value > value {
focus = focus.left
} else {
focus = focus.right
}
}
for i := len(parents) - 1; i >= 0; i-- {
if parents[i].value >= value {
return parents[i].value
}
}
return 0 // in the real world this would return an error
}
// Maximum of left child or the first left parent (smaller value)
func (b *BinaryTree) Predecessor(value int) int {
var parents []*node
focus := b.head
for focus != nil {
if focus.value == value {
if focus.left != nil {
return Maximum(focus.left)
}
break
}
parents = append(parents, focus)
if focus.value > value {
focus = focus.left
} else {
focus = focus.right
}
}
for i := len(parents) - 1; i >= 0; i-- {
if parents[i].value <= value {
return parents[i].value
}
}
return 0
}
func (b *BinaryTree) Contains(value int) bool {
focus := b.head
for focus != nil {
if focus.value == value {
return true
}
if focus.value > value {
focus = focus.left
continue
}
focus = focus.right
}
return false
}
func ContainsRecursive(focus *node, value int) bool {
if focus == nil {
return false
}
if focus.value == value {
return true
}
if focus.value > value {
return ContainsRecursive(focus.left, value)
}
return ContainsRecursive(focus.right, value)
}
func InOrderWalk(focus *node) string {
var result string
if focus == nil {
return result
}
result += InOrderWalk(focus.left)
result += fmt.Sprintf("[%d] ", focus.value)
result += InOrderWalk(focus.right)
return result
}
func Minimum(focus *node) int {
var result int
for focus != nil {
result = focus.value
focus = focus.left
}
return result
}
func Maximum(focus *node) int {
var result int
for focus != nil {
result = focus.value
focus = focus.right
}
return result
}
func BreadthFrist(focus *node) {
queue := []*node{focus}
i := 0
for i < len(queue) {
if queue[i].left != nil {
queue = append(queue, queue[i].left)
}
if queue[i].right != nil {
queue = append(queue, queue[i].right)
}
i++
}
for _, item := range queue {
fmt.Printf("[%d]", item.value)
}
fmt.Println()
}
func DepthFirstIterative(focus *node) {
var stack []*node
for focus != nil || len(stack) != 0 {
if focus == nil {
fmt.Printf("[%d]", stack[len(stack)-1].value)
focus = stack[len(stack)-1].right
stack = stack[:len(stack)-1]
} else {
stack = append(stack, focus)
focus = focus.left
}
}
fmt.Println()
}
func main() {
var tree BinaryTree
var input = []int{20, 8, 25, 34, 9, 5, 50, 40, 7, 15, 8, 51, 47}
fmt.Println("Adding:", input)
for _, value := range input {
tree.Add(value)
}
fmt.Println("sorted:", InOrderWalk(tree.head))
fmt.Println("Minimum()", Minimum(tree.head))
fmt.Println("Maximum()", Maximum(tree.head))
fmt.Println("Contains(15)", tree.Contains(15))
fmt.Println("Contains(16)", tree.Contains(16))
fmt.Println("Contains(51)", tree.Contains(51))
fmt.Println("Contains(7)", tree.Contains(7))
fmt.Println("ContainsRecursive(7)", ContainsRecursive(tree.head, 7))
fmt.Println("ContainsRecursive(16)", ContainsRecursive(tree.head, 16))
fmt.Println("ContainsRecursive(51)", ContainsRecursive(tree.head, 51))
fmt.Println("ContainsRecursive(52)", ContainsRecursive(tree.head, 52))
fmt.Println("Successor(15)", tree.Successor(15))
fmt.Println("Successor(8)", tree.Successor(8))
fmt.Println("Successor(9)", tree.Successor(9))
fmt.Println("Predecessor(9)", tree.Predecessor(9))
fmt.Println("Predecessor(15)", tree.Predecessor(15))
fmt.Println("Predecessor(50)", tree.Predecessor(50))
fmt.Println("BreadthFirst: --->")
BreadthFrist(tree.head)
fmt.Println("DepthFirstIteraative: --->")
DepthFirstIterative(tree.head)
}