We had a test this week and on Monday lecture, we talked about mutating trees. The lab was also about tree mutation. In this post, I want to provide my analysis on trees (particularly relating to the lab)
Personally, I found the first bit of the course very straight forward. I see algorithms that can be applied to solve the problem. Okay done. Now, we are thinking about different algorithms and how they compare to one another. I think this makes one of the most interesting topics we ever touched on.
The lab was easy in some sense but I think it raised a lot of interesting questions. To fully (mathematically) answer the question, I needed to think really hard.
For lab 8, we had to tinker with insert. Here are my observations on insert:
1. the insertion only occurs at the "edge".
This is significant because if there were nodes with only one child, there is an opportunity for insert to work there. First, let's examine for how many positions insertion is "valid" for the following example (blue is the tree and red is after inserting). I see 2.
From this example, it could be seen that there are at maximum possible positions in general: one "sandwitched between nodes" and one at the leaf. It is also easy to reason out considering the >, < relations that must hold. Just as side node, also notice that the "sandwitched between nodes" option may not exist when the value is the new max or min.
2. based on the current algorithm there is a "best" order of insertion and it is to insert the "middle values" first.
By this I mean to say that if you have a list of 7 values, you would first insert the 3rd largest, then the (6-1)/2 = 2nd largest and 6th largest, and then finally the remaining values.
Try it with [1, 2, 3, 4, 5, 6, 7] and you will see that you get a tree with minimum height.
This can be reasoned based on 1. Since we know that insertion only happens at the edges, once we have a point in the "non-edge" you need certain numbers to populate that edge. With a sorted list, you will never actually get that "certain number".
3. One related observation is that the total number of valid ways to arrange a tree with node count n is the number of possible node arrangements.
So, you can easily see that given the shape of the tree, it is always possible to fill in the values fitting that tree in exactly one way (since exactly x values must be < the node at every point, where x is the number of nodes in the left subtree; as an aside, you and easily fill this tree with sorted values by travelling "inorder"). So what are the possible arrangement of nodes given size n?
I have yet to figure that out... but I have three leads:
a) thinking inductively, what are the possible combinations for size n-1; what is the number of child nodes where that 1 new node can be added for each? things to sort out with this approach are:
i) degenerate trees are created and I'm not sure how to account for them
ii) the number of child changes depending on the structure, how can I generalize?
b) at every node, you have at maximum 4 choices: to add to left, to add to right, to add to none, to add to both. Problems:
i) adding to none is not an always accessible choice.
i) again only the leafs get this kind of choice. all other nodes have no choice (already fixed)
In a sense, method b is the same as option a I think.
c) Somehow the order of the list will influence the tree arrangement. How?
i) again there are issues of degeneracies. It is easy to calculate the total possible list order tho.
If you have any hints on the latter, I would be really interested to hear about it.
I will not be posting my work on lab 8, since it should be obvious that you just need to shuffle the list for insert and when deleting randomly choose to "fill" using left or right (at least based on the hint)
No comments:
Post a Comment