github.com/wangyougui/gf/v2@v2.6.5/container/gtree/gtree_z_example_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/Agogf/gf. 6 7 package gtree_test 8 9 import ( 10 "fmt" 11 12 "github.com/wangyougui/gf/v2/container/gtree" 13 "github.com/wangyougui/gf/v2/util/gconv" 14 "github.com/wangyougui/gf/v2/util/gutil" 15 ) 16 17 func ExampleNewAVLTree() { 18 avlTree := gtree.NewAVLTree(gutil.ComparatorString) 19 for i := 0; i < 6; i++ { 20 avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 21 } 22 23 fmt.Println(avlTree) 24 25 // Output: 26 // │ ┌── key5 27 // │ ┌── key4 28 // └── key3 29 // │ ┌── key2 30 // └── key1 31 // └── key0 32 } 33 34 func ExampleNewAVLTreeFrom() { 35 avlTree := gtree.NewAVLTree(gutil.ComparatorString) 36 for i := 0; i < 6; i++ { 37 avlTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 38 } 39 40 otherAvlTree := gtree.NewAVLTreeFrom(gutil.ComparatorString, avlTree.Map()) 41 fmt.Println(otherAvlTree) 42 43 // May Output: 44 // │ ┌── key5 45 // │ │ └── key4 46 // └── key3 47 // │ ┌── key2 48 // └── key1 49 // └── key0 50 } 51 52 func ExampleNewBTree() { 53 bTree := gtree.NewBTree(3, gutil.ComparatorString) 54 for i := 0; i < 6; i++ { 55 bTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 56 } 57 fmt.Println(bTree.Map()) 58 59 // Output: 60 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 61 } 62 63 func ExampleNewBTreeFrom() { 64 bTree := gtree.NewBTree(3, gutil.ComparatorString) 65 for i := 0; i < 6; i++ { 66 bTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 67 } 68 69 otherBTree := gtree.NewBTreeFrom(3, gutil.ComparatorString, bTree.Map()) 70 fmt.Println(otherBTree.Map()) 71 72 // Output: 73 // map[key0:val0 key1:val1 key2:val2 key3:val3 key4:val4 key5:val5] 74 } 75 76 func ExampleNewRedBlackTree() { 77 rbTree := gtree.NewRedBlackTree(gutil.ComparatorString) 78 for i := 0; i < 6; i++ { 79 rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 80 } 81 82 fmt.Println(rbTree) 83 84 // Output: 85 // │ ┌── key5 86 // │ ┌── key4 87 // │ ┌── key3 88 // │ │ └── key2 89 // └── key1 90 // └── key0 91 } 92 93 func ExampleNewRedBlackTreeFrom() { 94 rbTree := gtree.NewRedBlackTree(gutil.ComparatorString) 95 for i := 0; i < 6; i++ { 96 rbTree.Set("key"+gconv.String(i), "val"+gconv.String(i)) 97 } 98 99 otherRBTree := gtree.NewRedBlackTreeFrom(gutil.ComparatorString, rbTree.Map()) 100 fmt.Println(otherRBTree) 101 102 // May Output: 103 // │ ┌── key5 104 // │ ┌── key4 105 // │ ┌── key3 106 // │ │ └── key2 107 // └── key1 108 // └── key0 109 }