Not Yet Answered # height of an avl tree?

SasseMan 49 Discussion Starter timb89 SasseMan 49 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

An avl tree is a self balancing tree which means that there is a mathematical relationship between the number of elements in the tree and the height.

Since a lookup in a AVL tree takes O(log(n)) time, the height should also be O(log(n)) i.e. ceiling(log2(n)). Read about avl trees here...

0

An avl tree is a self balancing tree which means that there is a mathematical relationship between the number of elements in the tree and the height.

Since a lookup in a AVL tree takes O(log(n)) time, the height should also be O(log(n)) i.e. ceiling(log2(n)). Read about avl trees here...

obvuously regular code like this would work? but is it as simple as just a one line return code?

```
int height(node *AVLtree, int current, int max)
{
if(AVLtree != NULL)
{
if(current > max)
{
max = current;
}
}
if(AVLtree->left != NULL)
{
current = height(AVLtree->left, current, max);
}
if(AVLtree->right != NULL)
{
current = height(AVLtree->right, current, max);
}
}
```

0

If you want to calculate the height by going down the tree you don't need all that code. Just follow nodes down until you reach the end and thats your height. But the better thing to do is to keep track of the number of nodes in the data structure an just return ceil(log2(n)). Your code look to be running in O(n), I would do it like this...

```
int height( node *AVLtree ) {
if(AVLtree == NULL) return 1;
return 1 + height(AVLtree->left);
}
```

But of course you can be off by 1 if you use this method, it depends on what the heigh value is going to be used for. If your looking for the most "correct" solution you probably want to go through the entire tree. I'm not sure if your code will work though, it not even returning anything.

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 = ...
```