| 1 |
% Generated by roxygen2: do not edit by hand |
| 2 |
% Please edit documentation in R/nleaves.R |
| 3 |
\name{count_terminal_nodes} |
| 4 |
\alias{count_terminal_nodes} |
| 5 |
\title{Counts the number of terminal nodes (merging 0 nodes!)} |
| 6 |
\usage{ |
| 7 |
count_terminal_nodes(dend_node, ...) |
| 8 |
} |
| 9 |
\arguments{ |
| 10 |
\item{dend_node}{a dendrogram object for which to count its number of terminal nodes (leaves or 0 height nodes).} |
| 11 |
|
| 12 |
\item{...}{not used} |
| 13 |
} |
| 14 |
\value{ |
| 15 |
The number of terminal nodes (excluding the leaves of nodes of height 0) |
| 16 |
} |
| 17 |
\description{ |
| 18 |
This function counts the number of "practical" terminal nodes (nodes which are not leaves, but has 0 height to them are considered "terminal" nodes). |
| 19 |
If the tree is standard, that would simply be the number of leaves (only the leaves will have height 0). |
| 20 |
However, in cases where the tree has several nodes (before the leaves) with 0 height, |
| 21 |
the count_terminal_nodes counts such nodes as terminal nodes |
| 22 |
|
| 23 |
The function is recursive in that it either returns 1 if it reached a terminal node (either a leaf or a 0 height node), |
| 24 |
else: it will count the number of terminal nodes in each of its sub-nodes, sum them up, and return them. |
| 25 |
} |
| 26 |
\examples{ |
| 27 |
# define dendrogram object to play with: |
| 28 |
hc <- hclust(dist(USArrests[1:3, ]), "ave") |
| 29 |
dend <- as.dendrogram(hc) |
| 30 |
|
| 31 |
### |
| 32 |
# Trivial case |
| 33 |
count_terminal_nodes(dend) # 3 terminal nodes |
| 34 |
length(labels(dend)) # 3 - the same number |
| 35 |
plot(dend, |
| 36 |
main = "This is considered a tree \n with THREE terminal nodes (leaves)" |
| 37 |
) |
| 38 |
|
| 39 |
### |
| 40 |
# NON-Trivial case |
| 41 |
str(dend) |
| 42 |
attr(dend[[2]], "height") <- 0 |
| 43 |
count_terminal_nodes(dend) # 2 terminal nodes, why? see this plot: |
| 44 |
plot(dend, |
| 45 |
main = "This is considered a tree \n with TWO terminal nodes only" |
| 46 |
) |
| 47 |
# while we have 3 leaves, in practice we have only 2 terminal nodes |
| 48 |
# (this is a feature, not a bug.) |
| 49 |
} |