Description Usage Format Value Methods Active methods Examples
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.
1 |
R6Class object.
Object of R6Class with methods for creating a general
tree.
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.
rootReturns the root of a node.
left_childReturns the left child of a node.
siblingsReturns the left sibling of a node.
idReturns the id of a node.
have_childReturns TRUE if the node has childs and
FALSE otherwise.
have_siblingsReturns TRUE if the node has siblings and
FALSE otherwise.
is_last_siblingReturns TRUE if the node is the last siblings and
FALSE otherwise.
have_private_siblingsReturns TRUE if the node has a
private field siblings set and FALSE otherwise.
have_parentReturns TRUE if the node has a
parent field set and FALSE otherwise.
dataReturns the data of the node.
idReturns the id of the node.
is_rootReturns TRUE if the node is the
root and FALSE otherwise.
parentReturn the parent of the node.
treeDepthReturns the depth of the tree.
branch_depthReturns the depth of the branch.
isSingletonTreeReturns TRUE if the tree contains only
a single element and FALSE otherwise.
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)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.