Learn R Programming

CI ToolStatus for MasterStatus for Develop
Travis
AppVeyor
Codecov
Coveralls

GeneralTree

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.

Example

General use

require(GeneralTree)
# 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
tree

The output would be:

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

Iteration

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.

Internal

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

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

Foreach

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

require(iterators)
require(foreach)
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,

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

License

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

Copy Link

Version

Install

install.packages('GeneralTree')

Monthly Downloads

4

Version

0.0.1

License

Apache License (== 2.0)

Maintainer

Anton Bossenbroek

Last Published

September 8th, 2016

Functions in GeneralTree (0.0.1)

addChild

Add a child at a point in the tree.
branchToList

Convert a branch to a list.
as.GeneralTree.data.frame

Convert a data frame to a GeneralTree.
as.GeneralTree.expression

Convert a R parsed expression to a GeneralTree.
getChildNodes

Get all the child nodes below the current node.
generate_grViz

Create a DiagrammeR graph that represents the tree.
=.GeneralTree

Deep clone a General Tree.
getChildData

Get the data of the child nodes below the current node.
getChildId

Get the ids of the child nodes below the current node.
GeneralTree

A tree that can have multiple childeren per parent.
deep_clone.GeneralTree

Deep clone a General Tree.
deep_clone

Deep clone a General tree
have_child

Tell whether the current node has childeren.
have_parent

Returns true when the node has a parent.
is_last_sibling

Tell whether the current node is the last sibling.
delete

Delete all a node and all nodes below that node.
is_root

Tell whether the passed node is the root of the tree.
deleteId

Delete a node with a given id.
have_private_siblings

Tell whether the current node has private siblings.
have_siblings

Tell whether the current node has siblings.
getIterVal

Function heavily inspired by iterators package.
getIterVal.GeneralTreeIter

Function heavily inspired by iterators package.
isDiscovered

Informs whether the node has the discovered bit set.
isRootDiscovered

Returns whether the node has the is_root_discovered bit set.
root

Return the root of the node.
resetDiscoveredOnBranch

Reset the discover bit of all the nodes on the branch to FALSE.
iter.GeneralTree

Internal function heavily inspired by iterators package.
isSingletonTree

Informs whether the tree is a singleton tree.
<-.GeneralTree

Deep clone a General Tree.
setDiscovered

Set the discovered bit to a certain state.
setRootDiscovered

Set the root discovered bit to a certain state.
siblings

Return the private siblings of the node.
id

Return the id of the current node.
initialize

Initialize a General Tree object.
nextElem.GeneralTree

Internal function heavily inspired by iterators package.
nextElem.GeneralTreeIter

Internal function heavily inspired by iterators package.
parent

Get the parent of a node.
plot.GeneralTree

Plot a GeneralTree object.
dataWorker

Return the data associated with a node.
iteratorImpl

The implementation of the GeneralTree method iterator.
left_child

Return the left child of the node.
searchData

Search for an id in starting at a point in the tree and return the data matching the id.
searchNode

Search for an id in starting at a point in the tree and return the node matching the id.
toString

Convert a branch to a string.
as.GeneralTree

Convert an object to a GeneralTree.
branch_depth

Get the depth of a branch.
nodeInfoToString

Convert a node to string.
nextElemWorkerImpl

The implementation of the GeneralTree method nextElem.
print.GeneralTree

Print a GeneralTree object.
resetDiscovered

The implementation of the GeneralTree method iterator.
treeDepth

Get the depth of a tree.
as.data.frame.GeneralTree

Convert a GeneralTree to a data frame.
addSiblingNode

Add a node to the list of siblings of the current node.
addSibling

Add a sibling to the current node.