bitreevis

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 15, 2023 License: Apache-2.0 Imports: 8 Imported by: 0

README

bitreevis

Go Reference

This repo is a tool which helps visualize binary tree structure in Golang. It is useful when debugging a program with binary tree structure.

Prerequisites

Installation

With Go module support (Go 1.11+), simply add the following import

import "github.com/ryanreadbooks/bitreevis"

to your code. Or you can use the following the get this package.

$ go get -u github.com/ryanreadbooks/bitreevis

Usage

Quick start

Details can be found in examples/svg_demo.go.

bitreevis.BiNode

In order to visualize your own binary tree, you should implement the bitreevis.BiNode interface. Then you can use bitreevis.VisAsSvg() function to visualize the binary tree in svg graphic format.

bitreevis.RenderOption

bitreevis.RenderOption is used to define the output style of the visualization. The size of nodes, color of nodes, the width of edges, etc. can be customized by setting option.

svg-demo

Private color for each node

If you want to paint different colors for different nodes. You should do the extra work after implementing the bitreevis.BiNode interface above, which is implementing the bitreevis.PaintableBiNode. For example, if you want to visualize a red-black tree, you can use private color for each node. See example.

rb-tree-svg

Learn more

Documentation

Overview

Package bitreevis implements tool whichs help visualize binary tree structure in golang.

Index

Constants

View Source
const (
	DefaultBackgroundColor    = "white"
	DefaultNodeColor          = "#868383"
	DefaultNodeStrokeWidth    = 1
	DefaultNodeFieldTextSize  = 16
	DefaultNodeFieldTextColor = "black"

	DefaultEdegLineWidth = 2
	DefaultEdgeColor     = "black"
	DefaultEdgeArrowSize = 2
	DefaultEdgeLineWidth = 2
)

Default settings for the graphic

Variables

This section is empty.

Functions

func BiNodeIsNil

func BiNodeIsNil(node BiNode) bool

BiNodeIsNil reports whether the BiNode interface is nil.

If BiNode interface itself is nil, BiNodeIsNil returns true. If BiNode interface itself is not nil, but the data of interface is nil, BiNodeIsNil returns true

func CalHeight

func CalHeight(root BiNode) int

CalHeight calculates the height of a binary tree in a recursive manner.

If root does not have any children, CalHeight returns 1. If root is nil, returns 0.

func CollectNodeByLevelOrder

func CollectNodeByLevelOrder(root BiNode) [][]BiNode

CollectNodeByLevelOrder collects all nodes in a binary tree in level order.

CollectNodeByLevelOrder returns nodes from top to down, from left to right, in the form of [][]BiNode

func VisAsSvg

func VisAsSvg(root BiNode, filename string, opt *RenderOption) error

VisAsSvg visualize the binary tree with given root in a svg graphic. The svg graphic is saved with the given filename.

Types

type BiNode

type BiNode interface {
	FieldHolder
	GetLeftChild() BiNode
	GetRightChild() BiNode
}

A BiNode represents a node in binary tree.

BiNode specify the minumum elements of a node in binary tree, which are left child, right child and content of node.

type FieldHolder

type FieldHolder interface {
	GetField() string
}

A FieldHolder is an interface for storing content of a node in binary tree.

GetField() should return a string .

type PaintableBiNode added in v1.1.0

type PaintableBiNode interface {
	BiNode

	// GetColor returns a color string for this node.
	GetColor() string
}

PaintableBiNode represents a node whose color is private. You can implement this interface if you want each of your node to have different colors.

type PlaceableNode

type PlaceableNode struct {
	Parent *PlaceableNode
	Left   *PlaceableNode
	Right  *PlaceableNode
	X      float32
	Y      float32
	Offset float32
	Thread bool
	Field  string
	Color  string
}

PlaceableNode represents a node which can be placed and rendered.

PlaceableNode's attributes follow the definition in algorithm of Tidier Drawings of Trees by Edward M. Reingold and John S. Tilford.

func NewPlaceableNode

func NewPlaceableNode(field string) *PlaceableNode

NewPlaceableNode returns a new *PlaceableNode with spefified field value.

func NewPlaceableTreeFromBiNode

func NewPlaceableTreeFromBiNode(root BiNode) *PlaceableNode

NewPlaceableTreeFromBiNode builds a tree made of placeableNode from a tree made of BiNode

func PerformLayout

func PerformLayout(root *PlaceableNode, siblingSeparation, nodeWidth, levelSeparation int) *PlaceableNode

func (*PlaceableNode) CollectNodes

func (p *PlaceableNode) CollectNodes() []*PlaceableNode

func (*PlaceableNode) CollectNodesWithStat

func (p *PlaceableNode) CollectNodesWithStat() (nodes []*PlaceableNode, limit *SizeLimitStat)

func (*PlaceableNode) GetField

func (p *PlaceableNode) GetField() string

func (*PlaceableNode) GetLeftChild

func (p *PlaceableNode) GetLeftChild() BiNode

Implement interface BiNode for placeableNode

func (*PlaceableNode) GetRightChild

func (p *PlaceableNode) GetRightChild() BiNode

func (*PlaceableNode) IsLeaf

func (p *PlaceableNode) IsLeaf() bool

type RenderOption

type RenderOption struct {
	// BackgroundColor specifies the global background color of the whole graphic.
	BackgroundColor string
	// BackgroundColor specifies the horizontal padding of the graphic on one size.
	HorizontalPadding int
	// BackgroundColor specifies the vertical padding of the graphic on one size.
	VerticalPadding int

	// SiblingSeparation specifies the minimum gap between two sibling nodes.
	SiblingSeparation int
	// LevelSeparation specifies the gap between two different levels.
	LevelSeparation int

	// NodeRadius specifies the radius of node.
	NodeRadius int
	// NodeColor specifies the color of nodes.
	NodeColor string
	// NodeColor specifies the color of leaf nodes, if not specified, NodeColor is used.
	NodeLeafColor string
	// NodeStrokeColor specifies the stroke color of nodes.
	NodeStrokeColor string
	// NodeStrokeWidth specifies the stroke-width of nodes.
	NodeStrokeWidth int
	// NodeFieldTextSize specifies the font size inside of node.
	NodeFieldTextSize int
	// NodeFieldTextColor specifies the color of font inside of node.
	NodeFieldTextColor string

	// EdgeLineWidth specifies the width of edges which connects nodes.
	EdgeLineWidth int
	// EdgeLineWidth specifies the color of edges which connects nodes.
	EdgeLineColor string
	// EdgeWithArrow specifies whether to use arrow at the end of edge
	EdgeWithArrow bool
	// EdgeArrowSize specifies the arrow size of edge
	EdgeArrowSize int
}

RenderOption is the options of graphics when rendering.

All units related to position and size are pixel(px).

type RenderResult

type RenderResult interface {
	// GetContent return an io.Reader which can be used to access the rendered data.
	GetContent() io.Reader

	// Save helps save the rendered data into a given file.
	Save(string) error

	// Error returns the error occurred during rendering. If no error occurs, it returns nil.
	Error() error
}

RenderResult contains rendered output from renderer.

RenderResult should contain a reader which can be read (for example, bytes, string, etc.) from the outsize. GetContent() method returns an io.Reader which provides access to the rendered data.

Save method can save the rendered result to the given filepath.

Error method provides a way for the user to check the error occurred during the rendering process. If no error occurs during rendering, Error should return nil.

type Renderer

type Renderer interface {
	Render(*PlaceableNode, RenderOption) RenderResult
}

Render is the interface which defines how to render the binary tree.

type SizeLimitStat

type SizeLimitStat struct {
	MinX float32
	MaxX float32
	MinY float32
	MaxY float32
}

type SvgRenderResult

type SvgRenderResult struct {
	// contains filtered or unexported fields
}

SvgRenderResult implements the RenderResult interface. It is the render result for SvgRenderer

func (*SvgRenderResult) Error

func (r *SvgRenderResult) Error() error

func (*SvgRenderResult) GetContent

func (r *SvgRenderResult) GetContent() io.Reader

GetContent returns the inner buffer of the rendered result The actual type of the returned io.Reader is strings.Reader.

func (*SvgRenderResult) Save

func (r *SvgRenderResult) Save(filename string) error

Save save the svg graphic into the given file.

type SvgRenderer

type SvgRenderer struct {
	Canvas svg.SVG
	// contains filtered or unexported fields
}

SvgRenderer is a renderer which can render the binary tree into svg format.

func NewSvgRenderer

func NewSvgRenderer() *SvgRenderer

NewSvgRenderer returns a new SvgRenderer.

func (*SvgRenderer) Render

func (sr *SvgRenderer) Render(root *PlaceableNode, option *RenderOption) RenderResult

Render performs rendering process for specified binary tree.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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