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

Description Usage Format Value Methods Active methods Examples

Description

This class allows to create a tree with multiple childs per node. The data as well as the id are left totally to the choice of the user and can even be different.

Usage

1

Format

R6Class object.

Value

Object of R6Class with methods for creating a general tree.

Methods

addNode(parent_id, id, data)

Add a new node to the tree. The new node will be a child of parent_id and have an id and data.

searchData(id)

Search an node in the tree that has an id equal to id. This method returns the data associated with the node.

searchNode(id)

Search an node in the tree that has an id equal to id. This method returns the node.

searchBranch(id)

Search for a node in a particular branch of the tree. The function returns a node.

getSiblingNodes()

Get all the siblings of this node in a list. The results will not include the node itself.

getSiblingId()

Get all the sibling ids in a list. The results will not include the node itself.

getSiblingData()

Get all the sibling data in a list. The results will not include the node itself.

getChildNodes(recursive = FALSE)

Get the child nodes from the current branch. On default the function will only return one level deep. If recursive is set to TRUE, also childs in nested branches will be returned. The childs will all be returned in a list.

getChildId(recursive = FALSE)

Get the ids from all the child nodes. If recursive is set to TRUE, also ids from childs in nested branches will be returned. The ids will all be returned in a list.

getChildData(recursive = FALSE)

Get the data from all the child nodes. If recursive is set to TRUE, also data from childs in nested branches will be returned. The data will all be returned in a list.

deleteId(id)

Delete a node with id equal to id. All child nodes will also be deleted.

delete()

Delete the current node and all childs. Should not be called directly.

iterator()

Get an iterator to iterate through the tree in a depth first search.

nextElem()

Get the next element in a depth first search. Before using this function always create an iterator.

toString(what = c("id", "data"), string_prepend = "")

Creates a string representation of the node. Note that id and data should work with paste to work correctly. All branches will also be returned to the string.

Active methods

root

Returns the root of a node.

left_child

Returns the left child of a node.

siblings

Returns the left sibling of a node.

id

Returns the id of a node.

have_child

Returns TRUE if the node has childs and FALSE otherwise.

have_siblings

Returns TRUE if the node has siblings and FALSE otherwise.

is_last_sibling

Returns TRUE if the node is the last siblings and FALSE otherwise.

have_private_siblings

Returns TRUE if the node has a private field siblings set and FALSE otherwise.

have_parent

Returns TRUE if the node has a parent field set and FALSE otherwise.

data

Returns the data of the node.

id

Returns the id of the node.

is_root

Returns TRUE if the node is the root and FALSE otherwise.

parent

Return the parent of the node.

treeDepth

Returns the depth of the tree.

branch_depth

Returns the depth of the branch.

isSingletonTree

Returns TRUE if the tree contains only a single element and FALSE otherwise.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# Create a tree
tree <- GeneralTree$new(0, "root")
tree$addNode(0, 1, "child.0.1")
tree$addNode(0, 2, "child.0.2")
tree$addNode(0, 3, "child.0.3")
tree$addNode(3, 4, "child.3.4")
tree$searchData(4)

#
# Print the tree
tree

#
# Example how to iterate through the tree in a depth first iteration.
i <- tree$iterator()
while (!is.null(i)) {
   i$setData(paste("id:", i$id, " : data", i$data))
   i <- tryCatch(i$nextElem(), error = function(e) NULL)
}

# An example with the foreach package.
require(iterators)
require(foreach)
itx <- iter(tree, by = "id")
numbers_in_tree <- foreach(i = itx, .combine = c) %do% c(i)

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

GeneralTree documentation built on Jan. 15, 2017, 6:17 p.m.