Not Yet Answered # Function to determine if the binary search tree is perfectly balanced

ken_taiken deceptikon 1,760 Discussion Starter ariel930 11 deceptikon 1,760 Discussion Starter ariel930 11 firstPerson 761 yde Hey, so I wanna ask how I need to create a method who will remove word if in that word is 2 same chars. Example: "Potato" in this word there is a 2 "o" chars so this word will need to be removed. "Forum" in this word there is no ...

Hi I'm having a problem implementing a mini shopping cart drop down in the header to show the user all the products they have in their shopping cart. It seems the only solution for this is Ajax, and I've looked all over and can't find anything that I could possibly ...

0

You can modify and use DFS. For every node you can check if the depth of the two subtrees coming under it is equal or not.

0

By perfectly balanced do you mean a complete tree? A full tree? Merely a height balanced or weight balanced tree? The algorithm is different for each. Typically what's meant is height balance, or what would be the desired end structure maintained an AVL tree.

A recursive algorithm presents itself immediately when you think about measuring the height of each subtree and comparing them:

```
is_balanced(tree)
balanced = true
height_diff = 0
if not empty(tree)
balanced = is_balanced(tree.left) and is_balanced(tree.right)
height_diff = abs(height(tree.left) - height(tree.right))
end if
return empty or (balanced and height_diff <= 1)
```

0

Our question says the following definition: A tree is perfectly balanced if on each level there are 2 power n nodes except for last level.

0

Well, you could derive a solution directly from the wording of the definition. First count the number of nodes to get a value for `n`

, then determine the depth of the tree so you know what the last level is, finally do a level order traversal and check at the end of each level whether the size is 2^n or it's the last level.

This breaks the problem down into three smaller problems:

- Counting the nodes in a tree.
- Finding the depth of a tree.
- Performing a level order traversal and counting nodes on each level.

1

Thank you so much you all for your kind relpies.I have finished making my function.My function does the following:

1)Counts the total number of nodes of the tree.

2)Counts the numbers of nodes on the last level.

3)Determine the depth of the tree.

4)Total nodes-last level nodes.

5)Compare the answer to 2 pow (height-1) -1.

And it worked.

0

Also note that you might be able to skip transversal if the number of nodes isn't a power of 2. If it is, then you have to transverse just to make sure it fits the definition.

0

Using your definition of a perfectly balanced tree (I would call it an optimally packed tree), I think the following algorithm would work. If I'm wrong please let me know.

Traverse each path measuring its length.

Keep track of the longest and shortest paths measured.

If, at any time during the scan, the difference between longest and shortest

is greater than 1, the tree is not balanced.

If the tree is perfectly balanced you'll have to traverse the whole tree but if it's unbalanced you won't.

This article has been dead for over six months. Start a new discussion instead.

Recommended Articles

Hi. Im using vb 2010. I want to dynamically add textboxes to my form by clicking on a button. I've google searched and so far this code worked:

```
Private Sub btn_addline_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_addline.Click
Dim txtB1 As New TextBox
Dim i
For i = ...
```