This question has been asked a few times but they are all in different languages (for example, see here for java and here for python, but Im am trying to achieve this in R.
I have a dataframe of trees. My trees are ordered by depth-first-left-side traversal, as in the code below:
df <- data.frame(
var = c("x2", NA, NA, "x1", NA, "x2", "x2", NA, NA, NA, "x2", NA, "x10", NA, NA, NA, "x1", NA, NA, "x5", NA, NA),
node = c(1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 1, 1, 2, 3, 1, 2, 3),
terminal = c(FALSE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, FALSE, TRUE, TRUE, FALSE, TRUE, TRUE),
iteration = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2),
treeNum = c(1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 2, 2, 2, 3, 3, 3),
stringsAsFactors = FALSE
)
The structure of the dataframe is as follows:
var = variable name (if terminal node or stump, then this is just NA)
node = node number
terminal = if the node is a terminal node or not.
iteration = Iteration number
treeNum = the tree number
As you can see, my dataframe contains 3 trees over 2 iterations.
For clarity, if we look at a single tree (eg, tree number 2 in iteration 1), it would look like the following image (where the nodes are numbered following the traversal method):
And the depth for this tree (ordered in the depth-first-left-side method) would be: 0,1,1,2,3,3,2
Ultimately, I want to make a depth column to add to my dataframe, but Im struggling to make the code work. Any suggestions as to how I could do this?

I guess if you explicitly do have which nodes are terminal and which are not, then it is possible to decode. So here's a function that can calculate depth with that extra information
It looks like the tree you drew is treeNum==2 at iteration==1. We can run the function on that tree with
(you can subtract 1 from the vector if you prefer to start at 0).
We can run this on all the trees with
which returns
So while it's not generally that a depth first pre-order traversal can allow you to reconstruct the tree, it can if you explicitly know which nodes are terminal and which are not.