tree

package
v0.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2019 License: MIT Imports: 3 Imported by: 12

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

This section is empty.

Functions

func Print

func Print(root Node)

Print prints the formatted tree to standard output.

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 Sprint

func Sprint(root Node) string

Sprint returns the formatted tree.

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.

Types

type Node

type Node interface {
	// Data must return a value representing the node.
	Data() interface{}
	// Children must return a list of all child nodes of the node.
	Children() []Node
}

Node represents a node in a tree.

Jump to

Keyboard shortcuts

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