rbtree

package module
v0.0.0-...-82b3a40 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2017 License: BSD-3-Clause Imports: 0 Imported by: 0

README

Rbtree GoDoc

This is an implementation of Red-Black tree written by Golang.

Installation

With a healthy Go language installed, simply run go get github.com/HuKeping/rbtree

Example

All you have to do is implement a comparison function Less() bool for your Item which will be store in the Red-Black tree, here are some examples.

A simple case for int items.
package main

import (
	"fmt"
	"github.com/HuKeping/rbtree"
)

func main() {
	rbt := rbtree.New()

	m := 0
	n := 10

	for m < n {
		rbt.Insert(rbtree.Int(m))
		m++
	}

	m = 0
	for m < n {
		if m%2 == 0 {
			rbt.Delete(rbtree.Int(m))
		}
		m++
	}

	// 1, 3, 5, 7, 9 were expected.
	rbt.Ascend(rbt.Min(), Print)
}

func Print(item rbtree.Item) bool {
	i, ok := item.(rbtree.Int)
	if !ok {
		return false
	}
	fmt.Println(i)
	return true
}
A simple case for string items.
package main

import (
	"fmt"
	"github.com/HuKeping/rbtree"
)

func main() {
	rbt := rbtree.New()

	rbt.Insert(rbtree.String("Hello"))
	rbt.Insert(rbtree.String("World"))

	rbt.Ascend(rbt.Min(), Print)
}

func Print(item rbtree.Item) bool {
	i, ok := item.(rbtree.String)
	if !ok {
		return false
	}
	fmt.Println(i)
	return true
}
A quite interesting case for struct items.
package main

import (
	"fmt"
	"github.com/HuKeping/rbtree"
	"time"
)

type Var struct {
	Expiry time.Time `json:"expiry,omitempty"`
	ID     string    `json:"id",omitempty`
}

// We will order the node by `Time`
func (x Var) Less(than rbtree.Item) bool {
	return x.Expiry.Before(than.(Var).Expiry)
}

func main() {
	rbt := rbtree.New()

	var1 := Var{
		Expiry: time.Now().Add(time.Second * 10),
		ID:     "var1",
	}
	var2 := Var{
		Expiry: time.Now().Add(time.Second * 20),
		ID:     "var2",
	}
	var3 := Var{
		Expiry: var2.Expiry,
		ID:     "var2-dup",
	}
	var4 := Var{
		Expiry: time.Now().Add(time.Second * 40),
		ID:     "var4",
	}
	var5 := Var{
		Expiry: time.Now().Add(time.Second * 50),
		ID:     "var5",
	}

	rbt.Insert(var1)
	rbt.Insert(var2)
	rbt.Insert(var3)
	rbt.Insert(var4)
	rbt.Insert(var5)

	tmp := Var{
		Expiry: var4.Expiry,
		ID:     "This field is not the key factor",
	}

	// var4 and var5 were expected
	rbt.Ascend(rbt.Get(tmp), Print)
}

func Print(item rbtree.Item) bool {
	i, ok := item.(Var)
	if !ok {
		return false
	}
	fmt.Printf("%+v\n", i)
	return true
}

Documentation

Overview

Package rbtree implements operations on Red-Black tree.

Index

Constants

View Source
const (
	RED   = true
	BLACK = false
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Int

type Int int

func (Int) Less

func (x Int) Less(than Item) bool

type Item

type Item interface {
	Less(than Item) bool
}

type Iterator

type Iterator func(i Item) bool

type Node

type Node struct {
	Left   *Node
	Right  *Node
	Parent *Node
	Color  bool

	// for use by client.
	Item
}

Red-Black tree properties: http://en.wikipedia.org/wiki/Rbtree

  1. A node is either red or black
  2. The root is black
  3. All leaves (NULL) are black
  4. Both children of every red node are black
  5. Every simple path from root to leaves contains the same number of black nodes.

type Rbtree

type Rbtree struct {
	NIL *Node
	// contains filtered or unexported fields
}

Rbtree represents a Red-Black tree.

func New

func New() *Rbtree

New returns an initialized Red-Black tree

func (*Rbtree) Ascend

func (t *Rbtree) Ascend(pivot Item, iterator Iterator)

Ascend will call iterator once for each element greater or equal than pivot in ascending order. It will stop whenever the iterator returns false.

func (*Rbtree) AscendRange

func (t *Rbtree) AscendRange(ge, lt Item, iterator Iterator)

AscendRange will call iterator once for elements greater or equal than @ge and less than @lt, which means the range would be [ge, lt). It will stop whenever the iterator returns false.

func (*Rbtree) Delete

func (t *Rbtree) Delete(item Item) Item

func (*Rbtree) Descend

func (t *Rbtree) Descend(pivot Item, iterator Iterator)

Descend will call iterator once for each element less or equal than pivot in descending order. It will stop whenever the iterator returns false.

func (*Rbtree) First

func (t *Rbtree) First() *Node

func (*Rbtree) Get

func (t *Rbtree) Get(item Item) Item

func (*Rbtree) Init

func (t *Rbtree) Init() *Rbtree

func (*Rbtree) Insert

func (t *Rbtree) Insert(item Item) *Node

func (*Rbtree) InsertOrGet

func (t *Rbtree) InsertOrGet(item Item) Item

InsertOrGet inserts or retrieves the item in the tree. If the item is already in the tree then the return value will be that. If the item is not in the tree the return value will be the item you put in.

func (*Rbtree) Len

func (t *Rbtree) Len() uint

Number of nodes in the tree.

func (*Rbtree) Max

func (t *Rbtree) Max() Item

func (*Rbtree) Min

func (t *Rbtree) Min() Item

func (*Rbtree) Next

func (t *Rbtree) Next(x *Node) *Node

func (*Rbtree) Prev

func (t *Rbtree) Prev(x *Node) *Node

func (*Rbtree) Search

func (t *Rbtree) Search(item Item) *Node

TODO: This is for debug, delete it in the future

func (*Rbtree) SearchLe

func (t *Rbtree) SearchLe(item Item) *Node

func (*Rbtree) Tail

func (t *Rbtree) Tail() *Node

type String

type String string

func (String) Less

func (x String) Less(than Item) bool

type Uint32

type Uint32 uint32

func (Uint32) Less

func (x Uint32) Less(than Item) bool

Directories

Path Synopsis
example

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL