5

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?

rullzing
  • 622
  • 2
  • 10
  • 22
  • While BST stands for Binary "Search" Tree, beyond searching and returning a boolean value it stil could be usefull as a very fast data type to insert, delete, read chunks of data in a sorted manner. However as you show in your last snippet AVL is not a perfectly balanced binary tree since it only regards the level difference to be 1 at max. A perfectly balanced binary tree (PBBT) should care about the size difference between Left and Right to be 1 at max, in order to be more efficient in tasks other than searching. – Redu Dec 08 '20 at 22:18

3 Answers3

11

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!

Eneko Alonso
  • 18,884
  • 9
  • 62
  • 84
Sergio Ayestarán
  • 5,590
  • 4
  • 38
  • 62
  • Thanks for answering. Just wanted to make sure. so because b has 1 child and p has 2 children it's balanced right? – rullzing Dec 10 '13 at 23:53
  • Is your perfect balance tree method always work ? because I have a tree that's perfectly balanced but your method didn't work with it – rullzing Dec 10 '13 at 23:58
  • Note that a perfect balanced tree means it must fulfill the balanced rule and the perfect rule. Probably you mean you have a balanced tree, can you add it to the question? – Sergio Ayestarán Dec 11 '13 at 00:03
  • Sure. I just added it to the question – rullzing Dec 11 '13 at 00:06
  • I'm pretty sure that the third one is perfectly balanced because I got that in an exam and the answer was perfectly balanced. I've also found another tree that is perfectly balanced but doesn't work with this rule : 2^(n+1) -1. So I'm just wondering if there is another method that works with everything – rullzing Dec 11 '13 at 00:19
  • how is it even balanced since A has 0 children and P has 2? – rullzing Dec 11 '13 at 00:27
  • Quick question: Wouldn't depth be dependent on node youre talking about https://courses.cs.washington.edu/courses/cse373/13wi/lectures/02-11/15-bst-review.pdf See slide 3. Would n be height then, which is length of the longest path from root to any node? – committedandroider Dec 28 '14 at 09:59
2

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

1

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.

wwwayne
  • 113
  • 1
  • 7