https://github.com/liwnn/treerank
This Go package provides an implementation of ranking-list based on red-black tree
https://github.com/liwnn/treerank
go ranking ranking-list red-black-tree
Last synced: 3 months ago
JSON representation
This Go package provides an implementation of ranking-list based on red-black tree
- Host: GitHub
- URL: https://github.com/liwnn/treerank
- Owner: liwnn
- Created: 2022-08-03T10:53:36.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-19T11:08:36.000Z (about 3 years ago)
- Last Synced: 2025-08-10T22:24:01.970Z (4 months ago)
- Topics: go, ranking, ranking-list, red-black-tree
- Language: Go
- Homepage:
- Size: 22.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# TreeRank
This Go package provides an implementation of ranking-list based on red-black tree. It is in-memory-only and does not store any data.
## Usage
All you have to do is to implement a comparison function Less(Item) bool for your Item which will be store in the tree, here are some examples.
``` go
package main
import (
"fmt"
"github.com/liwnn/treerank"
)
type User struct {
Name string
Score int
}
func (u User) Key() string {
return u.Name
}
func (u User) Less(than treerank.Item) bool {
if u.Score == than.(User).Score {
return u.Name < than.(User).Name
}
return u.Score < than.(User).Score
}
func main() {
tr := treerank.New()
// Add
tr.Add("Hurst", User{Name: "Hurst", Score: 88})
tr.Add("Peek", User{Name: "Peek", Score: 100})
tr.Add("Beaty", User{Name: "Beaty", Score: 66})
// Rank
rank := tr.Rank("Hurst", true)
fmt.Printf("Hurst's rank is %v\n", rank) // expected 2
// Range
tr.Range(0, 3, true, func(key string, v treerank.Item, rank int) bool {
fmt.Printf("%v's rank is %v\n", v.(User).Name, rank)
return true
})
// Remove
tr.Remove("Peek")
// Rank
rank = tr.Rank("Hurst", true)
fmt.Printf("Hurst's rank is %v\n", rank) // expected 1
}
```
Output:
```
Hurst's rank is 2
Peek's rank is 1
Hurst's rank is 2
Beaty's rank is 3
Hurst's rank is 1
```