tree

package
v0.0.0-...-89aa834 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package tree provides functionality for working with tree-like data structures.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal[T comparable](root NodeMarshaler[T]) ([]byte, error)

Marshal serializes a tree structure into a byte slice.

func Stringify

func Stringify[T comparable](node Node[T], options *Options) string

Stringify converts a node to a string representation.

Types

type Node

type Node[T comparable] interface {
	// String returns the node's information as a string.
	String() string

	// Parent returns the parent node or zero value if it's the root.
	Parent() T

	// NumChild returns the number of child nodes.
	NumChild() int

	// GetChildByIndex returns the child node at the given index.
	// It returns the zero value of T if the index is out of range.
	GetChildByIndex(i int) T
}

Node represents a generic printable node in a tree structure.

type NodeMarshaler

type NodeMarshaler[T comparable] interface {
	Node[T]

	// Marshal serializes the node into a byte slice.
	Marshal() ([]byte, error)
}

NodeMarshaler extends Node with marshaling capability.

type Options

type Options struct {
	Prefix     string
	Parent     string // Default "│  "
	Space      string // Default "   "
	Branch     string // Default "├──"
	LastBranch string // Default "└──"
}

Options represents the configuration for stringifying a Node.

func (*Options) Fix

func (options *Options) Fix()

Fix ensures all options have valid values, using defaults where necessary.

Jump to

Keyboard shortcuts

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