-
-
Notifications
You must be signed in to change notification settings - Fork 86
/
bst.go
171 lines (140 loc) · 3.19 KB
/
bst.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
// Copyright (c) 2021-2024 Onur Cinar.
// The source code is provided under GNU AGPLv3 License.
// https://github.com/cinar/indicator
package helper
// BstNode represents the binary search tree node.
type BstNode[T Number] struct {
value T
left *BstNode[T]
right *BstNode[T]
}
// Bst represents the binary search tree.
type Bst[T Number] struct {
root *BstNode[T]
}
// NewBst creates a new binary search tree.
func NewBst[T Number]() *Bst[T] {
return &Bst[T]{}
}
// Insert adds a new value to the binary search tree.
func (b *Bst[T]) Insert(value T) {
node := &BstNode[T]{
value: value,
}
if b.root == nil {
b.root = node
return
}
cur := b.root
for {
if node.value <= cur.value {
if cur.left == nil {
cur.left = node
return
}
cur = cur.left
} else {
if cur.right == nil {
cur.right = node
return
}
cur = cur.right
}
}
}
// Contains checks whether the given value exists in the binary search tree.
func (b *Bst[T]) Contains(value T) bool {
node, _ := b.searchNode(value)
return node != nil
}
// Remove removes the specified value from the binary search tree
// and rebalances the tree.
func (b *Bst[T]) Remove(value T) bool {
node, parent := b.searchNode(value)
if node == nil {
return false
}
b.removeNode(node, parent)
return true
}
// Min function returns the minimum value in the binary search tree.
func (b *Bst[T]) Min() T {
if b.root == nil {
return T(0)
}
node, _ := minNode(b.root)
return node.value
}
// Max function returns the maximum value in the binary search tree.
func (b *Bst[T]) Max() T {
if b.root == nil {
return T(0)
}
node, _ := maxNode(b.root)
return node.value
}
// searchNode searches for the given value in the binary search tree and returns
// the first matching node and its parent.
func (b *Bst[T]) searchNode(value T) (*BstNode[T], *BstNode[T]) {
var parent *BstNode[T]
node := b.root
for node != nil {
diff := value - node.value
if diff == 0 {
break
}
parent = node
if diff < 0 {
node = node.left
} else {
node = node.right
}
}
return node, parent
}
// removeNode removes the specified node from the binary search tree
// and rebalances the tree.
func (b *Bst[T]) removeNode(node, parent *BstNode[T]) {
if node.left != nil && node.right != nil {
min, minParent := minNode(node.right)
if minParent == nil {
minParent = node
}
b.removeNode(min, minParent)
node.value = min.value
} else {
var child *BstNode[T]
if node.left != nil {
child = node.left
} else {
child = node.right
}
if node == b.root {
b.root = child
} else if parent.left == node {
parent.left = child
} else {
parent.right = child
}
}
}
// minNode functions returns the node with the minimum value and its parent node.
func minNode[T Number](root *BstNode[T]) (*BstNode[T], *BstNode[T]) {
var parent *BstNode[T]
node := root
for node.left != nil {
parent = node
node = node.left
}
return node, parent
}
// maxNode functions returns the node with the maximum value and its parent node.
func maxNode[T Number](root *BstNode[T]) (*BstNode[T], *BstNode[T]) {
var parent *BstNode[T]
node := root
for node.right != nil {
parent = node
node = node.right
}
return node, parent
}