Skip to content

Commit 7a21742

Browse files
committed
Initial commit
0 parents  commit 7a21742

File tree

8 files changed

+698
-0
lines changed

8 files changed

+698
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Alomerry Wu
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

algorithm/bst.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package algorithm
2+
3+
type Priority interface {
4+
Less(i, j interface{}) bool
5+
Equal(i, j interface{}) bool
6+
Empty() bool
7+
}
8+
9+
type BSTree[T Priority] struct {
10+
data T
11+
Left *BSTree[T]
12+
Right *BSTree[T]
13+
}
14+
15+
// NewBSTNode Attention: data need implement Priority
16+
func NewBSTNode[T Priority](data T) *BSTree[T] {
17+
return &BSTree[T]{
18+
data: data,
19+
}
20+
}
21+
22+
func (p *BSTree[T]) Insert(newNode *BSTree[T]) *BSTree[T] {
23+
if newNode == nil {
24+
return p
25+
}
26+
if p == nil {
27+
return NewBSTNode(newNode.data)
28+
}
29+
30+
if p.data.Empty() {
31+
panic("can't less nil data")
32+
}
33+
34+
if p.data.Less(p.data, newNode.data) {
35+
if p.Right == nil {
36+
p.Right = newNode
37+
} else {
38+
p.Right.Insert(newNode)
39+
}
40+
} else {
41+
if p.Left == nil {
42+
p.Left = newNode
43+
} else {
44+
p.Left.Insert(newNode)
45+
}
46+
}
47+
return p
48+
}
49+
50+
func (p *BSTree[T]) Remove(newNode *BSTree[T]) *BSTree[T] {
51+
if p.Left != nil {
52+
p.Left = p.Left.Remove(newNode)
53+
}
54+
55+
if p.Right != nil {
56+
p.Right = p.Right.Remove(newNode)
57+
}
58+
59+
// ATTENTION golang 无法修改 receiver 本身,需要 return
60+
if p.data.Equal(p.data, newNode.data) {
61+
if p.Left == nil && p.Right == nil {
62+
return nil
63+
}
64+
65+
if p.Left == nil || p.Right == nil {
66+
if p.Left != nil {
67+
return p.Left
68+
} else {
69+
return p.Right
70+
}
71+
}
72+
73+
if p.Left != nil && p.Right != nil {
74+
p.Right.Insert(p.Left)
75+
return p.Right
76+
}
77+
}
78+
79+
return p
80+
}
81+
82+
func (p *BSTree[T]) Has(newNode *BSTree[T]) bool {
83+
if p == nil {
84+
return false
85+
}
86+
87+
if p.data.Empty() {
88+
panic("can't less nil data")
89+
}
90+
91+
if p.Left != nil {
92+
found := p.Left.Has(newNode)
93+
if found {
94+
return true
95+
}
96+
}
97+
98+
if p.Right != nil {
99+
found := p.Right.Has(newNode)
100+
if found {
101+
return true
102+
}
103+
}
104+
105+
if p.data.Equal(p.data, newNode.data) {
106+
return true
107+
}
108+
return false
109+
}
110+
111+
func (p *BSTree[T]) Minimum() *BSTree[T] {
112+
if p.Left == nil {
113+
return p
114+
} else {
115+
return p.Left.Minimum()
116+
}
117+
}
118+
119+
func (p *BSTree[T]) Maximum() *BSTree[T] {
120+
if p.Right == nil {
121+
return p
122+
} else {
123+
return p.Right.Maximum()
124+
}
125+
}
126+
127+
func (p *BSTree[T]) Data() T {
128+
return p.data
129+
}
130+
131+
func (p *BSTree[T]) ToArray() []T {
132+
result := make([]T, 0)
133+
p.PreTravel(&result)
134+
return result
135+
}
136+
137+
func (p *BSTree[T]) PreTravel(list *[]T) {
138+
if p == nil {
139+
return
140+
}
141+
142+
if p.Left != nil {
143+
p.Left.PreTravel(list)
144+
}
145+
146+
*list = append(*list, p.data)
147+
148+
if p.Right != nil {
149+
p.Right.PreTravel(list)
150+
}
151+
}
152+
153+
func (p *BSTree[T]) Merge(b *BSTree[T]) *BSTree[T] {
154+
if p == nil {
155+
return b
156+
} else {
157+
list := b.ToArray()
158+
for i := range list {
159+
p.Insert(NewBSTNode(list[i]))
160+
}
161+
}
162+
return p
163+
}

0 commit comments

Comments
 (0)