github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/structure/trees/avltree/avltree_test.go (about)

     1  package avltree
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
     7  	"reflect"
     8  	"strings"
     9  	"testing"
    10  )
    11  
    12  func TestAVLTreeGet(t *testing.T) {
    13  	tree := NewWithIntComparator[string]()
    14  
    15  	if actualValue := tree.Size(); actualValue != 0 {
    16  		t.Errorf("Got %v expected %v", actualValue, 0)
    17  	}
    18  
    19  	if actualValue := tree.GetNode(2).Size(); actualValue != 0 {
    20  		t.Errorf("Got %v expected %v", actualValue, 0)
    21  	}
    22  
    23  	tree.Put(1, "x") // 1->x
    24  	tree.Put(2, "b") // 1->x, 2->b (in order)
    25  	tree.Put(1, "a") // 1->a, 2->b (in order, replacement)
    26  	tree.Put(3, "c") // 1->a, 2->b, 3->c (in order)
    27  	tree.Put(4, "d") // 1->a, 2->b, 3->c, 4->d (in order)
    28  	tree.Put(5, "e") // 1->a, 2->b, 3->c, 4->d, 5->e (in order)
    29  	tree.Put(6, "f") // 1->a, 2->b, 3->c, 4->d, 5->e, 6->f (in order)
    30  	//
    31  	//  AVLTree
    32  	//  │       ┌── 6
    33  	//  │   ┌── 5
    34  	//  └── 4
    35  	//      │   ┌── 3
    36  	//      └── 2
    37  	//          └── 1
    38  
    39  	if actualValue := tree.Size(); actualValue != 6 {
    40  		t.Errorf("Got %v expected %v", actualValue, 6)
    41  	}
    42  
    43  	if actualValue := tree.GetNode(2).Size(); actualValue != 3 {
    44  		t.Errorf("Got %v expected %v", actualValue, 3)
    45  	}
    46  
    47  	if actualValue := tree.GetNode(4).Size(); actualValue != 6 {
    48  		t.Errorf("Got %v expected %v", actualValue, 6)
    49  	}
    50  
    51  	if actualValue := tree.GetNode(7).Size(); actualValue != 0 {
    52  		t.Errorf("Got %v expected %v", actualValue, 0)
    53  	}
    54  }
    55  
    56  func TestAVLTreePut(t *testing.T) {
    57  	tree := NewWithIntComparator[string]()
    58  	tree.Put(5, "e")
    59  	tree.Put(6, "f")
    60  	tree.Put(7, "g")
    61  	tree.Put(3, "c")
    62  	tree.Put(4, "d")
    63  	tree.Put(1, "x")
    64  	tree.Put(2, "b")
    65  	tree.Put(1, "a") //overwrite
    66  
    67  	if actualValue := tree.Size(); actualValue != 7 {
    68  		t.Errorf("Got %v expected %v", actualValue, 7)
    69  	}
    70  	if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4, 5, 6, 7}; !reflect.DeepEqual(actualValue, expectedValue) {
    71  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    72  	}
    73  	if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d", "e", "f", "g"}; !reflect.DeepEqual(actualValue, expectedValue) {
    74  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
    75  	}
    76  
    77  	tests1 := [][]interface{}{
    78  		{1, "a", true},
    79  		{2, "b", true},
    80  		{3, "c", true},
    81  		{4, "d", true},
    82  		{5, "e", true},
    83  		{6, "f", true},
    84  		{7, "g", true},
    85  		{8, "", false},
    86  	}
    87  
    88  	for _, test := range tests1 {
    89  		// retrievals
    90  		actualValue, actualFound := tree.Get(test[0].(int))
    91  		if actualValue != test[1] || actualFound != test[2] {
    92  			t.Errorf("Got %v expected %v", actualValue, test[1])
    93  		}
    94  	}
    95  }
    96  
    97  func TestAVLTreeRemove(t *testing.T) {
    98  	tree := NewWithIntComparator[string]()
    99  	tree.Put(5, "e")
   100  	tree.Put(6, "f")
   101  	tree.Put(7, "g")
   102  	tree.Put(3, "c")
   103  	tree.Put(4, "d")
   104  	tree.Put(1, "x")
   105  	tree.Put(2, "b")
   106  	tree.Put(1, "a") //overwrite
   107  
   108  	tree.Remove(5)
   109  	tree.Remove(6)
   110  	tree.Remove(7)
   111  	tree.Remove(8)
   112  	tree.Remove(5)
   113  
   114  	if actualValue, expectedValue := tree.Keys(), []int{1, 2, 3, 4}; !reflect.DeepEqual(actualValue, expectedValue) {
   115  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   116  	}
   117  	if actualValue, expectedValue := tree.Values(), []string{"a", "b", "c", "d"}; !reflect.DeepEqual(actualValue, expectedValue) {
   118  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   119  	}
   120  	if actualValue := tree.Size(); actualValue != 4 {
   121  		t.Errorf("Got %v expected %v", actualValue, 7)
   122  	}
   123  
   124  	tests2 := [][]interface{}{
   125  		{1, "a", true},
   126  		{2, "b", true},
   127  		{3, "c", true},
   128  		{4, "d", true},
   129  		{5, "", false},
   130  		{6, "", false},
   131  		{7, "", false},
   132  		{8, "", false},
   133  	}
   134  
   135  	for _, test := range tests2 {
   136  		actualValue, actualFound := tree.Get(test[0].(int))
   137  		if actualValue != test[1] || actualFound != test[2] {
   138  			t.Errorf("Got %v expected %v", actualValue, test[1])
   139  		}
   140  	}
   141  
   142  	tree.Remove(1)
   143  	tree.Remove(4)
   144  	tree.Remove(2)
   145  	tree.Remove(3)
   146  	tree.Remove(2)
   147  	tree.Remove(2)
   148  
   149  	if actualValue, expectedValue := tree.Keys(), []int{}; !reflect.DeepEqual(actualValue, expectedValue) {
   150  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   151  	}
   152  	if actualValue, expectedValue := tree.Values(), []string{}; !reflect.DeepEqual(actualValue, expectedValue) {
   153  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   154  	}
   155  	if empty, size := tree.Empty(), tree.Size(); empty != true || size != -0 {
   156  		t.Errorf("Got %v expected %v", empty, true)
   157  	}
   158  
   159  }
   160  
   161  func TestAVLTreeLeftAndRight(t *testing.T) {
   162  	tree := NewWithIntComparator[string]()
   163  
   164  	if actualValue := tree.Left(); actualValue != nil {
   165  		t.Errorf("Got %v expected %v", actualValue, nil)
   166  	}
   167  	if actualValue := tree.Right(); actualValue != nil {
   168  		t.Errorf("Got %v expected %v", actualValue, nil)
   169  	}
   170  
   171  	tree.Put(1, "a")
   172  	tree.Put(5, "e")
   173  	tree.Put(6, "f")
   174  	tree.Put(7, "g")
   175  	tree.Put(3, "c")
   176  	tree.Put(4, "d")
   177  	tree.Put(1, "x") // overwrite
   178  	tree.Put(2, "b")
   179  
   180  	if actualValue, expectedValue := fmt.Sprintf("%d", tree.Left().Key), "1"; actualValue != expectedValue {
   181  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   182  	}
   183  	if actualValue, expectedValue := fmt.Sprintf("%s", tree.Left().Value), "x"; actualValue != expectedValue {
   184  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   185  	}
   186  
   187  	if actualValue, expectedValue := fmt.Sprintf("%d", tree.Right().Key), "7"; actualValue != expectedValue {
   188  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   189  	}
   190  	if actualValue, expectedValue := fmt.Sprintf("%s", tree.Right().Value), "g"; actualValue != expectedValue {
   191  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   192  	}
   193  }
   194  
   195  func TestAVLTreeCeilingAndFloor(t *testing.T) {
   196  	tree := NewWithIntComparator[string]()
   197  
   198  	if node, found := tree.Floor(0); node != nil || found {
   199  		t.Errorf("Got %v expected %v", node, "<nil>")
   200  	}
   201  	if node, found := tree.Ceiling(0); node != nil || found {
   202  		t.Errorf("Got %v expected %v", node, "<nil>")
   203  	}
   204  
   205  	tree.Put(5, "e")
   206  	tree.Put(6, "f")
   207  	tree.Put(7, "g")
   208  	tree.Put(3, "c")
   209  	tree.Put(4, "d")
   210  	tree.Put(1, "x")
   211  	tree.Put(2, "b")
   212  
   213  	if node, found := tree.Floor(4); node.Key != 4 || !found {
   214  		t.Errorf("Got %v expected %v", node.Key, 4)
   215  	}
   216  	if node, found := tree.Floor(0); node != nil || found {
   217  		t.Errorf("Got %v expected %v", node, "<nil>")
   218  	}
   219  
   220  	if node, found := tree.Ceiling(4); node.Key != 4 || !found {
   221  		t.Errorf("Got %v expected %v", node.Key, 4)
   222  	}
   223  	if node, found := tree.Ceiling(8); node != nil || found {
   224  		t.Errorf("Got %v expected %v", node, "<nil>")
   225  	}
   226  }
   227  
   228  func TestAVLTreeIteratorNextOnEmpty(t *testing.T) {
   229  	tree := NewWithIntComparator[string]()
   230  	it := tree.Iterator()
   231  	for it.Next() {
   232  		t.Errorf("Shouldn't iterate on empty tree")
   233  	}
   234  }
   235  
   236  func TestAVLTreeIteratorPrevOnEmpty(t *testing.T) {
   237  	tree := NewWithIntComparator[string]()
   238  	it := tree.Iterator()
   239  	for it.Prev() {
   240  		t.Errorf("Shouldn't iterate on empty tree")
   241  	}
   242  }
   243  
   244  func TestAVLTreeIterator1Next(t *testing.T) {
   245  	tree := NewWithIntComparator[string]()
   246  	tree.Put(5, "e")
   247  	tree.Put(6, "f")
   248  	tree.Put(7, "g")
   249  	tree.Put(3, "c")
   250  	tree.Put(4, "d")
   251  	tree.Put(1, "x")
   252  	tree.Put(2, "b")
   253  	tree.Put(1, "a") //overwrite
   254  	// │   ┌── 7
   255  	// └── 6
   256  	//     │   ┌── 5
   257  	//     └── 4
   258  	//         │   ┌── 3
   259  	//         └── 2
   260  	//             └── 1
   261  	it := tree.Iterator()
   262  
   263  	count := 0
   264  	for it.Next() {
   265  		count++
   266  		key := it.Key()
   267  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   268  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   269  		}
   270  	}
   271  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   272  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   273  	}
   274  }
   275  
   276  func TestAVLTreeIterator1Prev(t *testing.T) {
   277  	tree := NewWithIntComparator[string]()
   278  	tree.Put(5, "e")
   279  	tree.Put(6, "f")
   280  	tree.Put(7, "g")
   281  	tree.Put(3, "c")
   282  	tree.Put(4, "d")
   283  	tree.Put(1, "x")
   284  	tree.Put(2, "b")
   285  	tree.Put(1, "a") //overwrite
   286  	// │   ┌── 7
   287  	// └── 6
   288  	//     │   ┌── 5
   289  	//     └── 4
   290  	//         │   ┌── 3
   291  	//         └── 2
   292  	//             └── 1
   293  	it := tree.Iterator()
   294  	for it.Next() {
   295  	}
   296  	countDown := tree.size
   297  	for it.Prev() {
   298  		key := it.Key()
   299  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   300  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   301  		}
   302  		countDown--
   303  	}
   304  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   305  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   306  	}
   307  }
   308  
   309  func TestAVLTreeIterator2Next(t *testing.T) {
   310  	tree := NewWithIntComparator[string]()
   311  	tree.Put(3, "c")
   312  	tree.Put(1, "a")
   313  	tree.Put(2, "b")
   314  	it := tree.Iterator()
   315  	count := 0
   316  	for it.Next() {
   317  		count++
   318  		key := it.Key()
   319  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   320  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   321  		}
   322  	}
   323  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   324  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   325  	}
   326  }
   327  
   328  func TestAVLTreeIterator2Prev(t *testing.T) {
   329  	tree := NewWithIntComparator[string]()
   330  	tree.Put(3, "c")
   331  	tree.Put(1, "a")
   332  	tree.Put(2, "b")
   333  	it := tree.Iterator()
   334  	for it.Next() {
   335  	}
   336  	countDown := tree.size
   337  	for it.Prev() {
   338  		key := it.Key()
   339  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   340  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   341  		}
   342  		countDown--
   343  	}
   344  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   345  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   346  	}
   347  }
   348  
   349  func TestAVLTreeIterator3Next(t *testing.T) {
   350  	tree := NewWithIntComparator[string]()
   351  	tree.Put(1, "a")
   352  	it := tree.Iterator()
   353  	count := 0
   354  	for it.Next() {
   355  		count++
   356  		key := it.Key()
   357  		if actualValue, expectedValue := key, count; actualValue != expectedValue {
   358  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   359  		}
   360  	}
   361  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   362  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   363  	}
   364  }
   365  
   366  func TestAVLTreeIterator3Prev(t *testing.T) {
   367  	tree := NewWithIntComparator[string]()
   368  	tree.Put(1, "a")
   369  	it := tree.Iterator()
   370  	for it.Next() {
   371  	}
   372  	countDown := tree.size
   373  	for it.Prev() {
   374  		key := it.Key()
   375  		if actualValue, expectedValue := key, countDown; actualValue != expectedValue {
   376  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   377  		}
   378  		countDown--
   379  	}
   380  	if actualValue, expectedValue := countDown, 0; actualValue != expectedValue {
   381  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   382  	}
   383  }
   384  
   385  func TestAVLTreeIterator4Next(t *testing.T) {
   386  	tree := NewWithIntComparator[int]()
   387  	tree.Put(13, 5)
   388  	tree.Put(8, 3)
   389  	tree.Put(17, 7)
   390  	tree.Put(1, 1)
   391  	tree.Put(11, 4)
   392  	tree.Put(15, 6)
   393  	tree.Put(25, 9)
   394  	tree.Put(6, 2)
   395  	tree.Put(22, 8)
   396  	tree.Put(27, 10)
   397  	// │           ┌── 27
   398  	// │       ┌── 25
   399  	// │       │   └── 22
   400  	// │   ┌── 17
   401  	// │   │   └── 15
   402  	// └── 13
   403  	//     │   ┌── 11
   404  	//     └── 8
   405  	//         │   ┌── 6
   406  	//         └── 1
   407  	it := tree.Iterator()
   408  	count := 0
   409  	for it.Next() {
   410  		count++
   411  		value := it.Value()
   412  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   413  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   414  		}
   415  	}
   416  	if actualValue, expectedValue := count, tree.Size(); actualValue != expectedValue {
   417  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   418  	}
   419  }
   420  
   421  func TestAVLTreeIterator4Prev(t *testing.T) {
   422  	tree := NewWithIntComparator[int]()
   423  	tree.Put(13, 5)
   424  	tree.Put(8, 3)
   425  	tree.Put(17, 7)
   426  	tree.Put(1, 1)
   427  	tree.Put(11, 4)
   428  	tree.Put(15, 6)
   429  	tree.Put(25, 9)
   430  	tree.Put(6, 2)
   431  	tree.Put(22, 8)
   432  	tree.Put(27, 10)
   433  	// │           ┌── 27
   434  	// │       ┌── 25
   435  	// │       │   └── 22
   436  	// │   ┌── 17
   437  	// │   │   └── 15
   438  	// └── 13
   439  	//     │   ┌── 11
   440  	//     └── 8
   441  	//         │   ┌── 6
   442  	//         └── 1
   443  	it := tree.Iterator()
   444  	count := tree.Size()
   445  	for it.Next() {
   446  	}
   447  	for it.Prev() {
   448  		value := it.Value()
   449  		if actualValue, expectedValue := value, count; actualValue != expectedValue {
   450  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   451  		}
   452  		count--
   453  	}
   454  	if actualValue, expectedValue := count, 0; actualValue != expectedValue {
   455  		t.Errorf("Size different. Got %v expected %v", actualValue, expectedValue)
   456  	}
   457  }
   458  
   459  func TestAVLTreeIteratorBegin(t *testing.T) {
   460  	tree := NewWithIntComparator[string]()
   461  	tree.Put(3, "c")
   462  	tree.Put(1, "a")
   463  	tree.Put(2, "b")
   464  	it := tree.Iterator()
   465  
   466  	if it.Key() != 0 {
   467  		t.Errorf("Got %v expected %v", it.Key(), nil)
   468  	}
   469  
   470  	it.Begin()
   471  
   472  	if it.Key() != 0 {
   473  		t.Errorf("Got %v expected %v", it.Key(), nil)
   474  	}
   475  
   476  	for it.Next() {
   477  	}
   478  
   479  	it.Begin()
   480  
   481  	if it.Key() != 0 {
   482  		t.Errorf("Got %v expected %v", it.Key(), nil)
   483  	}
   484  
   485  	it.Next()
   486  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   487  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   488  	}
   489  }
   490  
   491  func TestAVLTreeIteratorEnd(t *testing.T) {
   492  	tree := NewWithIntComparator[string]()
   493  	it := tree.Iterator()
   494  
   495  	if it.Key() != 0 {
   496  		t.Errorf("Got %v expected %v", it.Key(), nil)
   497  	}
   498  
   499  	it.End()
   500  	if it.Key() != 0 {
   501  		t.Errorf("Got %v expected %v", it.Key(), nil)
   502  	}
   503  
   504  	tree.Put(3, "c")
   505  	tree.Put(1, "a")
   506  	tree.Put(2, "b")
   507  	it.End()
   508  	if it.Key() != 0 {
   509  		t.Errorf("Got %v expected %v", it.Key(), nil)
   510  	}
   511  
   512  	it.Prev()
   513  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   514  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   515  	}
   516  }
   517  
   518  func TestAVLTreeIteratorFirst(t *testing.T) {
   519  	tree := NewWithIntComparator[string]()
   520  	tree.Put(3, "c")
   521  	tree.Put(1, "a")
   522  	tree.Put(2, "b")
   523  	it := tree.Iterator()
   524  	if actualValue, expectedValue := it.First(), true; actualValue != expectedValue {
   525  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   526  	}
   527  	if key, value := it.Key(), it.Value(); key != 1 || value != "a" {
   528  		t.Errorf("Got %v,%v expected %v,%v", key, value, 1, "a")
   529  	}
   530  }
   531  
   532  func TestAVLTreeIteratorLast(t *testing.T) {
   533  	tree := NewWithIntComparator[string]()
   534  	tree.Put(3, "c")
   535  	tree.Put(1, "a")
   536  	tree.Put(2, "b")
   537  	it := tree.Iterator()
   538  	if actualValue, expectedValue := it.Last(), true; actualValue != expectedValue {
   539  		t.Errorf("Got %v expected %v", actualValue, expectedValue)
   540  	}
   541  	if key, value := it.Key(), it.Value(); key != 3 || value != "c" {
   542  		t.Errorf("Got %v,%v expected %v,%v", key, value, 3, "c")
   543  	}
   544  }
   545  
   546  func TestAVLTreeIteratorNextTo(t *testing.T) {
   547  	// Sample seek function, i.e. string starting with "b"
   548  	seek := func(index int, value string) bool {
   549  		return strings.HasSuffix(value, "b")
   550  	}
   551  
   552  	// NextTo (empty)
   553  	{
   554  		tree := NewWithIntComparator[string]()
   555  		it := tree.Iterator()
   556  		for it.NextTo(seek) {
   557  			t.Errorf("Shouldn't iterate on empty tree")
   558  		}
   559  	}
   560  
   561  	// NextTo (not found)
   562  	{
   563  		tree := NewWithIntComparator[string]()
   564  		tree.Put(0, "xx")
   565  		tree.Put(1, "yy")
   566  		it := tree.Iterator()
   567  		for it.NextTo(seek) {
   568  			t.Errorf("Shouldn't iterate on empty tree")
   569  		}
   570  	}
   571  
   572  	// NextTo (found)
   573  	{
   574  		tree := NewWithIntComparator[string]()
   575  		tree.Put(2, "cc")
   576  		tree.Put(0, "aa")
   577  		tree.Put(1, "bb")
   578  		it := tree.Iterator()
   579  		it.Begin()
   580  		if !it.NextTo(seek) {
   581  			t.Errorf("Shouldn't iterate on empty tree")
   582  		}
   583  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
   584  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   585  		}
   586  		if !it.Next() {
   587  			t.Errorf("Should go to first element")
   588  		}
   589  		if index, value := it.Key(), it.Value(); index != 2 || value != "cc" {
   590  			t.Errorf("Got %v,%v expected %v,%v", index, value, 2, "cc")
   591  		}
   592  		if it.Next() {
   593  			t.Errorf("Should not go past last element")
   594  		}
   595  	}
   596  }
   597  
   598  func TestAVLTreeIteratorPrevTo(t *testing.T) {
   599  	// Sample seek function, i.e. string starting with "b"
   600  	seek := func(index int, value string) bool {
   601  		return strings.HasSuffix(value, "b")
   602  	}
   603  
   604  	// PrevTo (empty)
   605  	{
   606  		tree := NewWithIntComparator[string]()
   607  		it := tree.Iterator()
   608  		it.End()
   609  		for it.PrevTo(seek) {
   610  			t.Errorf("Shouldn't iterate on empty tree")
   611  		}
   612  	}
   613  
   614  	// PrevTo (not found)
   615  	{
   616  		tree := NewWithIntComparator[string]()
   617  		tree.Put(0, "xx")
   618  		tree.Put(1, "yy")
   619  		it := tree.Iterator()
   620  		it.End()
   621  		for it.PrevTo(seek) {
   622  			t.Errorf("Shouldn't iterate on empty tree")
   623  		}
   624  	}
   625  
   626  	// PrevTo (found)
   627  	{
   628  		tree := NewWithIntComparator[string]()
   629  		tree.Put(2, "cc")
   630  		tree.Put(0, "aa")
   631  		tree.Put(1, "bb")
   632  		it := tree.Iterator()
   633  		it.End()
   634  		if !it.PrevTo(seek) {
   635  			t.Errorf("Shouldn't iterate on empty tree")
   636  		}
   637  		if index, value := it.Key(), it.Value(); index != 1 || value != "bb" {
   638  			t.Errorf("Got %v,%v expected %v,%v", index, value, 1, "bb")
   639  		}
   640  		if !it.Prev() {
   641  			t.Errorf("Should go to first element")
   642  		}
   643  		if index, value := it.Key(), it.Value(); index != 0 || value != "aa" {
   644  			t.Errorf("Got %v,%v expected %v,%v", index, value, 0, "aa")
   645  		}
   646  		if it.Prev() {
   647  			t.Errorf("Should not go before first element")
   648  		}
   649  	}
   650  }
   651  
   652  func TestAVLTreeSerialization(t *testing.T) {
   653  	tree := NewWith[string, string](bcomparator.StringComparator())
   654  	tree = NewWithStringComparator[string]()
   655  	tree.Put("c", "3")
   656  	tree.Put("b", "2")
   657  	tree.Put("a", "1")
   658  
   659  	var err error
   660  	assert := func() {
   661  		if actualValue, expectedValue := tree.Size(), 3; actualValue != expectedValue {
   662  			t.Errorf("Got %v expected %v", actualValue, expectedValue)
   663  		}
   664  		if actualValue := tree.Keys(); actualValue[0] != "a" || actualValue[1] != "b" || actualValue[2] != "c" {
   665  			t.Errorf("Got %v expected %v", actualValue, "[a,b,c]")
   666  		}
   667  		if actualValue := tree.Values(); actualValue[0] != "1" || actualValue[1] != "2" || actualValue[2] != "3" {
   668  			t.Errorf("Got %v expected %v", actualValue, "[1,2,3]")
   669  		}
   670  		if err != nil {
   671  			t.Errorf("Got error %v", err)
   672  		}
   673  	}
   674  
   675  	assert()
   676  
   677  	bytes, err := tree.MarshalJSON()
   678  	assert()
   679  
   680  	err = tree.UnmarshalJSON(bytes)
   681  	assert()
   682  
   683  	bytes, err = json.Marshal([]interface{}{"a", "b", "c", tree})
   684  	if err != nil {
   685  		t.Errorf("Got error %v", err)
   686  	}
   687  
   688  	err = json.Unmarshal([]byte(`{"a":"1","b":"2"}`), &tree)
   689  	if err != nil {
   690  		t.Errorf("Got error %v", err)
   691  	}
   692  }
   693  
   694  func TestAVLTreeString(t *testing.T) {
   695  	c := NewWithIntComparator[int]()
   696  	c.Put(1, 1)
   697  	c.Put(2, 1)
   698  	c.Put(3, 1)
   699  	c.Put(4, 1)
   700  	c.Put(5, 1)
   701  	c.Put(6, 1)
   702  	c.Put(7, 1)
   703  	c.Put(8, 1)
   704  
   705  	if !strings.HasPrefix(c.String(), "AVLTree") {
   706  		t.Errorf("String should start with container name")
   707  	}
   708  }
   709  
   710  func benchmarkGet[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   711  	for i := 0; i < b.N; i++ {
   712  		for n := 0; n < size; n++ {
   713  			tree.Get(K(n))
   714  		}
   715  	}
   716  }
   717  
   718  func benchmarkPut[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   719  	for i := 0; i < b.N; i++ {
   720  		for n := 0; n < size; n++ {
   721  			tree.Put(K(n), struct{}{})
   722  		}
   723  	}
   724  }
   725  
   726  func benchmarkRemove[K int, V struct{}](b *testing.B, tree *Tree[K, V], size int) {
   727  	for i := 0; i < b.N; i++ {
   728  		for n := 0; n < size; n++ {
   729  			tree.Remove(K(n))
   730  		}
   731  	}
   732  }
   733  
   734  func BenchmarkAVLTreeGet100(b *testing.B) {
   735  	b.StopTimer()
   736  	size := 100
   737  	tree := NewWithIntComparator[struct{}]()
   738  	for n := 0; n < size; n++ {
   739  		tree.Put(n, struct{}{})
   740  	}
   741  	b.StartTimer()
   742  	benchmarkGet(b, tree, size)
   743  }
   744  
   745  func BenchmarkAVLTreeGet1000(b *testing.B) {
   746  	b.StopTimer()
   747  	size := 1000
   748  	tree := NewWithIntComparator[struct{}]()
   749  	for n := 0; n < size; n++ {
   750  		tree.Put(n, struct{}{})
   751  	}
   752  	b.StartTimer()
   753  	benchmarkGet(b, tree, size)
   754  }
   755  
   756  func BenchmarkAVLTreeGet10000(b *testing.B) {
   757  	b.StopTimer()
   758  	size := 10000
   759  	tree := NewWithIntComparator[struct{}]()
   760  	for n := 0; n < size; n++ {
   761  		tree.Put(n, struct{}{})
   762  	}
   763  	b.StartTimer()
   764  	benchmarkGet(b, tree, size)
   765  }
   766  
   767  func BenchmarkAVLTreeGet100000(b *testing.B) {
   768  	b.StopTimer()
   769  	size := 100000
   770  	tree := NewWithIntComparator[struct{}]()
   771  	for n := 0; n < size; n++ {
   772  		tree.Put(n, struct{}{})
   773  	}
   774  	b.StartTimer()
   775  	benchmarkGet(b, tree, size)
   776  }
   777  
   778  func BenchmarkAVLTreePut100(b *testing.B) {
   779  	b.StopTimer()
   780  	size := 100
   781  	tree := NewWithIntComparator[struct{}]()
   782  	b.StartTimer()
   783  	benchmarkPut(b, tree, size)
   784  }
   785  
   786  func BenchmarkAVLTreePut1000(b *testing.B) {
   787  	b.StopTimer()
   788  	size := 1000
   789  	tree := NewWithIntComparator[struct{}]()
   790  	for n := 0; n < size; n++ {
   791  		tree.Put(n, struct{}{})
   792  	}
   793  	b.StartTimer()
   794  	benchmarkPut(b, tree, size)
   795  }
   796  
   797  func BenchmarkAVLTreePut10000(b *testing.B) {
   798  	b.StopTimer()
   799  	size := 10000
   800  	tree := NewWithIntComparator[struct{}]()
   801  	for n := 0; n < size; n++ {
   802  		tree.Put(n, struct{}{})
   803  	}
   804  	b.StartTimer()
   805  	benchmarkPut(b, tree, size)
   806  }
   807  
   808  func BenchmarkAVLTreePut100000(b *testing.B) {
   809  	b.StopTimer()
   810  	size := 100000
   811  	tree := NewWithIntComparator[struct{}]()
   812  	for n := 0; n < size; n++ {
   813  		tree.Put(n, struct{}{})
   814  	}
   815  	b.StartTimer()
   816  	benchmarkPut(b, tree, size)
   817  }
   818  
   819  func BenchmarkAVLTreeRemove100(b *testing.B) {
   820  	b.StopTimer()
   821  	size := 100
   822  	tree := NewWithIntComparator[struct{}]()
   823  	for n := 0; n < size; n++ {
   824  		tree.Put(n, struct{}{})
   825  	}
   826  	b.StartTimer()
   827  	benchmarkRemove(b, tree, size)
   828  }
   829  
   830  func BenchmarkAVLTreeRemove1000(b *testing.B) {
   831  	b.StopTimer()
   832  	size := 1000
   833  	tree := NewWithIntComparator[struct{}]()
   834  	for n := 0; n < size; n++ {
   835  		tree.Put(n, struct{}{})
   836  	}
   837  	b.StartTimer()
   838  	benchmarkRemove(b, tree, size)
   839  }
   840  
   841  func BenchmarkAVLTreeRemove10000(b *testing.B) {
   842  	b.StopTimer()
   843  	size := 10000
   844  	tree := NewWithIntComparator[struct{}]()
   845  	for n := 0; n < size; n++ {
   846  		tree.Put(n, struct{}{})
   847  	}
   848  	b.StartTimer()
   849  	benchmarkRemove(b, tree, size)
   850  }
   851  
   852  func BenchmarkAVLTreeRemove100000(b *testing.B) {
   853  	b.StopTimer()
   854  	size := 100000
   855  	tree := NewWithIntComparator[struct{}]()
   856  	for n := 0; n < size; n++ {
   857  		tree.Put(n, struct{}{})
   858  	}
   859  	b.StartTimer()
   860  	benchmarkRemove(b, tree, size)
   861  }