I\'ve checked binary tree class methods, and How to extract tree structure from ctree function? (which was helpful understanding S4 object structure and slots),
Below is the script I implemented to traverse the tree from a ctree object. I use the same example in the party package which is airct dataset.
require(party)
data(airquality)
traverse <- function(treenode){
if(treenode$terminal){
bas=paste("Current node is terminal node with",treenode$nodeID,'prediction',treenode$prediction)
print(bas)
return(0)
} else {
bas=paste("Current node",treenode$nodeID,"Split var. ID:",treenode$psplit$variableName,"split value:",treenode$psplit$splitpoint,'prediction',treenode$prediction)
print(bas)
}
traverse(treenode$left)
traverse(treenode$right)
}
airq <- subset(airquality, !is.na(Ozone))
airct <- ctree(Ozone ~ ., data = airq,
controls = ctree_control(maxsurrogate = 3))
plot(airct)
traverse(airct@tree)
This function, traverse, just traverses the tree in a depth-first order. You can change the order of the traversal by changing the recursive part.
Moreover, if you want to return other node characteristics, I would recommend checking the structure of the ctree object.
edit: Minor code revisions.