@@ -65,21 +65,41 @@ func main() {
6565 fmt.Println (" ~>" , k, v)
6666 }
6767
68- // Longest prefix with entries that key nodes.
68+ // Longest prefix with entries that are key nodes.
6969 fmt.Println (t.LongestPrefix (" antagonist" )) // Output: ant
7070
7171 // Keys with prefix (autocompletion).
7272 fmt.Println (t.KeysWithPrefix (" an" , 5 )) // Output: [anagram ant antares antihero]
7373}
7474```
7575
76+ ### Set
77+
78+ The ` Set ` method inserts or updates a key-value pair in the tree.
79+
80+ ``` go
81+ tree.Set (" hello" , 100 )
82+ ```
83+
84+ ### Get
85+
86+ The ` Get ` method retrieves the value associated with a key. It returns the value and a boolean indicating if the
87+ key was found or not.
88+
89+ ``` go
90+ if value , ok := tree.Get (" hello" ); ok {
91+ fmt.Println (value)
92+ }
93+ ```
94+
7695### Iteration
7796
78- The ` All() ` method returns a Go iterator, allowing you to use ` for ... range ` loops.
97+ The ` All() ` method returns a Go iterator, allowing you to use ` for ... range ` loops. It allows you to walk the tree in
98+ lexicographical order.
7999
80100``` go
81101for key , value := range tree.All () {
82- fmt.Printf (" %s : %v \n " , key, value)
102+ fmt.Printf (" %s : %v \n " , key, value)
83103}
84104// Output:
85105// anagram: 40
@@ -97,21 +117,19 @@ Find the longest prefix of a given string that exists as a key in the tree.
97117
98118``` go
99119// Returns "ant" if we look for "antagonist".
100- prefix := tree.LongestPrefix (" antagonist" )
101- fmt.Println (prefix)
120+ fmt.Println (tree.LongestPrefix (" antagonist" ))
102121// Output: ant
103122```
104123
105- ### Prefix Search
124+ ### Keys With Prefix (Autocomplete)
106125
107126Get all keys that start with a specific prefix. The ` limit ` parameter controls the maximum number of keys to return. Use
108127` -1 ` for no limit.
109128
110129``` go
111130// Get up to (autocomplete) 10 keys with prefix "ant".
112- keys := tree.KeysWithPrefix (" ant" , 10 )
113- fmt.Println (" Keys with prefix 'ant':" , keys)
114- // Output: Keys with prefix 'ant': [anagram ant antares antihero]
131+ fmt.Println (tree.KeysWithPrefix (" ant" , 10 ))
132+ // Output: [anagram ant antares antihero]
115133```
116134
117135### Visualization
@@ -156,7 +174,7 @@ key "hoar" in a tree containing 370,105 words.
156174| ` Get ` (Small Tree) | 78,383,768 | 15.32 ns/op | 0 B/op | 0 allocs/op |
157175| ` Get ` (Big Tree) | 32,526,567 | 36.76 ns/op | 0 B/op | 0 allocs/op |
158176
159- ### Prefix Search (KeysWithPrefix)
177+ ### Keys With Prefix (KeysWithPrefix)
160178
161179Measures the performance of finding all keys that share a common prefix.
162180
0 commit comments