{"id":20254819,"url":"https://github.com/chippyash/go-hierarchy-tree","last_synced_at":"2026-05-10T11:36:23.817Z","repository":{"id":53915812,"uuid":"521920094","full_name":"chippyash/go-hierarchy-tree","owner":"chippyash","description":"Basic but useful hierarchical tree, a form of directed graph","archived":false,"fork":false,"pushed_at":"2022-08-06T12:40:34.000Z","size":16,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-14T03:34:14.863Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chippyash.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-08-06T10:51:48.000Z","updated_at":"2022-09-21T19:56:55.000Z","dependencies_parsed_at":"2022-08-13T04:10:20.266Z","dependency_job_id":null,"html_url":"https://github.com/chippyash/go-hierarchy-tree","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2Fgo-hierarchy-tree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2Fgo-hierarchy-tree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2Fgo-hierarchy-tree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chippyash%2Fgo-hierarchy-tree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chippyash","download_url":"https://codeload.github.com/chippyash/go-hierarchy-tree/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241705919,"owners_count":20006399,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-14T10:35:01.961Z","updated_at":"2026-05-10T11:36:23.262Z","avatar_url":"https://github.com/chippyash.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hierarchy tree for Go\n## github.com/chippyash/go-hierarchy-tree/tree\n\nGo: 1.18\n\n## What\nProvides a basic but flexible hierarchical tree data structure together with an implementation of a \nVisitor pattern for tree manipulation.\n\n![docs/tree-graph.puml](docs/tree-graph-The_tree_is_a_Directed_Graph.png)\n\n## How\n\n`import \"github.com/chippyash/go-hierarchy-tree/tree\"`\n\n### For Development\n#### Setup\n\n- clone repository\n- cd to project directory\n- `go get ./...`\n\n#### Usage\n##### Create a node\n```go\nimport \"github.com/chippyash/go-hierarchy-tree/tree\"\n\n//node with no value or children\nnode := tree.NewNode(nil, nil)\n//node with some value\nnode := tree.NewNode(\"foo\", nil)\n//node with children\nchild1 := tree.NewNode(\"bar\", nil)\nchild2 := tree.NewNode(\"baz\", nil)\nchildren := []tree.NodeIFace{child1, child2}\nnode := tree.NewNode(\"foo\", \u0026children)\n```\n\n##### Getting and setting a node value\nA node can have any value. The value of a node is an interface{}\n```go\nval := tree.NewNode(nil, nil).\n\t\tSetValue(\"foo\").\n\t\tGetValue()\n```\n\n##### Adding one or more children\nYou can add children when you construct a node (see above) or afterwards.\n```go\nchild1 := tree.NewNode(1, nil)\nchild2 := tree.NewNode(2, nil)\nchildren := []tree.NodeIFace{child1, child2}\n\nchildNodes := tree.NewNode(0, nil).\n\t\tSetChildren(children...).\n\t\tGetChildren()\n\nchildNodes = tree.NewNode(0, nil).\nAddChild(child1).\nAddChild(child2).\nGetChildren()\n```\n\n##### Removing child nodes\n```go\nnode.RemoveChild(child1)\nnode.RemoveAllChildren()\n```\n\n##### Testing nodes\n```go\nnode.IsLeaf()  //true if node has no children\nnode.IsRoot()  //true if node is the root node, i.e. no parent\nnode.IsChild() //true if node has a parent\n```\n\n##### Parents, siblings and ancestors\n```go\nnode.SetParent(someOtherNode)  //set the parent to the node\nnode.GetParent()            //return parent of this node - nil if node is root\nnode.GetSiblings()          //returns nodes with same parent\nnode.GetSiblingsAndSelf()   //returns nodes with same parent including self\nnode.GetAncestors()         //parents and grandparents\nnode.GetAncestorsAndSelf()  //self, parents and grandparents\n\n```\n##### Traversing a tree\nThe tree implements the Visitor pattern Accept method.\n###### Pre-order traversal\n```go\n/**\n *    root\n *    /|\\\n *   a b c\n *  /| |\n * d e f\n */\nvisitor := tree.NewPreOrderVisitor()\nresult := root.Accept(visitor)\n//returns []tree.NodeIFace{root, a, d, e, b, f, c}\n```\n\n###### Post-order traversal\n```go\n/**\n *    root\n *    /|\\\n *   a b c\n *  /| |\n * d e f\n */\nvisitor := tree.NewPostOrderVisitor()\nresult := root.Accept(visitor)\n//returns []tree.NodeIFace{d, e, a, f, b, c, root}\n```\n\n###### Gathering leaves\n```go\n/**\n *    root\n *    /|\\\n *   a b c\n *  /| |\n * d e f\n */\nvisitor := tree.NewLeafVisitor()\nresult := root.Accept(visitor)\n//returns []tree.NodeIFace{d, e, f, c}\n```\n\n##### Filtering\n```go\n/**\n *    root\n *    /|\\\n *   a b c\n *  /| |\n * d e f\n */\nvisitor := tree.NewFilterVisitor(func(n tree.NodeIFace) bool {\nreturn n.GetValue() == \"e\"\n})\nresult := root.Accept(visitor)\n//returns []tree.NodeIFace{e}\n```\nsee [Filter Test](tree/filter_test.go) for other examples\n\n##### Node information\n```go\nnode.GetDepth()   //returns the distance from the current node to the root\nnode.GetHeight()  //returns the height of the tree whose root is this node\nnode.GetSize()    //returns the number of nodes in the tree rooted at this node\n```\n#### Testing\n\n`go test ./...`\n\n#### Before you do a PR\n\n- Update the readme if necessary\n\n\n\n## References\n\n- [Github](https://github.com/chippyash/go-hierarchy-tree)\n- [Based on Nicmart/Tree](https://github.com/nicmart/Tree)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fgo-hierarchy-tree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchippyash%2Fgo-hierarchy-tree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchippyash%2Fgo-hierarchy-tree/lists"}