https://github.com/hslam/rbtree
Package rbtree implements a red–black tree.
https://github.com/hslam/rbtree
go golang rbtree red-black-tree tree
Last synced: 29 days ago
JSON representation
Package rbtree implements a red–black tree.
- Host: GitHub
- URL: https://github.com/hslam/rbtree
- Owner: hslam
- License: mit
- Created: 2020-10-29T00:19:56.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-06-07T14:55:35.000Z (about 5 years ago)
- Last Synced: 2025-03-30T11:32:43.452Z (about 1 year ago)
- Topics: go, golang, rbtree, red-black-tree, tree
- Language: Go
- Homepage:
- Size: 93.8 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rbtree
[](https://pkg.go.dev/github.com/hslam/rbtree)
[](https://github.com/hslam/rbtree/actions)
[](https://codecov.io/gh/hslam/rbtree)
[](https://goreportcard.com/report/github.com/hslam/rbtree)
[](https://github.com/hslam/rbtree/blob/master/LICENSE)
Package rbtree implements a red–black tree.
#### Properties
In addition to the requirements imposed on a binary search tree the following must be satisfied by a **[red–black tree](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree "red–black-tree")**:
* Each node is either red or black.
* The root is black. This rule is sometimes omitted. Since the root can always be changed from red to black, but not necessarily vice versa, this rule has little effect on analysis.
* All leaves (NIL) are black.
* If a node is red, then both its children are black.
* Every path from a given node to any of its descendant NIL nodes goes through the same number of black nodes.
## [Benchmark](http://github.com/hslam/tree-benchmark "tree-benchmark")




## Get started
### Install
```
go get github.com/hslam/rbtree
```
### Import
```
import "github.com/hslam/rbtree"
```
### Usage
#### Example
```go
package main
import (
"fmt"
"github.com/hslam/rbtree"
)
func main() {
tree := rbtree.New()
str := item("Hello World")
tree.Insert(str)
fmt.Println(tree.Search(str))
tree.Delete(str)
}
type item string
func (n item) Less(b rbtree.Item) bool {
return n < b.(item)
}
```
#### Output
```
Hello World
```
#### Iterator Example
```go
package main
import (
"fmt"
"github.com/hslam/rbtree"
)
func main() {
tree := rbtree.New()
l := []Int{13, 8, 17, 1, 11, 15, 25, 6, 22, 27}
for _, v := range l {
tree.Insert(v)
}
iter := tree.Min()
for iter != nil {
fmt.Printf("%d\t", iter.Item())
iter = iter.Next()
}
}
type Int int
func (a Int) Less(b rbtree.Item) bool {
return a < b.(Int)
}
```
#### Red–Black Tree

#### Output
```
1 6 8 11 13 15 17 22 25 27
```
### License
This package is licensed under a MIT license (Copyright (c) 2020 Meng Huang)
### Author
rbtree was written by Meng Huang.