-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
bstree.go
228 lines (197 loc) · 5.04 KB
/
bstree.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
// Binary search tree.
//
// For more details check out those links below here:
// Wikipedia article: https://en.wikipedia.org/wiki/Binary_search_tree
// see bstree.go
package tree
import "github.com/TheAlgorithms/Go/constraints"
// Verify Interface Compliance
var _ Node[int] = &BSNode[int]{}
// BSNode represents a single node in the BinarySearch.
type BSNode[T constraints.Ordered] struct {
key T
parent *BSNode[T]
left *BSNode[T]
right *BSNode[T]
}
func (n *BSNode[T]) Key() T {
return n.key
}
func (n *BSNode[T]) Parent() Node[T] {
return n.parent
}
func (n *BSNode[T]) Left() Node[T] {
return n.left
}
func (n *BSNode[T]) Right() Node[T] {
return n.right
}
// BinarySearch represents a Binary-Search tree.
// By default, _NIL = nil.
type BinarySearch[T constraints.Ordered] struct {
Root *BSNode[T]
_NIL *BSNode[T] // a sentinel value for nil
}
// NewBinarySearch creates a novel Binary-Search tree
func NewBinarySearch[T constraints.Ordered]() *BinarySearch[T] {
return &BinarySearch[T]{
Root: nil,
_NIL: nil,
}
}
// Empty determines the Binary-Search tree is empty
func (t *BinarySearch[T]) Empty() bool {
return t.Root == t._NIL
}
// Push a chain of Node's into the BinarySearch
func (t *BinarySearch[T]) Push(keys ...T) {
for _, key := range keys {
t.pushHelper(t.Root, key)
}
}
// Delete removes the node of val
func (t *BinarySearch[T]) Delete(val T) bool {
node, ok := t.Get(val)
if !ok {
return false
}
t.deleteHelper(node.(*BSNode[T]))
return true
}
// Get a Node from the Binary-Search Tree
func (t *BinarySearch[T]) Get(key T) (Node[T], bool) {
return searchTreeHelper[T](t.Root, t._NIL, key)
}
// Has Determines the tree has the node of Key
func (t *BinarySearch[T]) Has(key T) bool {
_, ok := searchTreeHelper[T](t.Root, t._NIL, key)
return ok
}
// PreOrder Traverses the tree in the following order Root --> Left --> Right
func (t *BinarySearch[T]) PreOrder() []T {
traversal := make([]T, 0)
preOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// InOrder Traverses the tree in the following order Left --> Root --> Right
func (t *BinarySearch[T]) InOrder() []T {
return inOrderHelper[T](t.Root, t._NIL)
}
// PostOrder traverses the tree in the following order Left --> Right --> Root
func (t *BinarySearch[T]) PostOrder() []T {
traversal := make([]T, 0)
postOrderRecursive[T](t.Root, t._NIL, &traversal)
return traversal
}
// LevelOrder returns the level order traversal of the tree
func (t *BinarySearch[T]) LevelOrder() []T {
traversal := make([]T, 0)
levelOrderHelper[T](t.Root, t._NIL, &traversal)
return traversal
}
// AccessNodesByLayer accesses nodes layer by layer (2-D array), instead of printing the results as 1-D array.
func (t *BinarySearch[T]) AccessNodesByLayer() [][]T {
return accessNodeByLayerHelper[T](t.Root, t._NIL)
}
// Depth returns the calculated depth of a binary search tree
func (t *BinarySearch[T]) Depth() int {
return calculateDepth[T](t.Root, t._NIL, 0)
}
// Max returns the Max value of the tree
func (t *BinarySearch[T]) Max() (T, bool) {
ret := maximum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Min returns the Min value of the tree
func (t *BinarySearch[T]) Min() (T, bool) {
ret := minimum[T](t.Root, t._NIL)
if ret == t._NIL {
var dft T
return dft, false
}
return ret.Key(), true
}
// Predecessor returns the Predecessor of the node of Key
// if there is no predecessor, return default value of type T and false
// otherwise return the Key of predecessor and true
func (t *BinarySearch[T]) Predecessor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return predecessorHelper[T](node, t._NIL)
}
// Successor returns the Successor of the node of Key
// if there is no successor, return default value of type T and false
// otherwise return the Key of successor and true
func (t *BinarySearch[T]) Successor(key T) (T, bool) {
node, ok := searchTreeHelper[T](t.Root, t._NIL, key)
if !ok {
var dft T
return dft, ok
}
return successorHelper[T](node, t._NIL)
}
func (t *BinarySearch[T]) pushHelper(x *BSNode[T], val T) {
y := t._NIL
for x != t._NIL {
y = x
switch {
case val < x.Key():
x = x.left
case val > x.Key():
x = x.right
default:
return
}
}
z := &BSNode[T]{
key: val,
left: t._NIL,
right: t._NIL,
parent: y,
}
if y == t._NIL {
t.Root = z
} else if val < y.key {
y.left = z
} else {
y.right = z
}
}
func (t *BinarySearch[T]) deleteHelper(z *BSNode[T]) {
switch {
case z.left == t._NIL:
t.transplant(z, z.right)
case z.right == t._NIL:
t.transplant(z, z.left)
default:
y := minimum[T](z.right, t._NIL).(*BSNode[T])
if y.parent != z {
t.transplant(y, y.right)
y.right = z.right
y.right.parent = y
}
t.transplant(z, y)
y.left = z.left
y.left.parent = y
}
}
func (t *BinarySearch[T]) transplant(u, v *BSNode[T]) {
switch {
case u.parent == t._NIL:
t.Root = v
case u == u.parent.left:
u.parent.left = v
default:
u.parent.right = v
}
if v != t._NIL {
v.parent = u.parent
}
}