how to determine a balanced or perfectly balanced Binary search tree ( just from the picture )

南笙酒味 提交于 2020-01-01 11:20:11

问题


I am not sure how to determine if a tree is balanced, perfectly balanced, or neither if I have it as a picture not a code

For example if I have this tree How can I check if it's balanced, perfectly balanced, or unbalanced? and can someone give me an example of a perfectly balanced tree?

    [o]
   /   \
 [b]   [p]
   \    / \
  [d]  [m] [r]

Clearly I can tell that the tree is unbalanced if it was something like this:

      [b]
        \
        [d]
         \
          [r]
           \
           [c]

However, if it was something very similar to the one above I don't know how to get it

This is a perfectly balanced and balanced tree:

        [k]
       /   \
      [A]   [p]
            /  \
           [N]  [R]

Can someone please explain it to me?


回答1:


A perfectly balanced tree should look like this:

       [ R ]
      /     \
    [a]      [b]
   /   \     /  \
 [c]   [d] [e]  [f]

Balanced: You can say it is balanced because the height of the left and right subtrees from every node differ by 1 or less (0 in this case),

Perfect: You can say it is perfect because the number of nodes is equal to 2^(n+1)-1 with n being the height of the tree, in this case (2^3) - 1 = 7

In your examples the 1st tree is balanced, but not perfect, the second is not balanced nor perfect. The third one is balanced because the depth for the left and right subtree on every node differ on 1 or less, but it is not perfect because the number of nodes is 5 when it should be 7 according to the perfect tree equation.

EDIT:

Regarding your lasts comments, the fact that you got it in an exam doesn't mean the answer was right in every sense. The notion of perfect tree is related to the notion of completeness, a complete tree is sometimes called a "perfect" tree, and it means the number of children for every node except the leafs is 2 what i gave you is an equation to calculate it. The third tree is balanced because what matters is the depth of the left and right subtrees for every node, not the number of children in the left and right subtrees. In this case from node A the depth of left subtree is 0 and the depth of right subtree is 0 -> 0 - 0 = 0, from P both depth are 1 -> 1 - 1 = 0 and from the root the depth from the left subtree is 1 and from the right subtree is 2 -> 2 - 1 = 1 <- it is balanced, since the difference should be 1 or less.

Hope it helps!




回答2:


A perfectly balanced AVL tree will have a height difference of no more than 1 between the left subtree and the right subtree




回答3:


The question should be about binary trees (BTs) in general, not just binary search trees (BSTs), since the order of the data in the nodes has nothing to do with whether or not the tree is balanced. One place to start is https://en.wikipedia.org/wiki/Binary_tree, but it has some problems since it is a bit of a mish-mosh of various possible definitions, some from CS and some from graph theory. Probably the most useful, non-contradictory set of definitions is:

A BT is perfect or height-balanced if every leaf is at the same level, which is equivalent to every path from a given node to a leaf being the same length; it is full if every internal (non-leaf) node has 2 children; it is complete if it is perfect and full; it is almost complete or nearly complete if is perfect and all levels but the last are full, and in the last level leaves are as far left as possible (so any "vacancies" are to the right); it is degenerate if every non-leaf node has just one child (and as a graph it is a path from the root to the one leaf).

Using these definitions: your first tree is perfect but not full, so not complete--node [b] is missing a left child, and adding it would make the tree complete; your second tree is degenerate (a path); your third tree is full (every node but the leaves has two children) and 1-height-balanced but neither "perfectly balanced (= perfect?)" or "balanced (meaning 0-height-balanced)" as you claim, since not every path from the root to a leaf is the same length.

In your first tree, if [b] had two children but [p] had only a left child, then it would be almost complete (perfect and full except for some missing children in the last level and the vacancies as far right at possible)--and these are important for storing binary heaps in arrays.

Sergio's example is complete (perfect or height-balanced, and full). (And note that it's not nice and can only cause confusion to use "balanced" to mean "1-height-balanced", or "perfect" as a synonym for "complete".)

Something less strict than being perfect (or perfectly-balanced) is being k-height-balanced, which means that the lengths of all paths from a given node to a leaf differ by at most k, which is equivalent to the difference in height of each node's left- and right-subtrees being at most k. For example, an AVL tree is 1-height-balanced.

The reason "height" is needed in these definitions is that there is a different concept of "weight-balanced BT", which has various definitions depending upon the use, with one being that for each node the number of nodes in the left sub-tree is the same as in the right sub-tree, and another being that the number of nodes in the left sub-tree is at least half and at most twice the number of nodes in the right sub-tree.



来源:https://stackoverflow.com/questions/20507665/how-to-determine-a-balanced-or-perfectly-balanced-binary-search-tree-just-from

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