github.com/searKing/golang/go@v1.2.117/container/trie_tree/ternary_search_tree/example_test.go (about) 1 // Copyright 2020 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package ternary_search_tree_test 6 7 import ( 8 "fmt" 9 10 "github.com/searKing/golang/go/container/traversal" 11 "github.com/searKing/golang/go/container/trie_tree/ternary_search_tree" 12 ) 13 14 func ExampleNew() { 15 tree := ternary_search_tree.New("abcdef") 16 fmt.Println("count: ", tree.Count()) 17 fmt.Println("depth: ", tree.Depth()) 18 fmt.Printf("contains key %q: %v\n", "abcdef", tree.Contains("abcdef")) 19 fmt.Printf("contains key prefix %q: %v\n", "ab", tree.ContainsPrefix("ab")) 20 val, _ := tree.Load("test") 21 fmt.Printf("load key %q's value: %v\n", "test", val) 22 23 // Output: 24 // count: 1 25 // depth: 6 26 // contains key "abcdef": true 27 // contains key prefix "ab": true 28 // load key "test"'s value: <nil> 29 } 30 31 func ExampleTernarySearchTree_Store() { 32 tree := ternary_search_tree.New() 33 tree.Store("key", "val") 34 35 val, ok := tree.Load("key") 36 fmt.Printf("%q: %q, %v\n", "key", val, ok) 37 // Output: 38 // "key": "val", true 39 } 40 41 func ExampleTernarySearchTree_Load() { 42 tree := ternary_search_tree.New() 43 tree.Store("key", "val") 44 45 val, ok := tree.Load("key") 46 fmt.Printf("%q: %q, %v\n", "key", val, ok) 47 val, ok = tree.Load("not exist key") 48 fmt.Printf("%q: %v, %v\n", "not exist key", val, ok) 49 // Output: 50 // "key": "val", true 51 // "not exist key": <nil>, false 52 } 53 54 func ExampleTernarySearchTree_Count() { 55 tree := ternary_search_tree.New() 56 tree.Store("a", nil) 57 fmt.Println(tree.Count()) 58 tree.Store("ab", nil) 59 fmt.Println(tree.Count()) 60 tree.Store("x", nil) 61 fmt.Println(tree.Count()) 62 tree.Store("y", nil) 63 fmt.Println(tree.Count()) 64 65 tree.Remove("abc", true) 66 fmt.Println(tree.Count()) 67 tree.Remove("x", true) 68 fmt.Println(tree.Count()) 69 tree.Remove("a", true) 70 fmt.Println(tree.Count()) 71 72 // Output: 73 // 1 74 // 2 75 // 3 76 // 4 77 // 4 78 // 3 79 // 2 80 } 81 82 func ExampleNode_Depth() { 83 tree := ternary_search_tree.New() 84 tree.Store("a", nil) 85 fmt.Println(tree.Depth()) 86 tree.Store("ab", nil) 87 fmt.Println(tree.Depth()) 88 tree.Store("x", nil) 89 fmt.Println(tree.Depth()) 90 tree.Store("y", nil) 91 fmt.Println(tree.Depth()) 92 93 tree.Remove("abc", true) 94 fmt.Println(tree.Depth()) 95 tree.Remove("x", true) 96 fmt.Println(tree.Depth()) 97 tree.Remove("a", true) 98 fmt.Println(tree.Depth()) 99 tree.Remove("ab", true) 100 fmt.Println(tree.Depth()) 101 102 // Output: 103 // 1 104 // 2 105 // 2 106 // 2 107 // 2 108 // 2 109 // 2 110 // 1 111 } 112 113 func ExampleTernarySearchTree_RemoveAll() { 114 tree := ternary_search_tree.New() 115 tree.Store("a", nil) 116 fmt.Println(tree.Count()) 117 tree.Store("ab", nil) 118 fmt.Println(tree.Count()) 119 tree.Store("x", nil) 120 fmt.Println(tree.Count()) 121 tree.Store("y", nil) 122 fmt.Println(tree.Count()) 123 124 tree.RemoveAll("x") 125 fmt.Println(tree.Count()) 126 tree.RemoveAll("a") 127 fmt.Println(tree.Count()) 128 tree.RemoveAll("ab") 129 fmt.Println(tree.Count()) 130 131 // Output: 132 // 1 133 // 2 134 // 3 135 // 4 136 // 2 137 // 1 138 // 1 139 } 140 141 func ExampleTernarySearchTree_Traversal() { 142 tree := ternary_search_tree.New() 143 tree.Store("test", 1) 144 tree.Store("test", 11) 145 tree.Store("testing", 2) 146 tree.Store("abcd", 0) 147 148 found := false 149 tree.Traversal(traversal.Preorder, ternary_search_tree.HandlerFunc( 150 func(key []byte, val any) bool { 151 if string(key) == "test" && val.(int) == 11 { 152 found = true 153 return false 154 } 155 return true 156 })) 157 fmt.Printf("traversal for key %q, found: %v\n", "test", found) 158 159 // Output: 160 // traversal for key "test", found: true 161 }