Skip to content
/ rbt Public

Red-black tree implementation in Go, for use as sorted associatve container

License

Notifications You must be signed in to change notification settings

pantonov/rbt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status GoDoc

RbMap - Golang red-black tree implementation of ordered map

RbMap is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function. Search, removal, and insertion operations have logarithmic complexity.

Documentation: Godoc

Installation

  • Stable:
    go get gopkg.in/pantonov/rbt.v1

  • Latest:
    go get github.com/pantonov/rbt

Example

    r := rbt.NewRbMap(func(k1, k2 interface{}) bool {
        return k1.(string) < k2.(string)
    })
    r.Insert("c", 1)
    r.Insert("b", 2)
    r.Insert("a", 3)
    r.Insert("d", 4)

    // iterate over map in ascending key order. Note that Key is a method,
    // to prevent in-place modification, while Value accessed directly
    for n := r.First(); n != nil; n = n.Next() {
        fmt.Printf("%s -> %d\n", n.Key(), n.Value)
    }
    // Find value by key
    fmt.Printf("Value for %s is: %d\n", "b", r.Find("b"))

    // More node-level (iperator) operations. You'll want to check return values
    // for nil's, which are omitted here for brevity
    n := r.FindNode("a")
    fmt.Printf("Key next to 'a': %s\n", n.Next().Key())
    r.DeleteNode(n.Next())
    n = r.FindNode("a")
    fmt.Printf("Now, key for node next to 'a': %s\n", n.Next().Key())

About

Red-black tree implementation in Go, for use as sorted associatve container

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages