Documentation
¶
Overview ¶
Package tree provides a binary search tree implementation.
There are a few more functions in the coming, when I have time.
Index ¶
- type Node
- type Tree
- func (t *Tree) Contains(v Value) bool
- func (t *Tree) Delete(v Value) bool
- func (t *Tree) Find(v Value) *Node
- func (t *Tree) Height() int
- func (t *Tree) Init(vs []Value)
- func (t *Tree) Insert(v Value) *Node
- func (t *Tree) Len() int
- func (t *Tree) Max() *Node
- func (t *Tree) Min() *Node
- func (t *Tree) RandInit(vs []Value)
- func (t *Tree) Root() *Node
- func (t *Tree) Slice() []Value
- func (t *Tree) String() string
- type Value
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Node ¶
type Node struct {
// contains filtered or unexported fields
}
Node represents the internal nodes of a binary search tree.
If a node is not nil, then it must store a value, and may contain links to one or two subtrees (left and right). A node always has a pointer to the parent node, unless it is the root node.
func (*Node) Contains ¶
Contains searches the (sub)tree for the value v and returns true if it is found.
func (*Node) Find ¶
Find searches the (sub)tree for the value v and returns the node if it is found.
func (*Node) Height ¶
Height calculates the maximum height of the (sub)tree.
Note: a tree with 0 elements has a height of 0; a tree with 1 element must have a height of 1; a tree with 2 elements a height of 2; and a tree with n elements must have a height >= lg n.
func (*Node) Max ¶
Max returns the maximum value found in the (sub)tree, or nil if the (sub)tree is empty.
func (*Node) Min ¶
Min returns the minimum value found in the (sub)tree, or nil if the (sub)tree is empty.
type Tree ¶
type Tree struct {
// contains filtered or unexported fields
}
Tree represents a binary search tree.
The zero value of a Tree is a ready to use tree. Do note however, that you will need a pointer to the tree to use any of the methods.
func New ¶
New returns a new Tree to use, with lessFn as the function that gives a < b.
A Tree must be created with this function, otherwise trying to insert into it will cause a panic.
func (*Tree) Init ¶
Init initializes the tree with elements from vs in the given order.
In the general case, RandInit should be used, as Init cannot hope to have a logarithmic height if vs is sorted in any way.
func (*Tree) Insert ¶
Insert inserts a value v into the tree if it does not exist and returns the node containing it.
Note: if the value v already is in the tree, nothing happens. If you really want to know if it succeeded, check Len() before and after.
func (*Tree) RandInit ¶
RandInit initializes the tree with elements from vs in random order.
If you know that vs is already random, then you should use Init instead, as it will be slightly more efficient (about twice as fast).
Note: you are responsible for seeding math/rand.