Coder Social home page Coder Social logo

go-hierarchy-tree's Introduction

Hierarchy tree for Go

github.com/chippyash/go-hierarchy-tree/tree

Go: 1.18

What

Provides a basic but flexible hierarchical tree data structure together with an implementation of a Visitor pattern for tree manipulation.

docs/tree-graph.puml

How

import "github.com/chippyash/go-hierarchy-tree/tree"

For Development

Setup

  • clone repository
  • cd to project directory
  • go get ./...

Usage

Create a node
import "github.com/chippyash/go-hierarchy-tree/tree"

//node with no value or children
node := tree.NewNode(nil, nil)
//node with some value
node := tree.NewNode("foo", nil)
//node with children
child1 := tree.NewNode("bar", nil)
child2 := tree.NewNode("baz", nil)
children := []tree.NodeIFace{child1, child2}
node := tree.NewNode("foo", &children)
Getting and setting a node value

A node can have any value. The value of a node is an interface{}

val := tree.NewNode(nil, nil).
		SetValue("foo").
		GetValue()
Adding one or more children

You can add children when you construct a node (see above) or afterwards.

child1 := tree.NewNode(1, nil)
child2 := tree.NewNode(2, nil)
children := []tree.NodeIFace{child1, child2}

childNodes := tree.NewNode(0, nil).
		SetChildren(children...).
		GetChildren()

childNodes = tree.NewNode(0, nil).
AddChild(child1).
AddChild(child2).
GetChildren()
Removing child nodes
node.RemoveChild(child1)
node.RemoveAllChildren()
Testing nodes
node.IsLeaf()  //true if node has no children
node.IsRoot()  //true if node is the root node, i.e. no parent
node.IsChild() //true if node has a parent
Parents, siblings and ancestors
node.SetParent(someOtherNode)  //set the parent to the node
node.GetParent()            //return parent of this node - nil if node is root
node.GetSiblings()          //returns nodes with same parent
node.GetSiblingsAndSelf()   //returns nodes with same parent including self
node.GetAncestors()         //parents and grandparents
node.GetAncestorsAndSelf()  //self, parents and grandparents
Traversing a tree

The tree implements the Visitor pattern Accept method.

Pre-order traversal
/**
 *    root
 *    /|\
 *   a b c
 *  /| |
 * d e f
 */
visitor := tree.NewPreOrderVisitor()
result := root.Accept(visitor)
//returns []tree.NodeIFace{root, a, d, e, b, f, c}
Post-order traversal
/**
 *    root
 *    /|\
 *   a b c
 *  /| |
 * d e f
 */
visitor := tree.NewPostOrderVisitor()
result := root.Accept(visitor)
//returns []tree.NodeIFace{d, e, a, f, b, c, root}
Gathering leaves
/**
 *    root
 *    /|\
 *   a b c
 *  /| |
 * d e f
 */
visitor := tree.NewLeafVisitor()
result := root.Accept(visitor)
//returns []tree.NodeIFace{d, e, f, c}
Filtering
/**
 *    root
 *    /|\
 *   a b c
 *  /| |
 * d e f
 */
visitor := tree.NewFilterVisitor(func(n tree.NodeIFace) bool {
return n.GetValue() == "e"
})
result := root.Accept(visitor)
//returns []tree.NodeIFace{e}

see Filter Test for other examples

Node information
node.GetDepth()   //returns the distance from the current node to the root
node.GetHeight()  //returns the height of the tree whose root is this node
node.GetSize()    //returns the number of nodes in the tree rooted at this node

Testing

go test ./...

Before you do a PR

  • Update the readme if necessary

References

go-hierarchy-tree's People

Contributors

akzincsystems avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.