data.tree (version 0.7.11)

Node: Create a data.tree Structure With Nodes

Description

Node is at the very heart of the data.tree package. All trees are constructed by tying together Node objects.

Usage

# n1 <- Node$new("Node 1")

Arguments

Format

An R6Class generator object

Fields

children

A list of child Nodes

parent

The node's parent Node

Methods

Node$new(name)

Creates a new Node called name. Often used to construct the root when creating trees programmatically.

AddChild(name)

Creates a new Node called name and adds it to this Node as a child.

AddChildNode(node)

Adds a Node as a child.

AddSibling(name)

Creates a new Node called name and adds it after this Node as a sibling.

AddSiblingNode(sibling)

Adds a new Node after this Node, as a sibling.

RemoveChild(name)

Remove the child Node called name from a Node and returns it.

RemoveAttribute(name, stopIfNotAvailable)

Removes attribute called name from this Node. Gives an error if stopIfNotAvailable and the attribute does not exist.

Climb(...)

Find a node with path ..., where the ... arguments are the names of the Nodes, or other field values.

Navigate(path)

Find a node by relative path

FindNode(name)

Find a node with name name. Especially useful if AreNamesUnique is TRUE

Get(attribute, ..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NULL, format = NULL, inheritFromAncestors = FALSE, simplify = c(TRUE, FALSE, "array", "regular"))

Traverses the tree and collects values along the way.

Do(fun, ..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NUL)

Traverses the tree and call fun on each node.

Set(..., traversal = c("pre-order", "post-order", "in-order", "level", "ancestor"), pruneFun = NULL, filterFun = NULL)

Traverses the tree and assigns the args along the way, recycling the args.

Sort(attribute, ..., decreasing = FALSE, recursive = TRUE

Sort children of a node with respect to an attribute (field, method, active, function)

Revert(recursive = TRUE)

Revert the sort order of a node

Prune(pruneFun)

Prune a tree. The pruneFun takes a node as its first argument, and returns TRUE if the node should be kept, FALSE otherwise

Actives (aka Properties)

name

Gets or sets the name of a Node. For example Node$name <- "Acme"

parent

Gets or sets the parent Node of a Node. Only set this if you know what you are doing, as you might mess up the tree structure!

children

Gets or sets the children list of a Node. Only set this if you know what you are doing, as you might mess up the tree structure!

siblings

Returns a list of the siblings of this Node

fields

Gets the names of the set properties of a Node

fieldsAll

Gets the names of the set properties of a Node or any of its sub-Nodes

isLeaf

Returns TRUE if the Node is a leaf, FALSE otherwise

isRoot

Returns TRUE if the Node is the root, FALSE otherwise

count

Returns the number of children of a Node

totalCount

Returns the total number of Nodes in the tree

path

Returns a vector of mode character containing the names of the Nodes in the path from the root to this Node

pathString

Returns a string representing the path to this Node, separated by backslash

levelName

Returns the name of the Node, preceded by level times '*'. Useful for printing.

leafCount

Returns the number of leaves are below a Node

leaves

Returns a list containing all the leaf Nodes

level

Returns an integer representing the level of a Node. For example, the root has level 1.

height

Returns max(level) of any of the Nodes of the tree

averageBranchingFactor

Returns the average number of crotches below this Node

root

Returns the root Node of a Node's tree

Details

Assemble Node objects into a data.tree structure and use the traversal methods to set, get, and perform operations on it. Typically, you construct larger tree structures by converting from data.frame, list, or other formats.

See Also

For more details see the data.tree documentations, or the data.tree vignette: vignette("data.tree")

Examples

Run this code
# NOT RUN {
library(data.tree)
acme <- Node$new("Acme Inc.")
accounting <- acme$AddChild("Accounting")$
              AddSibling("Research")$
              AddChild("New Labs")$
              parent$
              AddSibling("IT")$
              AddChild("Outsource")
print(acme)

# }

Run the code above in your browser using DataCamp Workspace