github.com/gogf/gf/v2@v2.7.4/container/gtree/gtree_z_example_redblacktree_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with gm file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gtree_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/gogf/gf/v2/container/gtree"
    13  	"github.com/gogf/gf/v2/internal/json"
    14  	"github.com/gogf/gf/v2/util/gconv"
    15  	"github.com/gogf/gf/v2/util/gutil"
    16  )
    17  
    18  func ExampleRedBlackTree_SetComparator() {
    19  	var tree gtree.RedBlackTree
    20  	tree.SetComparator(gutil.ComparatorString)
    21  	for i := 0; i < 6; i++ {
    22  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    23  	}
    24  
    25  	fmt.Println(tree.Map())
    26  	fmt.Println(tree.Size())
    27  
    28  	// Output:
    29  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    30  	// 6
    31  }
    32  
    33  func ExampleRedBlackTree_Clone() {
    34  	b := gtree.NewRedBlackTree(gutil.ComparatorString)
    35  	for i := 0; i < 6; i++ {
    36  		b.Set("key"+gconv.String(i), "val"+gconv.String(i))
    37  	}
    38  
    39  	tree := b.Clone()
    40  
    41  	fmt.Println(tree.Map())
    42  	fmt.Println(tree.Size())
    43  
    44  	// Output:
    45  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    46  	// 6
    47  }
    48  
    49  func ExampleRedBlackTree_Set() {
    50  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
    51  	for i := 0; i < 6; i++ {
    52  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    53  	}
    54  
    55  	fmt.Println(tree.Map())
    56  	fmt.Println(tree.Size())
    57  
    58  	// Output:
    59  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
    60  	// 6
    61  }
    62  
    63  func ExampleRedBlackTree_Sets() {
    64  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
    65  
    66  	tree.Sets(map[interface{}]interface{}{
    67  		"key1": "val1",
    68  		"key2": "val2",
    69  	})
    70  
    71  	fmt.Println(tree.Map())
    72  	fmt.Println(tree.Size())
    73  
    74  	// Output:
    75  	// map[key1:val1 key2:val2]
    76  	// 2
    77  }
    78  
    79  func ExampleRedBlackTree_Get() {
    80  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
    81  	for i := 0; i < 6; i++ {
    82  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    83  	}
    84  
    85  	fmt.Println(tree.Get("key1"))
    86  	fmt.Println(tree.Get("key10"))
    87  
    88  	// Output:
    89  	// val1
    90  	// <nil>
    91  }
    92  
    93  func ExampleRedBlackTree_GetOrSet() {
    94  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
    95  	for i := 0; i < 6; i++ {
    96  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
    97  	}
    98  
    99  	fmt.Println(tree.GetOrSet("key1", "newVal1"))
   100  	fmt.Println(tree.GetOrSet("key6", "val6"))
   101  
   102  	// Output:
   103  	// val1
   104  	// val6
   105  }
   106  
   107  func ExampleRedBlackTree_GetOrSetFunc() {
   108  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   109  	for i := 0; i < 6; i++ {
   110  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   111  	}
   112  
   113  	fmt.Println(tree.GetOrSetFunc("key1", func() interface{} {
   114  		return "newVal1"
   115  	}))
   116  	fmt.Println(tree.GetOrSetFunc("key6", func() interface{} {
   117  		return "val6"
   118  	}))
   119  
   120  	// Output:
   121  	// val1
   122  	// val6
   123  }
   124  
   125  func ExampleRedBlackTree_GetOrSetFuncLock() {
   126  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   127  	for i := 0; i < 6; i++ {
   128  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   129  	}
   130  
   131  	fmt.Println(tree.GetOrSetFuncLock("key1", func() interface{} {
   132  		return "newVal1"
   133  	}))
   134  	fmt.Println(tree.GetOrSetFuncLock("key6", func() interface{} {
   135  		return "val6"
   136  	}))
   137  
   138  	// Output:
   139  	// val1
   140  	// val6
   141  }
   142  
   143  func ExampleRedBlackTree_GetVar() {
   144  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   145  	for i := 0; i < 6; i++ {
   146  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   147  	}
   148  
   149  	fmt.Println(tree.GetVar("key1").String())
   150  
   151  	// Output:
   152  	// val1
   153  }
   154  
   155  func ExampleRedBlackTree_GetVarOrSet() {
   156  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   157  	for i := 0; i < 6; i++ {
   158  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   159  	}
   160  
   161  	fmt.Println(tree.GetVarOrSet("key1", "newVal1"))
   162  	fmt.Println(tree.GetVarOrSet("key6", "val6"))
   163  
   164  	// Output:
   165  	// val1
   166  	// val6
   167  }
   168  
   169  func ExampleRedBlackTree_GetVarOrSetFunc() {
   170  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   171  	for i := 0; i < 6; i++ {
   172  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   173  	}
   174  
   175  	fmt.Println(tree.GetVarOrSetFunc("key1", func() interface{} {
   176  		return "newVal1"
   177  	}))
   178  	fmt.Println(tree.GetVarOrSetFunc("key6", func() interface{} {
   179  		return "val6"
   180  	}))
   181  
   182  	// Output:
   183  	// val1
   184  	// val6
   185  }
   186  
   187  func ExampleRedBlackTree_GetVarOrSetFuncLock() {
   188  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   189  	for i := 0; i < 6; i++ {
   190  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   191  	}
   192  
   193  	fmt.Println(tree.GetVarOrSetFuncLock("key1", func() interface{} {
   194  		return "newVal1"
   195  	}))
   196  	fmt.Println(tree.GetVarOrSetFuncLock("key6", func() interface{} {
   197  		return "val6"
   198  	}))
   199  
   200  	// Output:
   201  	// val1
   202  	// val6
   203  }
   204  
   205  func ExampleRedBlackTree_SetIfNotExist() {
   206  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   207  	for i := 0; i < 6; i++ {
   208  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   209  	}
   210  
   211  	fmt.Println(tree.SetIfNotExist("key1", "newVal1"))
   212  	fmt.Println(tree.SetIfNotExist("key6", "val6"))
   213  
   214  	// Output:
   215  	// false
   216  	// true
   217  }
   218  
   219  func ExampleRedBlackTree_SetIfNotExistFunc() {
   220  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   221  	for i := 0; i < 6; i++ {
   222  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   223  	}
   224  
   225  	fmt.Println(tree.SetIfNotExistFunc("key1", func() interface{} {
   226  		return "newVal1"
   227  	}))
   228  	fmt.Println(tree.SetIfNotExistFunc("key6", func() interface{} {
   229  		return "val6"
   230  	}))
   231  
   232  	// Output:
   233  	// false
   234  	// true
   235  }
   236  
   237  func ExampleRedBlackTree_SetIfNotExistFuncLock() {
   238  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   239  	for i := 0; i < 6; i++ {
   240  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   241  	}
   242  
   243  	fmt.Println(tree.SetIfNotExistFuncLock("key1", func() interface{} {
   244  		return "newVal1"
   245  	}))
   246  	fmt.Println(tree.SetIfNotExistFuncLock("key6", func() interface{} {
   247  		return "val6"
   248  	}))
   249  
   250  	// Output:
   251  	// false
   252  	// true
   253  }
   254  
   255  func ExampleRedBlackTree_Contains() {
   256  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   257  	for i := 0; i < 6; i++ {
   258  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   259  	}
   260  
   261  	fmt.Println(tree.Contains("key1"))
   262  	fmt.Println(tree.Contains("key6"))
   263  
   264  	// Output:
   265  	// true
   266  	// false
   267  }
   268  
   269  func ExampleRedBlackTree_Remove() {
   270  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   271  	for i := 0; i < 6; i++ {
   272  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   273  	}
   274  
   275  	fmt.Println(tree.Remove("key1"))
   276  	fmt.Println(tree.Remove("key6"))
   277  	fmt.Println(tree.Map())
   278  
   279  	// Output:
   280  	// val1
   281  	// <nil>
   282  	// map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
   283  }
   284  
   285  func ExampleRedBlackTree_Removes() {
   286  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   287  	for i := 0; i < 6; i++ {
   288  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   289  	}
   290  
   291  	removeKeys := make([]interface{}, 2)
   292  	removeKeys = append(removeKeys, "key1")
   293  	removeKeys = append(removeKeys, "key6")
   294  
   295  	tree.Removes(removeKeys)
   296  
   297  	fmt.Println(tree.Map())
   298  
   299  	// Output:
   300  	// map[key0:val0 key2:val2 key3:val3 key4:val4 key5:val5]
   301  }
   302  
   303  func ExampleRedBlackTree_IsEmpty() {
   304  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   305  
   306  	fmt.Println(tree.IsEmpty())
   307  
   308  	for i := 0; i < 6; i++ {
   309  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   310  	}
   311  
   312  	fmt.Println(tree.IsEmpty())
   313  
   314  	// Output:
   315  	// true
   316  	// false
   317  }
   318  
   319  func ExampleRedBlackTree_Size() {
   320  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   321  
   322  	fmt.Println(tree.Size())
   323  
   324  	for i := 0; i < 6; i++ {
   325  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   326  	}
   327  
   328  	fmt.Println(tree.Size())
   329  
   330  	// Output:
   331  	// 0
   332  	// 6
   333  }
   334  
   335  func ExampleRedBlackTree_Keys() {
   336  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   337  	for i := 6; i > 0; i-- {
   338  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   339  	}
   340  
   341  	fmt.Println(tree.Keys())
   342  
   343  	// Output:
   344  	// [key1 key2 key3 key4 key5 key6]
   345  }
   346  
   347  func ExampleRedBlackTree_Values() {
   348  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   349  	for i := 6; i > 0; i-- {
   350  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   351  	}
   352  
   353  	fmt.Println(tree.Values())
   354  
   355  	// Output:
   356  	// [val1 val2 val3 val4 val5 val6]
   357  }
   358  
   359  func ExampleRedBlackTree_Map() {
   360  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   361  	for i := 0; i < 6; i++ {
   362  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   363  	}
   364  
   365  	fmt.Println(tree.Map())
   366  
   367  	// Output:
   368  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
   369  }
   370  
   371  func ExampleRedBlackTree_MapStrAny() {
   372  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   373  	for i := 0; i < 6; i++ {
   374  		tree.Set(1000+i, "val"+gconv.String(i))
   375  	}
   376  
   377  	fmt.Println(tree.MapStrAny())
   378  
   379  	// Output:
   380  	// map[1000:val0 1001:val1 1002:val2 1003:val3 1004:val4 1005:val5]
   381  }
   382  
   383  func ExampleRedBlackTree_Left() {
   384  	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   385  	for i := 1; i < 100; i++ {
   386  		tree.Set(i, i)
   387  	}
   388  	fmt.Println(tree.Left().Key, tree.Left().Value)
   389  
   390  	emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   391  	fmt.Println(emptyTree.Left())
   392  
   393  	// Output:
   394  	// 1 1
   395  	// <nil>
   396  }
   397  
   398  func ExampleRedBlackTree_Right() {
   399  	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   400  	for i := 1; i < 100; i++ {
   401  		tree.Set(i, i)
   402  	}
   403  	fmt.Println(tree.Right().Key, tree.Right().Value)
   404  
   405  	emptyTree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   406  	fmt.Println(emptyTree.Left())
   407  
   408  	// Output:
   409  	// 99 99
   410  	// <nil>
   411  }
   412  
   413  func ExampleRedBlackTree_Floor() {
   414  	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   415  	for i := 1; i < 100; i++ {
   416  		if i != 50 {
   417  			tree.Set(i, i)
   418  		}
   419  	}
   420  
   421  	node, found := tree.Floor(95)
   422  	if found {
   423  		fmt.Println("Floor 95:", node.Key)
   424  	}
   425  
   426  	node, found = tree.Floor(50)
   427  	if found {
   428  		fmt.Println("Floor 50:", node.Key)
   429  	}
   430  
   431  	node, found = tree.Floor(100)
   432  	if found {
   433  		fmt.Println("Floor 100:", node.Key)
   434  	}
   435  
   436  	node, found = tree.Floor(0)
   437  	if found {
   438  		fmt.Println("Floor 0:", node.Key)
   439  	}
   440  
   441  	// Output:
   442  	// Floor 95: 95
   443  	// Floor 50: 49
   444  	// Floor 100: 99
   445  }
   446  
   447  func ExampleRedBlackTree_Ceiling() {
   448  	tree := gtree.NewRedBlackTree(gutil.ComparatorInt)
   449  	for i := 1; i < 100; i++ {
   450  		if i != 50 {
   451  			tree.Set(i, i)
   452  		}
   453  	}
   454  
   455  	node, found := tree.Ceiling(1)
   456  	if found {
   457  		fmt.Println("Ceiling 1:", node.Key)
   458  	}
   459  
   460  	node, found = tree.Ceiling(50)
   461  	if found {
   462  		fmt.Println("Ceiling 50:", node.Key)
   463  	}
   464  
   465  	node, found = tree.Ceiling(100)
   466  	if found {
   467  		fmt.Println("Ceiling 100:", node.Key)
   468  	}
   469  
   470  	node, found = tree.Ceiling(-1)
   471  	if found {
   472  		fmt.Println("Ceiling -1:", node.Key)
   473  	}
   474  
   475  	// Output:
   476  	// Ceiling 1: 1
   477  	// Ceiling 50: 51
   478  	// Ceiling -1: 1
   479  }
   480  
   481  func ExampleRedBlackTree_Iterator() {
   482  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   483  	for i := 0; i < 10; i++ {
   484  		tree.Set(i, 10-i)
   485  	}
   486  
   487  	var totalKey, totalValue int
   488  	tree.Iterator(func(key, value interface{}) bool {
   489  		totalKey += key.(int)
   490  		totalValue += value.(int)
   491  
   492  		return totalValue < 20
   493  	})
   494  
   495  	fmt.Println("totalKey:", totalKey)
   496  	fmt.Println("totalValue:", totalValue)
   497  
   498  	// Output:
   499  	// totalKey: 3
   500  	// totalValue: 27
   501  }
   502  
   503  func ExampleRedBlackTree_IteratorFrom() {
   504  	m := make(map[interface{}]interface{})
   505  	for i := 1; i <= 5; i++ {
   506  		m[i] = i * 10
   507  	}
   508  	tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
   509  
   510  	tree.IteratorFrom(1, true, func(key, value interface{}) bool {
   511  		fmt.Println("key:", key, ", value:", value)
   512  		return true
   513  	})
   514  
   515  	// Output:
   516  	// key: 1 , value: 10
   517  	// key: 2 , value: 20
   518  	// key: 3 , value: 30
   519  	// key: 4 , value: 40
   520  	// key: 5 , value: 50
   521  }
   522  
   523  func ExampleRedBlackTree_IteratorAsc() {
   524  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   525  	for i := 0; i < 10; i++ {
   526  		tree.Set(i, 10-i)
   527  	}
   528  
   529  	tree.IteratorAsc(func(key, value interface{}) bool {
   530  		fmt.Println("key:", key, ", value:", value)
   531  		return true
   532  	})
   533  
   534  	// Output:
   535  	// key: 0 , value: 10
   536  	// key: 1 , value: 9
   537  	// key: 2 , value: 8
   538  	// key: 3 , value: 7
   539  	// key: 4 , value: 6
   540  	// key: 5 , value: 5
   541  	// key: 6 , value: 4
   542  	// key: 7 , value: 3
   543  	// key: 8 , value: 2
   544  	// key: 9 , value: 1
   545  }
   546  
   547  func ExampleRedBlackTree_IteratorAscFrom_Normal() {
   548  	m := make(map[interface{}]interface{})
   549  	for i := 1; i <= 5; i++ {
   550  		m[i] = i * 10
   551  	}
   552  	tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
   553  
   554  	tree.IteratorAscFrom(1, true, func(key, value interface{}) bool {
   555  		fmt.Println("key:", key, ", value:", value)
   556  		return true
   557  	})
   558  
   559  	// Output:
   560  	// key: 1 , value: 10
   561  	// key: 2 , value: 20
   562  	// key: 3 , value: 30
   563  	// key: 4 , value: 40
   564  	// key: 5 , value: 50
   565  }
   566  
   567  func ExampleRedBlackTree_IteratorAscFrom_NoExistKey() {
   568  	m := make(map[interface{}]interface{})
   569  	for i := 1; i <= 5; i++ {
   570  		m[i] = i * 10
   571  	}
   572  	tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
   573  
   574  	tree.IteratorAscFrom(0, true, func(key, value interface{}) bool {
   575  		fmt.Println("key:", key, ", value:", value)
   576  		return true
   577  	})
   578  
   579  	// Output:
   580  }
   581  
   582  func ExampleRedBlackTree_IteratorAscFrom_NoExistKeyAndMatchFalse() {
   583  	m := make(map[interface{}]interface{})
   584  	for i := 1; i <= 5; i++ {
   585  		m[i] = i * 10
   586  	}
   587  	tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
   588  
   589  	tree.IteratorAscFrom(0, false, func(key, value interface{}) bool {
   590  		fmt.Println("key:", key, ", value:", value)
   591  		return true
   592  	})
   593  
   594  	// Output:
   595  	// key: 1 , value: 10
   596  	// key: 2 , value: 20
   597  	// key: 3 , value: 30
   598  	// key: 4 , value: 40
   599  	// key: 5 , value: 50
   600  }
   601  
   602  func ExampleRedBlackTree_IteratorDesc() {
   603  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   604  	for i := 0; i < 10; i++ {
   605  		tree.Set(i, 10-i)
   606  	}
   607  
   608  	tree.IteratorDesc(func(key, value interface{}) bool {
   609  		fmt.Println("key:", key, ", value:", value)
   610  		return true
   611  	})
   612  
   613  	// Output:
   614  	// key: 9 , value: 1
   615  	// key: 8 , value: 2
   616  	// key: 7 , value: 3
   617  	// key: 6 , value: 4
   618  	// key: 5 , value: 5
   619  	// key: 4 , value: 6
   620  	// key: 3 , value: 7
   621  	// key: 2 , value: 8
   622  	// key: 1 , value: 9
   623  	// key: 0 , value: 10
   624  }
   625  
   626  func ExampleRedBlackTree_IteratorDescFrom() {
   627  	m := make(map[interface{}]interface{})
   628  	for i := 1; i <= 5; i++ {
   629  		m[i] = i * 10
   630  	}
   631  	tree := gtree.NewRedBlackTreeFrom(gutil.ComparatorInt, m)
   632  
   633  	tree.IteratorDescFrom(5, true, func(key, value interface{}) bool {
   634  		fmt.Println("key:", key, ", value:", value)
   635  		return true
   636  	})
   637  
   638  	// Output:
   639  	// key: 5 , value: 50
   640  	// key: 4 , value: 40
   641  	// key: 3 , value: 30
   642  	// key: 2 , value: 20
   643  	// key: 1 , value: 10
   644  }
   645  
   646  func ExampleRedBlackTree_Clear() {
   647  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   648  	for i := 0; i < 6; i++ {
   649  		tree.Set(1000+i, "val"+gconv.String(i))
   650  	}
   651  	fmt.Println(tree.Size())
   652  
   653  	tree.Clear()
   654  	fmt.Println(tree.Size())
   655  
   656  	// Output:
   657  	// 6
   658  	// 0
   659  }
   660  
   661  func ExampleRedBlackTree_Replace() {
   662  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   663  	for i := 0; i < 6; i++ {
   664  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   665  	}
   666  
   667  	fmt.Println(tree.Map())
   668  
   669  	data := map[interface{}]interface{}{
   670  		"newKey0": "newVal0",
   671  		"newKey1": "newVal1",
   672  		"newKey2": "newVal2",
   673  	}
   674  
   675  	tree.Replace(data)
   676  
   677  	fmt.Println(tree.Map())
   678  
   679  	// Output:
   680  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
   681  	// map[newKey0:newVal0 newKey1:newVal1 newKey2:newVal2]
   682  }
   683  
   684  func ExampleRedBlackTree_String() {
   685  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   686  	for i := 0; i < 6; i++ {
   687  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   688  	}
   689  
   690  	fmt.Println(tree.String())
   691  
   692  	// Output:
   693  	// │           ┌── key5
   694  	// │       ┌── key4
   695  	// │   ┌── key3
   696  	// │   │   └── key2
   697  	// └── key1
   698  	//     └── key0
   699  }
   700  
   701  func ExampleRedBlackTree_Print() {
   702  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   703  	for i := 0; i < 6; i++ {
   704  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   705  	}
   706  
   707  	tree.Print()
   708  
   709  	// Output:
   710  	// │           ┌── key5
   711  	// │       ┌── key4
   712  	// │   ┌── key3
   713  	// │   │   └── key2
   714  	// └── key1
   715  	//     └── key0
   716  }
   717  
   718  func ExampleRedBlackTree_Search() {
   719  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   720  	for i := 0; i < 6; i++ {
   721  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   722  	}
   723  
   724  	fmt.Println(tree.Search("key0"))
   725  	fmt.Println(tree.Search("key6"))
   726  
   727  	// Output:
   728  	// val0 true
   729  	// <nil> false
   730  }
   731  
   732  func ExampleRedBlackTree_Flip() {
   733  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   734  	for i := 1; i < 6; i++ {
   735  		tree.Set(i, i*10)
   736  	}
   737  
   738  	fmt.Println("Before Flip", tree.Map())
   739  
   740  	tree.Flip()
   741  
   742  	fmt.Println("After Flip", tree.Map())
   743  
   744  	// Output:
   745  	// Before Flip map[1:10 2:20 3:30 4:40 5:50]
   746  	// After Flip map[10:1 20:2 30:3 40:4 50:5]
   747  }
   748  
   749  func ExampleRedBlackTree_MarshalJSON() {
   750  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   751  	for i := 0; i < 6; i++ {
   752  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   753  	}
   754  
   755  	bytes, err := json.Marshal(tree)
   756  	if err == nil {
   757  		fmt.Println(gconv.String(bytes))
   758  	}
   759  
   760  	// Output:
   761  	// {"key0":"val0","key1":"val1","key2":"val2","key3":"val3","key4":"val4","key5":"val5"}
   762  }
   763  
   764  func ExampleRedBlackTree_UnmarshalJSON() {
   765  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   766  	for i := 0; i < 6; i++ {
   767  		tree.Set("key"+gconv.String(i), "val"+gconv.String(i))
   768  	}
   769  	bytes, err := json.Marshal(tree)
   770  
   771  	otherTree := gtree.NewRedBlackTree(gutil.ComparatorString)
   772  	err = json.Unmarshal(bytes, &otherTree)
   773  	if err == nil {
   774  		fmt.Println(otherTree.Map())
   775  	}
   776  
   777  	// Output:
   778  	// map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5]
   779  }
   780  
   781  func ExampleRedBlackTree_UnmarshalValue() {
   782  	tree := gtree.NewRedBlackTree(gutil.ComparatorString)
   783  
   784  	type User struct {
   785  		Uid   int
   786  		Name  string
   787  		Pass1 string `gconv:"password1"`
   788  		Pass2 string `gconv:"password2"`
   789  	}
   790  
   791  	var (
   792  		user = User{
   793  			Uid:   1,
   794  			Name:  "john",
   795  			Pass1: "123",
   796  			Pass2: "456",
   797  		}
   798  	)
   799  	if err := gconv.Scan(user, tree); err == nil {
   800  		fmt.Printf("%#v", tree.Map())
   801  	}
   802  
   803  	// Output:
   804  	// map[interface {}]interface {}{"Name":"john", "Uid":1, "password1":"123", "password2":"456"}
   805  }