There are a few very interesting things about a tree.
First is that a tree is made up of many other trees, which are called subtrees. I previously posted a question about what problems lend themselves to being solved by recursion. The fact that trees are made of smaller trees makes it obvious that that problems involving trees are good for solving by recursion.
Another interesting thing kind of related to the above is that the root only needs direct access to its children. At the end though, it can figure everything about the tree. In a sense, the tree has emergent properties.
But more interesting I think is the fact that the child nodes are not "aware" of their parent. I'm not sure if this is just an implementation factor, but the child has no knowledge of the parent in the following implementation (partial) provided in course lectures copied here for easy viewing:
It is actually impossible for them to point to the same tree as shown below (since a copy is made). However, when such a graph (not really a tree anymore) is built, many of the recursive functions intended to act on it will work. Of course for some things like counting leaves, it will behave a bit strangely and behave like tree in Figure 2. And I think it could be easily accounted for by checking if they are the same object etc.
![]() |
| Figure 1 |
But the above (figure 1) is definitely different from the representation below (Figure 2).
![]() |
| Figure 2 |
I wonder if figure 1 like structure has a name (more specific than a graph)? it's like a tree but it can have multiple parents or is it not special enough for a name...
EDIT: I was a little wrong about figure 1 being impossible in this implementation. see my follow up post


No comments:
Post a Comment