rbtree

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2019 License: MIT Imports: 0 Imported by: 0

README

go-rbtree

Package rbtree provides an iterative (not recursive) red-black tree with obvious semantics and powerful, resettable iteration.

This package was born out of a need to modify elements during tree iteration. Most packages do not provide an obvious way to efficiently "reset" a node or an iterator. To aid in this need, this package primarily operates on the basis of nodes rather than node items.

For more information about a red-black tree, and to understand the implementation, see Wikipedia.

Documentation

GoDoc

Documentation

Overview

Package rbtree provides an iterative (not recursive) red-black tree with obvious semantics and powerful, resettable iteration.

This package provides the easy ability to modify nodes on the fly, but note that this comes with the easy footgun of messing up iteration. If you modify a tree during iteration, you will likely need to reset your iterator. The node under the iterator may no longer be where you expect.

For more information about a red-black tree, and to understand the implementation, see Wikipedia:

https://en.wikipedia.org/wiki/Red%E2%80%93black_tree

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Item

type Item interface {
	// Less returns whether an item is less than another item.
	Less(other Item) bool
}

Item is the interface required for implenting to use a tree.

The red-black tree does support duplicate items, but one is not guaranteed which duplicate is found when looking to remove one of many duplicates. Essentially, there is no stable ordering to duplicate items.

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

Iter iterates over a tree. This returns nodes in the tree to support easy node deletion.

Note that if you modify the tree during iteration, you need to reset the iterator or stop iterating.

Example
package main

import (
	"fmt"

	"github.com/twmb/go-rbtree"
)

type Int int

func (l Int) Less(r rbtree.Item) bool {
	return l < r.(Int)
}

func main() {
	var r rbtree.Tree

	for i := 0; i < 10; i++ {
		r.Insert(Int(i)) // Int provides Less on int
	}

	// Declaring iter here to show we can reset it later.
	var iter rbtree.Iter

	// We can start iterating _at_ the max.
	fmt.Println("iterating down...")
	for iter = rbtree.IterAt(r.Max()); iter.Ok(); iter.Left() {
		fmt.Println(iter.Item())
	}

	// Or, we can start iterating just before the min so that
	// our first call to Right will start at the min.
	iter.Reset(rbtree.Into(r.Min()))
	fmt.Println("iterating up...")
	for {
		next := iter.Right()
		if next == nil { // when right returns nil, we are done
			break
		}
		fmt.Println(next.Item)
	}

	// Putting it together...
	fmt.Println("iterating down very inefficiently...")
	for it := rbtree.IterAt(r.Min()); it.Ok(); it.Right() {
		if it.Item() == r.Max().Item {
			fmt.Println(it.Item())
			r.Delete(it.Node())
			it.Reset(rbtree.Into(r.Min()))
		}
	}

}
Output:

iterating down...
9
8
7
6
5
4
3
2
1
0
iterating up...
0
1
2
3
4
5
6
7
8
9
iterating down very inefficiently...
9
8
7
6
5
4
3
2
1
0

func IterAt

func IterAt(on *Node) Iter

IterAt returns an iterator for a tree starting on the given node.

Note that modifications to the tree during iteration may invalidate the iterator and the iterator may need resetting.

func (Iter) Item

func (i Iter) Item() Item

Item returns the item in the node the iterator is currently on. This will panic if the iterator is not on a node.

This is shorthand for i.Node().Item.

func (*Iter) Left

func (i *Iter) Left() *Node

Left moves the iterator to the left (more towards the min) and returns the resulting node it lands on, or nil if this moves past the left side of the tree. This will panic if not on a node.

func (Iter) Node

func (i Iter) Node() *Node

Node returns the node the iterator is currently on, if any.

func (Iter) Ok

func (i Iter) Ok() bool

Ok returns whether the iterator is on a node.

func (Iter) PeekLeft

func (i Iter) PeekLeft() *Node

PeekLeft peeks at the next left node the iterator can move onto without moving the iterator. This will panic if not on a node.

For maximal efficiency, to move left after a left peek, use Reset with the peeked node.

func (Iter) PeekRight

func (i Iter) PeekRight() *Node

PeekRight peeks at the next right node the iterator can move onto without moving the iterator. This will panic if not on a node.

For maximal efficiency, to move right after a right peek, use Reset with the peeked node.

func (*Iter) Reset

func (i *Iter) Reset(on *Node)

Reset resets the iterator to be on the given node.

func (*Iter) Right

func (i *Iter) Right() *Node

Right moves the iterator to the right (more towards the max) and returns the resulting node it lands on, or nil if this moves past the right side of the tree. This will panic if not on a node.

type Node

type Node struct {
	Item Item
	// contains filtered or unexported fields
}

Node is an element in a tree containing a user provided item.

func Into

func Into(n *Node) *Node

Into returns a fake node that to both the Left or the Right will be the given node. This can be used in combination with iterating to reset iteration to the given node.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

Tree is a red-black tree.

func (*Tree) Delete

func (t *Tree) Delete(n *Node) *Node

Delete removes a node from the tree, returning the resulting node that was removed. Note that you cannot reuse the "deleted" node for anything; you must use the returned node. The node for deletion may actually remain in the tree with a different item.

func (*Tree) Find

func (t *Tree) Find(needle Item) *Node

Find finds a node in the tree equal to the needle, if any.

func (*Tree) FindOrInsert

func (t *Tree) FindOrInsert(needle Item) *Node

FindOrInsert finds a node equal to the needle, if any. If the node does not exist, this inserts a new node into the tree with new and returns that node.

func (*Tree) FindWith

func (t *Tree) FindWith(cmp func(*Node) int) *Node

FindWith calls cmp to find a node in the tree. To continue searching left, cmp must return negative. To continue searching right, cmp must return positive. To stop at the node containing the current item to cmp, return zero.

If cmp never returns zero, this returns nil.

This can be used to find an arbitrary node meeting a condition.

func (*Tree) FindWithOrInsertWith

func (t *Tree) FindWithOrInsertWith(
	find func(*Node) int,
	insert func() Item,
) *Node

FindWithOrInsertWith calls cmp to find a node in the tree following the semantics described in FindWith. If the cmp function never returns zero, this inserts a new node into the tree with new and returns that node.

func (*Tree) Fix

func (t *Tree) Fix(n *Node) *Node

Fix removes a node from the tree and reinserts it. This can be used to "fix" a node after the item has been updated, removing some minor garbage. This returns the updated node.

This is shorthand for t.Reinsert(t.Delete(n)).

func (*Tree) Insert

func (t *Tree) Insert(i Item) *Node

Insert inserts an item into the tree, returning the new node.

func (*Tree) Len

func (t *Tree) Len() int

Len returns the current size of the tree.

func (*Tree) Max

func (t *Tree) Max() *Node

Max returns the maximum node in the tree, or nil if empty.

func (*Tree) Min

func (t *Tree) Min() *Node

Min returns the minimum node in the tree, or nil if empty.

func (*Tree) Reinsert

func (t *Tree) Reinsert(n *Node)

Reinsert inserts a node into the tree.

This function is provided to aid in garbage reduction / ease of use when deleting, updating, and reinserting items into a tree. The only value required in the node is the item. All other fields are set inside this function.

Jump to

Keyboard shortcuts

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