General Tree Data Structure

A general tree data structure implementation in R.

CI ToolStatus for MasterStatus for Develop

This R package allows you to create trees with an arbitrary number of child nodes per parent node. It includes an depth first iterator, a function to plot the tree and a function to print the tree.

The current main benefit is that it allows to convert a R parsed object to a tree.

# Initialize the tree.
tree <- GeneralTree$new('root', '1.1')
# Add nodes.
tree$addNode('root', 'child1', '2.1')
tree$addNode('root', 'child2', '2.2')
tree$addNode('root', 'child3', '2.3')
# Print the tree

The output would be:

root : 1.1 --> child1 : 2.1
           |-> child2 : 2.2
           \-> child3 : 2.3

There are two ways to iterate through the tree depth first. The first uses an internal mechanism whereas the second allows the data structure to be hooked in the foreach and iterator packages.

The benefit of this approach is that you do not require dependencies on foreach and iterator.

i <- tree$iterator()
while (!is.null(i)) {
    i <- tryCatch(i$nextElem(), error = function(e) NULL)

Using the foreach and iterator packages permits you to write shorter code, as the following example shows:

itx <- iter(tree, by = "id")
numbers_in_tree <- foreach(i = itx, .combine = c) %do% c(i)

Note that the package has not yet been tested in a parallel environment.

How to install

The easiest way to install the package is by means of the devtools,

install_github('GeneralTree', username = 'abossenbroek')


The GeneralTree package is licensed under the Apache V2.0 license.


Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.0.1 by Anton Bossenbroek, 6 months ago

Browse source code at

Authors: Anton Bossenbroek [aut, cre]

Documentation:   PDF Manual  

Apache License (== 2.0) license

Depends on R6, utils

Suggests testthat, covr, knitr, DiagrammeR, iterators, foreach, tools, rmarkdown, microbenchmark, doParallel

See at CRAN