Skip to content

Commit 95753c5

Browse files
authored
Merge pull request #2 from li1234yun/trees_any_type_value
Modify trees value generic type to any for allowing receive map/slice etc
2 parents 070cec7 + facb43e commit 95753c5

File tree

10 files changed

+43
-38
lines changed

10 files changed

+43
-38
lines changed

examples/redblacktreeextended/redblacktreeextended.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ package redblacktreeextended
66

77
import (
88
"fmt"
9+
910
rbt "github.com/ugurcsen/gods-generic/trees/redblacktree"
1011
)
1112

1213
// RedBlackTreeExtended to demonstrate how to extend a RedBlackTree to include new functions
13-
type RedBlackTreeExtended[K, T comparable] struct {
14+
type RedBlackTreeExtended[K comparable, T any] struct {
1415
*rbt.Tree[K, T]
1516
}
1617

trees/avltree/avltree.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ package avltree
1111

1212
import (
1313
"fmt"
14+
1415
"github.com/ugurcsen/gods-generic/trees"
1516
"github.com/ugurcsen/gods-generic/utils"
1617
)
@@ -19,14 +20,14 @@ import (
1920
var _ trees.Tree[int] = new(Tree[int, int])
2021

2122
// Tree holds elements of the AVL tree.
22-
type Tree[K, T comparable] struct {
23+
type Tree[K comparable, T any] struct {
2324
Root *Node[K, T] // Root node
2425
Comparator utils.Comparator[K] // Key comparator
2526
size int // Total number of keys in the tree
2627
}
2728

2829
// Node is a single element within the tree
29-
type Node[K, T comparable] struct {
30+
type Node[K comparable, T any] struct {
3031
Key K
3132
Value T
3233
Parent *Node[K, T] // Parent node
@@ -35,17 +36,17 @@ type Node[K, T comparable] struct {
3536
}
3637

3738
// NewWith instantiates an AVL tree with the custom comparator.
38-
func NewWith[K, T comparable](comparator utils.Comparator[K]) *Tree[K, T] {
39+
func NewWith[K comparable, T any](comparator utils.Comparator[K]) *Tree[K, T] {
3940
return &Tree[K, T]{Comparator: comparator}
4041
}
4142

4243
// NewWithNumberComparator instantiates an AVL tree with the IntComparator, i.e. keys are of type int.
43-
func NewWithNumberComparator[T comparable]() *Tree[int, T] {
44+
func NewWithNumberComparator[T any]() *Tree[int, T] {
4445
return &Tree[int, T]{Comparator: utils.NumberComparator[int]}
4546
}
4647

4748
// NewWithStringComparator instantiates an AVL tree with the StringComparator, i.e. keys are of type string.
48-
func NewWithStringComparator[T comparable]() *Tree[string, T] {
49+
func NewWithStringComparator[T any]() *Tree[string, T] {
4950
return &Tree[string, T]{Comparator: utils.StringComparator}
5051
}
5152

@@ -291,7 +292,7 @@ func (t *Tree[K, T]) remove(key K, qp **Node[K, T]) bool {
291292
return false
292293
}
293294

294-
func removeMin[K, T comparable](qp **Node[K, T], minKey *K, minVal *T) bool {
295+
func removeMin[K comparable, T any](qp **Node[K, T], minKey *K, minVal *T) bool {
295296
q := *qp
296297
if q.Children[0] == nil {
297298
*minKey = q.Key
@@ -309,7 +310,7 @@ func removeMin[K, T comparable](qp **Node[K, T], minKey *K, minVal *T) bool {
309310
return false
310311
}
311312

312-
func putFix[K, T comparable](c int8, t **Node[K, T]) bool {
313+
func putFix[K comparable, T any](c int8, t **Node[K, T]) bool {
313314
s := *t
314315
if s.b == 0 {
315316
s.b = c
@@ -330,7 +331,7 @@ func putFix[K, T comparable](c int8, t **Node[K, T]) bool {
330331
return false
331332
}
332333

333-
func removeFix[K, T comparable](c int8, t **Node[K, T]) bool {
334+
func removeFix[K comparable, T any](c int8, t **Node[K, T]) bool {
334335
s := *t
335336
if s.b == 0 {
336337
s.b = c
@@ -359,14 +360,14 @@ func removeFix[K, T comparable](c int8, t **Node[K, T]) bool {
359360
return true
360361
}
361362

362-
func singlerot[K, T comparable](c int8, s *Node[K, T]) *Node[K, T] {
363+
func singlerot[K comparable, T any](c int8, s *Node[K, T]) *Node[K, T] {
363364
s.b = 0
364365
s = rotate(c, s)
365366
s.b = 0
366367
return s
367368
}
368369

369-
func doublerot[K, T comparable](c int8, s *Node[K, T]) *Node[K, T] {
370+
func doublerot[K comparable, T any](c int8, s *Node[K, T]) *Node[K, T] {
370371
a := (c + 1) / 2
371372
r := s.Children[a]
372373
s.Children[a] = rotate(-c, s.Children[a])
@@ -388,7 +389,7 @@ func doublerot[K, T comparable](c int8, s *Node[K, T]) *Node[K, T] {
388389
return p
389390
}
390391

391-
func rotate[K, T comparable](c int8, s *Node[K, T]) *Node[K, T] {
392+
func rotate[K comparable, T any](c int8, s *Node[K, T]) *Node[K, T] {
392393
a := (c + 1) / 2
393394
r := s.Children[a]
394395
s.Children[a] = r.Children[a^1]
@@ -446,7 +447,7 @@ func (n *Node[K, T]) walk1(a int) *Node[K, T] {
446447
return p
447448
}
448449

449-
func output[K, T comparable](node *Node[K, T], prefix string, isTail bool, str *string) {
450+
func output[K comparable, T any](node *Node[K, T], prefix string, isTail bool, str *string) {
450451
if node.Children[1] != nil {
451452
newPrefix := prefix
452453
if isTail {

trees/avltree/avltree_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ package avltree
66
import (
77
"encoding/json"
88
"fmt"
9-
"github.com/ugurcsen/gods-generic/utils"
109
"strings"
1110
"testing"
11+
12+
"github.com/ugurcsen/gods-generic/utils"
1213
)
1314

1415
func TestAVLTreeGet(t *testing.T) {
@@ -657,8 +658,7 @@ func TestAVLTreeIteratorPrevTo(t *testing.T) {
657658
}
658659

659660
func TestAVLTreeSerialization(t *testing.T) {
660-
tree := NewWith[string, string](utils.StringComparator)
661-
tree = NewWithStringComparator[string]()
661+
tree := NewWithStringComparator[string]()
662662
tree.Put("c", "3")
663663
tree.Put("b", "2")
664664
tree.Put("a", "1")

trees/avltree/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "github.com/ugurcsen/gods-generic/containers"
1010
var _ containers.ReverseIteratorWithKey[int, int] = (*Iterator[int, int])(nil)
1111

1212
// Iterator holding the iterator's state
13-
type Iterator[K, T comparable] struct {
13+
type Iterator[K comparable, T any] struct {
1414
tree *Tree[K, T]
1515
node *Node[K, T]
1616
position position

trees/binaryheap/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (iterator *Iterator[T]) Value() T {
5050
if end > iterator.heap.Size() {
5151
end = iterator.heap.Size()
5252
}
53-
tmpHeap := NewWith[T](iterator.heap.Comparator)
53+
tmpHeap := NewWith(iterator.heap.Comparator)
5454
for n := start; n < end; n++ {
5555
value, _ := iterator.heap.list.Get(n)
5656
tmpHeap.Push(value)

trees/btree/btree.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,50 +19,51 @@ package btree
1919
import (
2020
"bytes"
2121
"fmt"
22+
"strings"
23+
2224
"github.com/ugurcsen/gods-generic/trees"
2325
"github.com/ugurcsen/gods-generic/utils"
24-
"strings"
2526
)
2627

2728
// Assert Tree implementation
2829
var _ trees.Tree[int] = (*Tree[int, int])(nil)
2930

3031
// Tree holds elements of the B-tree
31-
type Tree[K, T comparable] struct {
32+
type Tree[K comparable, T any] struct {
3233
Root *Node[K, T] // Root node
3334
Comparator utils.Comparator[K] // Key comparator
3435
size int // Total number of keys in the tree
3536
m int // order (maximum number of children)
3637
}
3738

3839
// Node is a single element within the tree
39-
type Node[K, T comparable] struct {
40+
type Node[K comparable, T any] struct {
4041
Parent *Node[K, T]
4142
Entries []*Entry[K, T] // Contained keys in node
4243
Children []*Node[K, T] // Children nodes
4344
}
4445

4546
// Entry represents the key-value pair contained within nodes
46-
type Entry[K, T comparable] struct {
47+
type Entry[K comparable, T any] struct {
4748
Key K
4849
Value T
4950
}
5051

5152
// NewWith instantiates a B-tree with the order (maximum number of children) and a custom key comparator.
52-
func NewWith[K, T comparable](order int, comparator utils.Comparator[K]) *Tree[K, T] {
53+
func NewWith[K comparable, T any](order int, comparator utils.Comparator[K]) *Tree[K, T] {
5354
if order < 3 {
5455
panic("Invalid order, should be at least 3")
5556
}
5657
return &Tree[K, T]{m: order, Comparator: comparator}
5758
}
5859

5960
// NewWithNumberComparator instantiates a B-tree with the order (maximum number of children) and the IntComparator, i.e. keys are of type int.
60-
func NewWithNumberComparator[T comparable](order int) *Tree[int, T] {
61+
func NewWithNumberComparator[T any](order int) *Tree[int, T] {
6162
return NewWith[int, T](order, utils.NumberComparator[int])
6263
}
6364

6465
// NewWithStringComparator instantiates a B-tree with the order (maximum number of children) and the StringComparator, i.e. keys are of type string.
65-
func NewWithStringComparator[T comparable](order int) *Tree[string, T] {
66+
func NewWithStringComparator[T any](order int) *Tree[string, T] {
6667
return NewWith[string, T](order, utils.StringComparator)
6768
}
6869

@@ -415,7 +416,7 @@ func (tree *Tree[K, T]) splitRoot() {
415416
tree.Root = newRoot
416417
}
417418

418-
func setParent[K, T comparable](nodes []*Node[K, T], parent *Node[K, T]) {
419+
func setParent[K comparable, T any](nodes []*Node[K, T], parent *Node[K, T]) {
419420
for _, node := range nodes {
420421
node.Parent = parent
421422
}

trees/btree/btree_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@ package btree
77
import (
88
"encoding/json"
99
"fmt"
10-
"github.com/ugurcsen/gods-generic/utils"
1110
"strings"
1211
"testing"
12+
13+
"github.com/ugurcsen/gods-generic/utils"
1314
)
1415

1516
func TestBTreeGet1(t *testing.T) {
@@ -1044,13 +1045,13 @@ func TestBTreeSearch(t *testing.T) {
10441045
}
10451046
}
10461047

1047-
func assertValidTree[K, T comparable](t *testing.T, tree *Tree[K, T], expectedSize int) {
1048+
func assertValidTree[K comparable, T any](t *testing.T, tree *Tree[K, T], expectedSize int) {
10481049
if actualValue, expectedValue := tree.size, expectedSize; actualValue != expectedValue {
10491050
t.Errorf("Got %v expected %v for tree size", actualValue, expectedValue)
10501051
}
10511052
}
10521053

1053-
func assertValidTreeNode[K, T comparable](t *testing.T, node *Node[K, T], expectedEntries int, expectedChildren int, keys []K, hasParent bool) {
1054+
func assertValidTreeNode[K comparable, T any](t *testing.T, node *Node[K, T], expectedEntries int, expectedChildren int, keys []K, hasParent bool) {
10541055
if actualValue, expectedValue := node.Parent != nil, hasParent; actualValue != expectedValue {
10551056
t.Errorf("Got %v expected %v for hasParent", actualValue, expectedValue)
10561057
}

trees/btree/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "github.com/ugurcsen/gods-generic/containers"
1010
var _ containers.ReverseIteratorWithKey[int, int] = (*Iterator[int, int])(nil)
1111

1212
// Iterator holding the iterator's state
13-
type Iterator[K, T comparable] struct {
13+
type Iterator[K comparable, T any] struct {
1414
tree *Tree[K, T]
1515
node *Node[K, T]
1616
entry *Entry[K, T]

trees/redblacktree/iterator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import "github.com/ugurcsen/gods-generic/containers"
1010
var _ containers.ReverseIteratorWithKey[int, int] = (*Iterator[int, int])(nil)
1111

1212
// Iterator holding the iterator's state
13-
type Iterator[K, T comparable] struct {
13+
type Iterator[K comparable, T any] struct {
1414
tree *Tree[K, T]
1515
node *Node[K, T]
1616
position position

trees/redblacktree/redblacktree.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package redblacktree
1313

1414
import (
1515
"fmt"
16+
1617
"github.com/ugurcsen/gods-generic/trees"
1718
"github.com/ugurcsen/gods-generic/utils"
1819
)
@@ -27,14 +28,14 @@ const (
2728
)
2829

2930
// Tree holds elements of the red-black tree
30-
type Tree[K, T comparable] struct {
31+
type Tree[K comparable, T any] struct {
3132
Root *Node[K, T]
3233
size int
3334
Comparator utils.Comparator[K]
3435
}
3536

3637
// Node is a single element within the tree
37-
type Node[K, T comparable] struct {
38+
type Node[K comparable, T any] struct {
3839
Key K
3940
Value T
4041
color color
@@ -44,17 +45,17 @@ type Node[K, T comparable] struct {
4445
}
4546

4647
// NewWith instantiates a red-black tree with the custom comparator.
47-
func NewWith[K, T comparable](comparator utils.Comparator[K]) *Tree[K, T] {
48+
func NewWith[K comparable, T any](comparator utils.Comparator[K]) *Tree[K, T] {
4849
return &Tree[K, T]{Comparator: comparator}
4950
}
5051

5152
// NewWithNumberComparator instantiates a red-black tree with the IntComparator, i.e. keys are of type int.
52-
func NewWithNumberComparator[T comparable]() *Tree[int, T] {
53+
func NewWithNumberComparator[T any]() *Tree[int, T] {
5354
return &Tree[int, T]{Comparator: utils.NumberComparator[int]}
5455
}
5556

5657
// NewWithStringComparator instantiates a red-black tree with the StringComparator, i.e. keys are of type string.
57-
func NewWithStringComparator[T comparable]() *Tree[string, T] {
58+
func NewWithStringComparator[T any]() *Tree[string, T] {
5859
return &Tree[string, T]{Comparator: utils.StringComparator}
5960
}
6061

@@ -296,7 +297,7 @@ func (node *Node[K, T]) String() string {
296297
return fmt.Sprintf("%v", node.Key)
297298
}
298299

299-
func output[K, T comparable](node *Node[K, T], prefix string, isTail bool, str *string) {
300+
func output[K comparable, T any](node *Node[K, T], prefix string, isTail bool, str *string) {
300301
if node.Right != nil {
301302
newPrefix := prefix
302303
if isTail {
@@ -541,7 +542,7 @@ func (tree *Tree[K, T]) deleteCase6(node *Node[K, T]) {
541542
}
542543
}
543544

544-
func nodeColor[K, T comparable](node *Node[K, T]) color {
545+
func nodeColor[K comparable, T any](node *Node[K, T]) color {
545546
if node == nil {
546547
return black
547548
}

0 commit comments

Comments
 (0)