@@ -11,6 +11,7 @@ package avltree
1111
1212import (
1313 "fmt"
14+
1415 "github.com/ugurcsen/gods-generic/trees"
1516 "github.com/ugurcsen/gods-generic/utils"
1617)
@@ -19,14 +20,14 @@ import (
1920var _ 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 {
0 commit comments