tree

package
Version: v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2019 License: MIT Imports: 4 Imported by: 6

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

View Source
const (
	BoxVer       = "│"
	BoxHor       = "─"
	BoxVerRight  = "├"
	BoxDownLeft  = "┐"
	BoxDownRight = "┌"
	BoxDownHor   = "┬"
	BoxUpRight   = "└"
	// Gutter is number of spaces between two adjacent child nodes.
	Gutter = 2
)

Variables

View Source
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 added in v0.0.1

func PrintWithError(root Node) error

PrintWithError prints the formatted tree to standard output.

func Sprint

func Sprint(root Node) string

Sprint returns the formatted tree. To handle ErrDuplicateNode use SprintWithError.

func SprintHr

func SprintHr(root Node) (s string)

SprintHr returns the horizontal formatted tree.

func SprintHrn

func SprintHrn(root Node) (s string)

SprintHrn returns the horizontal-newline formatted tree.

func SprintWithError added in v0.0.1

func SprintWithError(root Node) (string, error)

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.

Jump to

Keyboard shortcuts

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