https://github.com/gbrlsnchs/radix
  
  
    Golang radix tree implementation 
    https://github.com/gbrlsnchs/radix
  
go golang patricia-tree patricia-trie radix radix-tree trie
        Last synced: 3 months ago 
        JSON representation
    
Golang radix tree implementation
- Host: GitHub
 - URL: https://github.com/gbrlsnchs/radix
 - Owner: gbrlsnchs
 - License: mit
 - Created: 2017-12-06T18:55:40.000Z (almost 8 years ago)
 - Default Branch: master
 - Last Pushed: 2019-03-11T13:47:49.000Z (over 6 years ago)
 - Last Synced: 2025-08-11T07:00:14.591Z (3 months ago)
 - Topics: go, golang, patricia-tree, patricia-trie, radix, radix-tree, trie
 - Language: Go
 - Homepage:
 - Size: 59.6 KB
 - Stars: 35
 - Watchers: 2
 - Forks: 5
 - Open Issues: 3
 - 
            Metadata Files:
            
- Readme: README.md
 - Changelog: CHANGELOG.md
 - License: LICENSE
 
 
Awesome Lists containing this project
README
          # radix (Radix tree implementation in Go)
[](https://travis-ci.org/gbrlsnchs/radix)
[](https://sourcegraph.com/github.com/gbrlsnchs/radix?badge)
[](https://godoc.org/github.com/gbrlsnchs/radix)
[](https://golang.org/doc/go1.10)
## About
This package is an implementation of a [radix tree](https://en.wikipedia.org/wiki/Radix_tree) in [Go](https://golang.org) (or Golang).  
Searching for static values in the tree doesn't allocate memory on the heap, what makes it pretty fast.  
It can also sort nodes by priority, therefore traversing nodes that hold more non-nil values first.
## Usage
Full documentation [here](https://godoc.org/github.com/gbrlsnchs/radix).  
### Installing
#### Go 1.10
`vgo get -u github.com/gbrlsnchs/radix`
#### Go 1.11
`go get -u github.com/gbrlsnchs/radix`
### Importing
```go
import (
	// ...
	"github.com/gbrlsnchs/radix"
)
```
### Building [this example from Wikipedia](https://upload.wikimedia.org/wikipedia/commons/a/ae/Patricia_trie.svg)
```go
tr := radix.New(radix.Tdebug)
tr.Add("romane", 1)
tr.Add("romanus", 2)
tr.Add("romulus", 3)
tr.Add("rubens", 4)
tr.Add("ruber", 5)
tr.Add("rubicon", 6)
tr.Add("rubicundus", 7)
tr.Sort(radix.PrioritySort) // optional step
fmt.Println(tr)
```
#### The code above will print this
```
. (14 nodes)
└── 7↑ r → 
    ├── 4↑ ub → 
    │   ├── 2↑ ic → 
    │   │   ├── 1↑ undus 🍂 → 7
    │   │   └── 1↑ on 🍂 → 6
    │   └── 2↑ e → 
    │       ├── 1↑ r 🍂 → 5
    │       └── 1↑ ns 🍂 → 4
    └── 3↑ om → 
        ├── 2↑ an → 
        │   ├── 1↑ us 🍂 → 2
        │   └── 1↑ e 🍂 → 1
        └── 1↑ ulus 🍂 → 3
```
### Retrieving a value from the tree
```go
n, _ := tr.Get("rubicon") // zero-allocation search
fmt.Println(n.Value)      // prints "6"
```
### Building a dynamic tree
A dynamic tree is a tree that can match labels based on a placeholder and a demiliter (e.g. an HTTP router that accepts dynamic routes).  
Note that this only works with prefix trees, not binary ones.
```go
tr := radix.New(0) // passing 0 means passing no flags
tr.Add("/dynamic/path/@id", 1)
tr.Add("/dynamic/path/@id/subpath/@name", 2)
tr.Add("/static/path", 3)
tr.SetBoundaries('@', '/')
var (
	n *radix.Node
	p map[string]string
)
n, p = tr.Get("/dynamic/path/123")
fmt.Println(n.Value) // prints "1"
fmt.Println(p["id"]) // prints "123"
n, p = tr.Get("/dynamic/path/456/subpath/foobar")
fmt.Println(n.Value)   // prints "2"
fmt.Println(p["id"])   // prints "456"
fmt.Println(p["name"]) // prints "foobar"
n, _ = tr.Get("/static/path") // p would be nil
fmt.Println(n.Value)          // prints "3"
```
### Building a binary tree
```go
tr := radix.New(radix.Tdebug | radix.Tbinary)
tr.Add("deck", 1)
tr.Add("did", 2)
tr.Add("doe", 3)
tr.Add("dog", 4)
tr.Add("doge", 5)
tr.Add("dogs", 6)
```
#### The code above will print this
```
. (71 nodes)
01100100011001010110001101101011 🍂 → 1
011001000110100101100100 🍂 → 2
011001000110111101100101 🍂 → 3
011001000110111101100111 → 4
01100100011011110110011101100101 🍂 → 5
01100100011011110110011101110011 🍂 → 6
```
## Contributing
### How to help
- For bugs and opinions, please [open an issue](https://github.com/gbrlsnchs/radix/issues/new)
- For pushing changes, please [open a pull request](https://github.com/gbrlsnchs/radix/compare)