GeneralTree
R6Class object.
R6Class with methods for creating a general
tree.
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.
addNode(parent_id, id, data)searchData(id)id. This method returns the data associated with the node.searchNode(id)id. This method returns the node.searchBranch(id)getSiblingNodes()getSiblingId()getSiblingData()getChildNodes(recursive = FALSE)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)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)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)id. All
child nodes will also be deleted.delete()iterator()nextElem()toString(what = c("id", "data"), string_prepend = "")rootleft_childsiblingsidhave_childTRUE if the node has childs and
FALSE otherwise.have_siblingsTRUE if the node has siblings and
FALSE otherwise.is_last_siblingTRUE if the node is the last siblings and
FALSE otherwise.have_private_siblingsTRUE if the node has a
private field siblings set and FALSE otherwise.have_parentTRUE if the node has a
parent field set and FALSE otherwise.dataidis_rootTRUE if the node is the
root and FALSE otherwise.parenttreeDepthbranch_depthisSingletonTreeTRUE if the tree contains only
a single element and FALSE otherwise.# 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)[1] "child.3.4"# # Print the tree tree0 : root --> 1 : child.0.1 |-> 2 : child.0.2 \-> 3 : child.0.3 --> 4 : child.3.4# # 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)