- ## find where is the node in `X':
- ## below 19 is: nchar("TheNodeWhereToGraftY") - 1
- for (i in 1:nchar(X)) {
- if ("TheNodeWhereToGraftY" == substr(X, i, i + 19))
- break
- i <- i + 1
+ x$root.edge <- x$root.edge - position
+ x$edge <- rbind(c(next.node, x$edge[1]), x$edge)
+ ROOT <- next.node
+ if (wbl) x$edge.length <- c(position, x$edge.length)
+ }
+ if (yHasNoRootEdge) {
+ j <- which(y$edge[, 1] == y$edge[1])
+ y$edge[j, 1] <- ROOT
+ } else y$edge[1] <- ROOT
+ x$edge <- rbind(x$edge, y$edge)
+ if (wbl)
+ x$edge.length <- c(x$edge.length, y$edge.length)
+ }, { # case = 2
+ if (position) {
+ x$edge[i, 2] <- next.node
+ x$edge <- rbind(x$edge[1:i, ], c(next.node, where), x$edge[-(1:i), ])
+ if (wbl) {
+ x$edge.length[i] <- x$edge.length[i] - position
+ x$edge.length <- c(x$edge.length[1:i], position, x$edge.length[-(1:i)])
+ }
+ i <- i + 1L
+ if (yHasNoRootEdge) {
+ j <- which(y$edge[, 1] == y$edge[1])
+ y$edge[j, 1] <- x$edge[i, 1]
+ } else y$edge[1] <- x$edge[i, 1]
+ } else {
+ if (yHasNoRootEdge) x$edge[i, 2] <- y$edge[1]
+ else {
+ ## the root edge of y is fused with the terminal edge of x
+ if (wbl) y$edge.length[1] <- y$edge.length[1] + x$edge.length[i]
+ y$edge[1] <- x$edge[i, 1]
+ ## delete i-th edge in x:
+ x$edge <- x$edge[-i, ]
+ i <- i - 1L
+ if (wbl) x$edge.length <- x$edge.length[-i]