A tree is defined as a graph having three properties:
- a single root node with no parent
- all non-root nodes have exactly one parent
- any node may have any number of children
The nodes of the tree are assumed to have an id or key by which parent and child relationships can be defined.
Only a node entity has been coded which can represent both root and non root nodes. How the root node is identified is up to you.
The idea to build this library came from the necessity to graphically visualise data that can be represented in a tree structure and analyse it visually.
Example:
- You have an API that returns a JSON with a tree like structure and you want to easily visualise and map this data to understand what the structure looks like.
- You are parsing data that is in a random order (parents are not sorted) and you want to build a tree of the data so the structure of the data can be visualised on screen.
- With
go get
:
go get -u github.com/lcucurachi/go-tree
Call NewNode()
constructor with the datatype that you want to store in the nodes and
create a root node that has a distinct id and title (root and root is okay).
If your data is unsorted you cannot link nodes immediately as the parent node might not
exist yet. Look at examples/generic
for an example of how to correctly build a tree
of unsorted data.
Similar to the unsorted case, build your nodes with NewNode()
and then use AddChildren()
and MoveNodeParent()
to build the tree.
Or create nodes and assign children directly using NewNodeWithChildren()
.
- Implement a more efficient tree search so that the library can be used to build and search bigger trees more efficiently.
- More tests.
We welcome all contributions, whether it's bug reports, feature requests, or pull requests.