Balanced Binary Search Tree for numbers

拈花ヽ惹草 提交于 2020-01-26 04:45:45

问题


I wanted to draw a balanced binary search tree for numbers from 1 to 20.

             _______10_______
            /                \
        ___5___                15
       /       \             /    \
      3         8          13      18
     / \       / \        /  \     /  \
    2   4     7   9     12    14  17    19
   /          /          /        /     
   1         6          11       16

Is the above tree correct and balanced?


回答1:


In answer to your original question as to whether or not you need to first calculate the height, no, you don't need to. You just have to understand that a balanced tree is one where the height difference between the tallest and shortest node is zero or one, and the simplest way to achieve this is to ensure that you always pick the midpoint of the possible list, when populating the top node in a sub-tree.

Your sample tree is balanced since all leaf nodes are either at the bottom or next-to-bottom level, hence the difference in heights between any two leaf nodes is at most one.

To create a balanced tree from the numbers 1 through 20 inclusive, you can just make the root entry 10 or 11 (the midpoint being 10.5 for those numbers), so that there's an equal quantity of numbers in either sub-tree.

Then just do that recursively for each sub-tree. On the lower side of 10, 5 is the midpoint:

           10
          /  \
         5    11-thru-19 sub-tree
        / \
1-thru-4   6-thru-9
sub-tree   sub-tree

Just expand on that and you'll end up with something like:

         _______10_______
        /                \
    ___5___               15
   /       \             /  \
  2         7          13    17
 / \       / \        /     /  \
1   3     6   8     11    16    18    <- depth of highest leaf node
     \         \      \           \
      4         9      12          19 <- depth of lowest leaf node
                                                     ^
                                                     |
                                               Difference is 1

The midpoint can be found at the number where the difference between quantities above and below that numbers is one or zero. For the whole list of numbers 1 through 20 inclusive, there are nine less than 10 and ten greater than 10 (or, if you chose 11 as the midpoint, the quantities are ten and nine).

The difference between your sample and mine is probably to do with the fact that I preferred to pick the midpoint by rounding down where there was a choice (meaning my right sub-trees tend to be "heavier"). Because your left sub-trees are heavier, you appear to have rounded up.

After choosing 10 as the initial midpoint, there's no leeway on the left sub-tree, you have to choose 5 since it has four above and below it. Any other midpoint would result in a difference of at least two between the two halves (for example, choosing 4 as the midpoint would have the two halves of size three and five). This can still give you a balanced sub-tree depending on the data but it's "safer" to choose the midpoint.



来源:https://stackoverflow.com/questions/16766159/balanced-binary-search-tree-for-numbers

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