I have an assignment on optimal binary search trees and some questions came up while doing it. I have found many of the links online helpful (just from a Google search) but I was wondering…
Why must the keys must be initially sorted?
If I get a lower cost (for an optimal BST) when the keys are not sorted, does that mean there must be an error in my code?
Must an optimal BST be complete/perfect? (using the Wikipedia definitions of complete and perfect)
A perfect binary tree is a full binary tree in which all leaves are at the same depth or same level. [1] (This is ambiguously also called a complete binary tree.)
A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible. [2]
For the last question, I would assume that an optimal tree must be complete/perfect, but some of the applets online lead me to believe otherwise. I cannot reason why though…
They don’t. In fact, unless you’re using a self-balancing tree, it’s better if you add the keys to the tree in random order, because the tree will end up more balanced.
Not unless you’re coding up a self-balancing tree (your self-balancing algorithm is not working).
Yes. In order to get the fastest possible search for a given tree, all of the tree’s nodes must be equally distributed; i.e. the tree must be as short as possible.