嗯,这里是遍历树,并增加了家长的每个节点的功能。
addParent <- function(n, parent = NULL) {
n$parent <- parent
if (!n$terminal) {
n$left <- Recall(n$left, n)
n$right <- Recall(n$right, n)
}
n
}
# Example usage
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
[email protected] <- addParent([email protected])
leaves <- nodes(airct, unique(where(airct)))
leaves[[1]] # node 5
leaves[[1]]$parent # node 4
# And here's a way to "walk up the tree"
walkUp <- function(n) {
cat("Node", n$nodeID, "\n")
n <- n$parent
if(!is.null(n)) Recall(n)
}
for(n in leaves) { cat("---\n"); walkUp(n) }
BTW,我想我宁愿爬上树比走到它;-)
请阅读更仔细:“二叉树类{}党”。这不是二叉树包。 – 2012-01-10 04:37:30