@gemma-the-husky
This can be done, but I will explain why doing this is problematic, unreliable/ambiguous, and not very useful.
I tend to load thee types of trees.
1. Single table that is self referencing. This means the number of possible levels is unknown and can continue to grow. (Systems and subsystems, BOM,Family/bilogoical Trees, organizations, etc.)
2. Related child tables like you would have in subforms (sub-sub forms). (Customer - Orders - OrderDetails). In order to build this you will know ahead of time the maximum potential level of the tree which is simply the number of tables involved. I guess this is not really a "Tree" in traditional sense.
3. A combination that includes related tables and one or more self referencing table.
The below is case 2 where there are three tables involved.
In this case it just easier and more full proof to do other methods. I cannot think of a generic case where this would be useful.
In this case it will be known ahead of time how many possible levels you could have. Unfortunately that does not mean how many levels are actually populated. You have three tables involved thus three is the max. But there may be times where there is not yet any branch with 3 levels. If you are populating the tree maybe there is no information yet in the order details table. It is just far more likely that you care what type of node it is (Customer, Order, Order Details) vs is it the last of the potential nodes. The Identifier stored in the tag tells what type of node. Any code you write would probably be specific to that database so this would be very reliable.
Secondly, even if there are records in all tables, I do not load the whole table or read through it. When I load, I load the top level Nodes (and one more) only. They get dynamically loaded when I expand a node. The next level is loaded (and so on). This makes loading big trees far faster. However there is an option to load the whole thing.
Since often I do not always span all the data to load the tree (and do not want to) I would have to separately write another method that spans the data (at the table level) and dynamically determine the longest branch. Again that fails if all the potential tables are not yet populated.
The single self referencing table below is Case 1.
The potential levels are dynamic and unknown.
The only way to determine the longest branch is to span all the data recursively. Again, I often do not do that and only load the next level down when click on it. Again I could write another method to span the data for the purpose of determining the longest branch, but I cannot see what use that would be. But even doing this would be problematic. Spanning a large self referencing table can be very expensive and time consuming. You could do it once on load, but what happens if you add, move, or delete a node in the tree or at the table level. You would have to potentially span again or write some pretty involved code to determine if you need to re-span again.
However there are lots of helpful methods already to answer similar questions.
1. Determine what table a node comes from
2. Determine all the children of a node
3. Determine level of a given node
4. Determine all the descendants of a node (hierrarchy)
Descendants: So for a given node you can determine the depth of the branch below by determining that Edison Cole is the highest level.
Now there are many cases where I do span the entire data and store information about each and every node. For example if each node had cost data in it may roll it up and store in a table to then build a form / report.
IMO it could be expensive, unreliable / ambiguous, and not real useful.