GithubHelp home page GithubHelp logo

obitech / go-trees Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 0.0 52 KB

BST, Red-Black-Tree & Interval Tree in Golang

License: MIT License

Go 99.69% Makefile 0.31%
golang interval-tree redblack-tree binary-search-tree

go-trees's Introduction

go-tree: Tree implementations in Golang

gopherbadger-tag-do-not-edit

package bst

Implements a Binary Search Tree.

package redblack

Implements a Red-Black-Tree.

Example

tree := NewRedBlackTree()

// Operations on arbitrary keys are allow as long as the type implements they 
// Key interface.
type myInt int

func (i myInt) Less(v Key) bool {
    return i < v.(myInt)
}

// Insert items.
tree.Upsert(myInt(5), "test")
tree.Upsert(myInt(10), "foo")
tree.Upsert(myInt(15), 42)

// Search for an item.
fmt.Println(tree.Search(15)) 

// Replace a payload.
tree.Upsert(myInt(15), "bar")

Benchmarks

goos: darwin
goarch: amd64
pkg: github.com/obitech/go-tree/redblack
BenchmarkRBTree_Upsert-8                 2012917               775 ns/op              31 B/op          0 allocs/op
BenchmarkRBTree_Search10_000-8          11450182               102 ns/op               0 B/op          0 allocs/op
BenchmarkRBTree_Search100_000-8          5394513               235 ns/op               0 B/op          0 allocs/op
BenchmarkRBTree_Search1_000_000-8         411782              2487 ns/op             117 B/op          3 allocs/op
BenchmarkRBTree_Delete10_000-8          25755091                43.7 ns/op             0 B/op          0 allocs/op
BenchmarkRBTree_Delete100_000-8         23051653                46.5 ns/op             0 B/op          0 allocs/op
BenchmarkRBTree_Delete1_000_000-8          37518             26899 ns/op            1291 B/op         43 allocs/op

package interval

Implements an Interval tree

Example

tree := NewIntervalTree()

nov, err := NewInterval(newTime(t, "2020-Nov-01"), newTime(t, "2020-Nov-02"))
if err != nil {
	panic(err)
}


feb, err := NewInterval(newTime(t, "2020-Feb-01"), newTime(t, "2020-Feb-02"))
if err != nil {
	panic(err)
}

dec, err := NewInterval(newTime(t, "2020-Dec-01"), newTime(t, "2020-Dec-02"))
if err != nil {
	panic(err)
}

tree.Upsert(nov)
tree.Upsert(feb)
tree.Upsert(dec)

search1, err := NewInterval(newTime(t, "2020-Feb-01"), newTime(t, "2021-Feb-02"))

r, err := tree.FindFirstOverlapping(search)
if err != nil {
	fmt.Println("no overlapping interval found for %s", search1)
}

r, err = tree.FindAllOverlapping(search)
if err != nil {
    fmt.Println("no overlapping interval found for %s", search1)
}

Benchmarks

TODO

go-trees's People

Contributors

obitech avatar pscheid92 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.