Left balanced binary trees

白昼怎懂夜的黑 提交于 2019-12-30 06:55:51

问题


I am reading a book on data structures and it says that a left balanced binary tree is a tree in which the leaves only occupy the leftmost positions in the last level.

This seemed a little vague to me. Does this mean that leaves are only on the left side of a root and are distributed throughout the whole level, or leave exists only on the left side of the entire tree. Exactly what constitute left balanced?

I am not sure if my guess even covers any of the answer, so if anyone could help, it would be greatly appreciated :-).


回答1:


You can think of a left-balanced binary tree as a balanced binary tree where the left sub-tree of each node is filled before the right sub-tree. In more informal terms, this is a tree wherein the nodes at the bottom-most level are all on the left side of the entire tree.

Take this tree for example:

This tree is balanced, but not left-balanced. If node 67 were removed, however, the tree would be left-balanced.




回答2:


It seems to me that the definition of left balanced binary tree is the same of complete binary tree.




回答3:


I don't really know the answer, but based on the description in the book it sounds to me like this...

For starters, lets think of it this way. Every "row" in the tree has a number, starting at zero and counting up. The row with the highest number is considered to be the last level.

Remember that leaf nodes are nodes without any children. So the tree meets the condition that every leaf node in the last level must be on the left, like so:

          50
       /     \
     /         \
    35         70
   /  \       /  \
 10    34    57  90
 /     /        /
9     7        78

If we were to add a "98" as the right-child of 90 or a "59" as the right-child of 57, then this tree would no longer be left-balanced.


Edit: After reading Brandon E Taylor's answer, you should definitely not choose this one. After looking it over and reading the description again, not only does his make more sense, but it fits the description much better.




回答4:


In addition to Brandon E Taylor's answer, a left balanced binary tree is a binary tree that when represented in an array there shouldn't exist any gaps in between the elements representing the tree nodes.

for a tree of size 6 for example, here are some possible array representations with the following criteria

1- let _ denote an empty slot in the array
2- let i be the index of a slot in an array (i is 1-based)
2- for any parent at index i the left child is at index (2*i) and the right child is at index (2*i)+1

1 2 3 4 _ _ <-  left balanced 
6 3 2 4 1   <-  left balanced
4 2 1 _ 6   <- not left balanced

to elaborate further more, let's represent user265312's answer:

50 35 70 10 34 57 90 9 _ 7 _ _ _ 78 _ <- not left balanced

meanwhile Brandon's answer (after removing node 67) doesn't violate the left balancing rule:

50 17 72 12 23 54 76 9 14 19 _ _ _ _ _ <- left balanced


来源:https://stackoverflow.com/questions/7275586/left-balanced-binary-trees

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