Skip to content

Commit dcb847e

Browse files
committed
update README
1 parent 9ae7e1d commit dcb847e

File tree

1 file changed

+55
-32
lines changed

1 file changed

+55
-32
lines changed

README.md

Lines changed: 55 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Radix Tree
22

33
[![Test](https://github.com/racsoraul/radixtree/actions/workflows/go.yml/badge.svg?branch=master)](https://github.com/racsoraul/radixtree/actions/workflows/go.yml)
4+
[![Go Reference](https://pkg.go.dev/badge/github.com/racsoraul/radixtree.svg)](https://pkg.go.dev/github.com/racsoraul/radixtree)
45

56
A fast, efficient Radix Tree implementation in Go. Provides a lexicographically ordered iteration and multiple lookup
67
methods. It leverages Go iterators for a more natural API when walking the tree.
@@ -22,6 +23,10 @@ methods. It leverages Go iterators for a more natural API when walking the tree.
2223
go get github.com/racsoraul/radixtree
2324
```
2425

26+
## API Reference
27+
28+
The full API documentation is available on [GoDoc](https://pkg.go.dev/github.com/racsoraul/radixtree).
29+
2530
## Usage
2631

2732
### Basic Operations
@@ -36,23 +41,35 @@ import (
3641
)
3742

3843
func main() {
39-
tree := radixtree.New[string]()
40-
41-
// Insert entries
42-
tree.Set("apple", "A sweet red fruit")
43-
tree.Set("app", "A small application")
44-
tree.Set("banana", "A long yellow fruit")
45-
46-
// Get an entry.
47-
if val, ok := tree.Get("apple"); ok {
48-
fmt.Printf("apple: %v\n", val)
44+
// Create a tree that holds integer values (can be any type).
45+
t := radixtree.New[int]()
46+
47+
// Insert entries.
48+
t.Set("crash", 72)
49+
t.Set("ant", 20)
50+
t.Set("anagram", 40)
51+
t.Set("car", 30)
52+
t.Set("antihero", 100)
53+
t.Set("height", 11)
54+
t.Set("antares", 50)
55+
56+
// Tree size (number of entries).
57+
fmt.Println(t.Len()) // Output: 7
58+
59+
// Get entries.
60+
fmt.Println(t.Get("height")) // Output: 11 true
61+
fmt.Println(t.Get("care")) // Output: 0 false
62+
63+
// Walk the tree in lexicographical order.
64+
for k, v := range t.All() {
65+
fmt.Println("~>", k, v)
4966
}
5067

51-
// Check tree size.
52-
fmt.Printf("Tree size: %d\n", tree.Len())
53-
// Output:
54-
// apple: A sweet red fruit
55-
// Tree size: 3
68+
// Longest prefix with entries that key nodes.
69+
fmt.Println(t.LongestPrefix("antagonist")) // Output: ant
70+
71+
// Keys with prefix (autocompletion).
72+
fmt.Println(t.KeysWithPrefix("ant", 5)) // Output: [ant antares antihero]
5673
}
5774
```
5875

@@ -65,20 +82,24 @@ for key, value := range tree.All() {
6582
fmt.Printf("%s: %v\n", key, value)
6683
}
6784
// Output:
68-
// app: A small application
69-
// apple: A sweet red fruit
70-
// banana: A long yellow fruit
85+
// anagram: 40
86+
// ant: 20
87+
// antares: 50
88+
// antihero: 100
89+
// car: 30
90+
// crash: 72
91+
// height: 11
7192
```
7293

7394
### Longest Prefix Match
7495

7596
Find the longest prefix of a given string that exists as a key in the tree.
7697

7798
```go
78-
// Returns "app" if only "app" and "apple" are in the tree and we look for "application".
79-
prefix := tree.LongestPrefix("application")
99+
// Returns "ant" if we look for "antagonist".
100+
prefix := tree.LongestPrefix("antagonist")
80101
fmt.Printf("Longest prefix: %s\n", prefix)
81-
// Output: Longest prefix: app
102+
// Output: Longest prefix: ant
82103
```
83104

84105
### Prefix Search
@@ -87,10 +108,10 @@ Get all keys that start with a specific prefix. The `limit` parameter controls t
87108
`-1` for no limit.
88109

89110
```go
90-
// Get up to 10 keys with prefix "ap".
91-
keys := tree.KeysWithPrefix("ap", 10)
92-
fmt.Println("Keys with prefix 'ap':", keys)
93-
// Output: Keys with prefix 'ap': [app apple]
111+
// Get up to 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': [ant antares antihero]
94115
```
95116

96117
### Visualization
@@ -100,15 +121,17 @@ The tree provides a `String()` method to visualize its internal structure. Handy
100121
```go
101122
fmt.Println(tree)
102123
// Output:
103-
// app(2)
104-
// |__le(1)
105-
// banana(1)
124+
// an(4)
125+
// |__agram(1)
126+
// |__t(3)
127+
// |__ares(1)
128+
// |__ihero(1)
129+
// c(2)
130+
// |__ar(1)
131+
// |__rash(1)
132+
// height(1)
106133
```
107134

108-
## API Reference
109-
110-
The full API documentation is available on [GoDoc](https://pkg.go.dev/github.com/racsoraul/radixtree).
111-
112135
## Benchmarks
113136

114137
Benchmarks performed on Apple M1 Max. The benchmarks use a dataset of 370,105 English words for "Big" tests and a small

0 commit comments

Comments
 (0)