Ordered Binary Tree of Strings

本小妞迷上赌 提交于 2019-12-11 05:46:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!