R/get_target_nodes.R

Defines functions is_target_node get_target_nodes_main get_target_nodes

#' @export
get_target_nodes <- function(tree) {
  root <- tree$get_root_node()
  get_target_nodes_main(root)
}

get_target_nodes_main <- function(node) {
  child_nodes <- node$get_children()
  if(length(child_nodes) == 0) {
    NULL
  } else {
    if(is_target_node(node)) {
      list(node)
    } else {
      result <- list()
      for(child_node in child_nodes) {
        result <- c(result, get_target_nodes_main(child_node))
      }
      result
    }
  }
}

is_target_node <- function(node) {
  all(sapply(node$get_children(), function(node) length(node$get_children()) == 0))
}
hoxo-m/mergetree documentation built on May 16, 2017, 12:56 a.m.