Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 294
rullzing Avatar asked Dec 10 '13 23:12

rullzing


People also ask

How do you know if a binary tree is balanced?

To check if a Binary tree is balanced we need to check three conditions : The absolute difference between heights of left and right subtrees at any node should be less than 1. For each node, its left subtree should be a balanced binary tree. For each node, its right subtree should be a balanced binary tree.

How do you identify a binary search tree?

To see if a binary tree is a binary search tree, check: If a node is a left child, then its key and the keys of the nodes in its right subtree are less than its parent's key. If a node is a right child, then its key and the keys of the nodes in its left subtree are greater than its parent's key.

How do you make a perfectly balanced tree?

Creating a Balanced BST Since we need the tree to be balanced, we must put the middle value as the root. After that, we can add the values before the middle to the left of the tree. Therefore, all smaller values will be added to the left subtree.

Is perfect binary tree a balanced binary tree?

Every complete binary tree is balanced but not the other way around. As implies, in a complete tree, always the level difference will be no more than 1 so it is always balanced.


1 Answers

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!

like image 73
Sergio Ayestarán Avatar answered Sep 27 '22 18:09

Sergio Ayestarán