Documentation ¶
Overview ¶
Package tree provides implementation to get and print formatted tree.
Example:
import "github.com/shivamMg/ppds/tree" // a tree node. type Node struct { data int children []*Node } func (n *Node) Data() interface{} { return strconv.Itoa(n.data) } // cannot return n.children directly. // https://github.com/golang/go/wiki/InterfaceSlice func (n *Node) Children() (c []tree.Node) { for _, child := range n.children { c = append(c, tree.Node(child)) } return } // n1, n2 := Node{data: "b"}, Node{data: "c"} // n3 := Node{"a", []*Node{&n1, &n2}} // tree.Print(&n3)
Index ¶
Constants ¶
const ( BoxVer = "│" BoxHor = "─" BoxVerRight = "├" BoxDownLeft = "┐" BoxDownRight = "┌" BoxDownHor = "┬" BoxUpRight = "└" // Gutter is number of spaces between two adjacent child nodes. Gutter = 2 )
Variables ¶
var ErrDuplicateNode = errors.New("duplicate node")
ErrDuplicateNode indicates that a duplicate Node (node with same hash) was encountered while going through the tree. As of now Sprint/Print and SprintWithError/PrintWithError cannot operate on such trees.
This error is returned by SprintWithError/PrintWithError. It's also used in Sprint/Print as error for panic for the same case.
FIXME: create internal representation of trees that copies data
Functions ¶
func Print ¶
func Print(root Node)
Print prints the formatted tree to standard output. To handle ErrDuplicateNode use PrintWithError.
func PrintHr ¶
func PrintHr(root Node)
PrintHr prints the horizontal formatted tree to standard output.
func PrintHrn ¶
func PrintHrn(root Node)
PrintHrn prints the horizontal-newline formatted tree to standard output.
func PrintWithError ¶
PrintWithError prints the formatted tree to standard output.
func SprintWithError ¶
SprintWithError returns the formatted tree.
Types ¶
type Node ¶
type Node interface { // Data must return a value representing the node. It is stringified using "%v". // If empty, a space is used. Data() interface{} // Children must return a list of all child nodes of the node. Children() []Node }
Node represents a node in a tree. Type that satisfies Node must be a hashable type.