- STRING <-
- if (output.tree.names) paste(tree.names, "(", sep = "") else "("
- br <- which(phy$edge[, 1] == n + 1)
- for (j in br) {
- desc <- phy$edge[j, 2]
- if (desc > n) add.internal(desc)
- else add.terminal(j)
- if (j != br[length(br)]) cp(",")
+
+ ## borrowed from phangorn:
+ parent <- phy$edge[, 1]
+ children <- phy$edge[, 2]
+ kids <- vector("list", n + phy$Nnode)
+ for (i in 1:length(parent))
+ kids[[parent[i]]] <- c(kids[[parent[i]]], children[i])
+
+ ind <- match(1:max(phy$edge), phy$edge[, 2])
+
+ LS <- 4*n + 5
+ if (brl) LS <- LS + 4*n
+ if (nodelab) LS <- LS + n
+ STRING <- character(LS)
+ k <- 1
+ if (output.tree.names) cp(tree.names)
+ cp("(")
+ k <- 2
+ getRoot <- function(phy)
+ phy$edge[, 1][!match(phy$edge[, 1], phy$edge[, 2], 0)][1]
+ root <- getRoot(phy) # replaced n+1 with root - root has not be n+1
+ desc <- kids[[root]]
+ for (j in desc) {
+ if (j > n) add.internal(j)
+ else add.terminal(ind[j])
+ if (j != desc[length(desc)]) cp(",")