Documentation ¶
Overview ¶
Package tree implements traversal of tree structures (parent node with child nodes).
The variant types and corresponding functions supports a tree structure (node with child nodes).
You can:
- represent a tree as a 1-dimensional slice
- walk a tree
- write tree out to an input stream as text.
This code has now been updated to remove reference to parents, and prevent the cyclic structure. This allows us to encode a Tree using gob.
REPRESENTATION AS SLICE ¶
A tree can be representated as a 1-dimensional slice.
Given a tree like:
ROOT a1 a2 b1 c1 c2 c3 d1 d2 d3 b2 b3 a3
And
DESC, RET
Encode to an an array like:
a1, a2, DESC, b1, DESC, c1, c2, DESC, d1, d2, d3, RET, RET, b2, b3, RET, a3
Decode same array to a tree as above.
WALKING ¶
You can walk a slice or write it out to an input stream as text.
During a walk, all the walk events are made available to the walk function.
Walk Algorithm
Perform EnterEvent operation if breadth first, Perform VisitEvent operation for i=0 to n-1 do Walk child[i], if present Perform BackUpEvent operation if depth first, Perform VisitEvent operation
Code Generation ¶
any_tree.go is the generic version of the tree.
We auto-generate the specific versions for an int64 tree and a uint64 tree.
You can update gen.sh to auto-generate for other types.
Run `go generate` (or gen.sh) in this directory to re-generate them.
Make sure you run this each time any_tree.go is updated.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( DefDesc = new(struct{}) DefAsc = new(struct{}) )
var ( Uint64DefDesc = uint64(math.MaxUint64 - 1) Uint64DefAsc = uint64(math.MaxUint64 - 2) )
var ( //StopDescent is returned as an error during a Walk to signify //that the Walk should not proceed below this node for this edge. StopDescent = errors.New("Stop Descent") )
Functions ¶
This section is empty.
Types ¶
type Codec ¶
type Codec struct { Desc interface{} Asc interface{} }
A Codec is used to encode or decode a Node into/from a slice. Slices are easier to store and manage, especially in some app environments e.g. Google App Engine.
func (Codec) DecodeChildren ¶
Decodes the children of Node n from the slice. Note that the "root" ie n is not reflected in the slice.
func (Codec) EncodeChildren ¶
Encodes the children of Node n into the slice . Note that the "root" ie n is not reflected in the slice. It's okay to pass a nil slice for starters.
type Int64Codec ¶
A Int64Codec is used to encode or decode a Int64Node into/from a slice. Slices are easier to store and manage, especially in some app environments e.g. Google App Engine.
func NewInt64Codec ¶
func NewInt64Codec() Int64Codec
func (Int64Codec) DecodeChildren ¶
func (c Int64Codec) DecodeChildren(n *Int64Node, vals []int64)
Decodes the children of Int64Node n from the slice. Note that the "root" ie n is not reflected in the slice.
func (Int64Codec) EncodeChildren ¶
func (c Int64Codec) EncodeChildren(n *Int64Node, vals []int64) []int64
Encodes the children of Int64Node n into the slice . Note that the "root" ie n is not reflected in the slice. It's okay to pass a nil slice for starters.
type Int64Node ¶
func (*Int64Node) Copy ¶
Copy returns a deep copy of Int64Node n. This just means that a copy of the node and copies of the children are returned. However, the "Value" in each node stays the same.
type Int64WalkFunc ¶
Function called for each event during a TreeWalk. Return StopDescent if you don't want to walk down a node further.
type Node ¶
type Node struct { //don't keep a reference to the parent. Value interface{} Children []*Node }
func (*Node) Copy ¶
Copy returns a deep copy of Node n. This just means that a copy of the node and copies of the children are returned. However, the "Value" in each node stays the same.
type Uint64Codec ¶
A Uint64Codec is used to encode or decode a Uint64Node into/from a slice. Slices are easier to store and manage, especially in some app environments e.g. Google App Engine.
func NewUint64Codec ¶
func NewUint64Codec() Uint64Codec
func (Uint64Codec) DecodeChildren ¶
func (c Uint64Codec) DecodeChildren(n *Uint64Node, vals []uint64)
Decodes the children of Uint64Node n from the slice. Note that the "root" ie n is not reflected in the slice.
func (Uint64Codec) EncodeChildren ¶
func (c Uint64Codec) EncodeChildren(n *Uint64Node, vals []uint64) []uint64
Encodes the children of Uint64Node n into the slice . Note that the "root" ie n is not reflected in the slice. It's okay to pass a nil slice for starters.
type Uint64Node ¶
type Uint64Node struct { //don't keep a reference to the parent. Value uint64 Children []*Uint64Node }
func (*Uint64Node) Copy ¶
func (n *Uint64Node) Copy() (n2 *Uint64Node)
Copy returns a deep copy of Uint64Node n. This just means that a copy of the node and copies of the children are returned. However, the "Value" in each node stays the same.
func (*Uint64Node) Walk ¶
func (n *Uint64Node) Walk(depthFirst bool, reverse bool, fn Uint64WalkFunc) (err error)
Walk Uint64Node n, depth first or breadth first, and call fn for every event during the Walk.
type Uint64WalkFunc ¶
type Uint64WalkFunc func(n *Uint64Node, evt Event) (err error)
Function called for each event during a TreeWalk. Return StopDescent if you don't want to walk down a node further.