问题
These values are entered into an ordered binary tree: Mercury
, Venus
, Earth
, Mars
, Jupiter
, Saturn
and Uranus
.
The resulting binary tree is supposed to be this.
Mercury
/ \
Earth Venus
\ /
Jupiter Saturn
\ \
Mars Uranus
Is there any reason for this order? Shouldn't Jupiter be under the Venus branch?
回答1:
By "ordered binary tree", I assume you mean a binary search tree. As long as the tree satisfies the following criteria:
1. The key in a node is greater than (or equal to) any key stored in its left subtree.
2. The key in a node is less than (or equal to) any key stored in its right subtree.
then the exact structure of the tree depends on the order in which the keys are added and the exact algorithm used to construct the tree.
However, you indicate that you believe that Jupiter should occur in the subtree of Venus. What is your ordering criteria? The tree you show is valid if the names are being compared alphabetically.
来源:https://stackoverflow.com/questions/11701602/ordered-binary-tree-of-strings