Tree: Create a Tree Object

Description Usage Arguments Format Details Value Fields Methods Author(s) Examples

Description

Tree is a class of R6. You can use it to create a binary tree via diffrent ways.

Usage

1
Tree$new(elem = NULL, left = NULL, right = NULL, node.ind = 1)

Arguments

elem

The element in the tree. More details show in help(Elem). Default NULL.

left

The left child of the Tree. Also a Tree oject. Default NULL.

right

The right child of the Tree. Also a Tree oject. Default NULL.

node.ind

The index of the current node in Tree. Default 1 for the root node.

Format

R6Class object.

Details

See details in description of each field or method.

Value

Object of R6Class, Object of Tree.

Fields

See Arguments.

Methods

generateTree(tree.mat, node.ind = 1)

Generate a Tree from a tree matrix which just likes the result of randomForest::getTree().

  • tree.mat - A tree matrix which can be obtained from randomForest::getTree(). Node that it must have a column named node.ind. See Examples.

  • node.ind - The index of the current node in Tree. Default 1 for the root node. For most purposes, don't need to change it.

updateChildren(l, r)

Update childre of the current node.

  • l - The left child.

  • r - The right child.

isLeaf()

TRUE if current node is a leaf node otherwise FALSE.

size()

Return size of Tree. For each node, size + 1.

numLeaves()

Return how many leaf nodes in Tree.

depth()

Return max depth of Tree. If only a root node, depth is 0.

draw()

Draw the Tree.

Author(s)

Quan Gu

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
ta <- Tree$new("abc", NULL, NULL)
tb <- Tree$new(1, Tree$new(36), Tree$new(3))
tc <- Tree$new(89, tb, ta)
td <- Tree$new("guquan", tc, tb)

tb$draw()
tb$size()
tb$depth()
tc$draw()
tc$depth()
tc$right$updateChildren(Tree$new("666"), Tree$new(999))
tc$right$right$updateChildren(Tree$new("666"), Tree$new(999))
tc$draw()
td$draw()

# generate a Tree from randomForest
if (!require(randomForest)) install.packages("randomForest")
library(randomForest)
dat <- iris; dat[,5] <- as.integer(dat[,5])
rf <- randomForest(factor(Species) ~ ., data=dat)
treemat <- getTree(rf, 1, labelVar = F)
treemat <- cbind(treemat, node.ind = 1:nrow(treemat))
tree <- Tree$new()
tree$generateTree(treemat)
tree$draw()

ZJUguquan/OnlineRandomForest documentation built on May 20, 2019, 2:57 p.m.