AVL tree rotation

This is a discussion on AVL tree rotation within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create AVL tree. So, a started with a binary tree and added a height value to it. ...

AVL tree rotation

I'm trying to create AVL tree. So, a started with a binary tree and added a height value to it. Now, i'm trying to make the left and right rotations so it can balance itself. The binary tree and height value works fine, but i can't get the left rotate to work, and i'm a little confused about the psuedocode for it.
I was trying to follow this psuedocode from the book:

Everything in and above the while loop is fine.
I thought maybe i was suppose to send in the root of the tree along with the node being rotated, so i duplicated the root node, and passed it in, but i guess that's not correct. So, all the code that is red, i'm not sure what i should be putting there. Any suggestions on how to fix this function?

Thanks

PS: If it looks familiar, it's the same as my hash program, except i'm using an AVL tree instead of a hash table.

>else if(x = x->parent->left)
Here's a useful tip: Think of = as the "is now" operator. Is x equal to x->parent->left? It is now!

>I was trying to follow this psuedocode from the book
I never liked the pseudocode examples from that book. If you don't mind me tooting my own horn, here's a tutorial on AVL trees using C that you might find more accessible.

Now, I'm pretty sure that my right and left rotations are working correctly. I made a file with just a few names that were inserted so that it is like a linked list. When the program is finished it is not balanced correctly. I think this is what it is doing:

It's balancing the root every time, so the root ends up balanced. But, the other nodes do not end up balanced (Bill and John). I thought by adding the BalanceTree call in my insert function, it would balance everything correctly. I also tried moving the BalanceTree call to main, but i get the same results.
On another file, where the names are inserted randomly, the root doesn't end up balanced, so maybe there is another problem.

The problem is that you're doing the insertion iteratively. You need to either do it recursively with a BalanceTree call in each recursive call, or use some other form of trickyness like parent pointers, or an explicit stack.

Do you mean i can't do it in the insert function?
I still can't figure it out. I tried replacing (in the insert function):

Code:

BalanceTree(root);

with:

Code:

while(z->parent != NULL)
{
z = z->parent;
BalanceTree(z);
}

I thought this should balance every node from z (the inserted node) up till the root node.
But, when i run the program, only the root prints. So, it's like the tree is not linked together anymore or something.

I also tried BalanceTree(z) at the end of the insert function, and adding:

Code:

if(root->parent != NULL)
{
root = root->parent;
BalanceTree(root);
}

at the end of the BalanceTree function.
So, i thought it should call itself recursively and balance every node from z (the inserted) untill the root. But i get the same result as above.

Edit* - I also tried calling BalanceTree recursively with its parent every time. I added:

Code:

BalanceTree(z);

to the end of the insert function, and:

Code:

if(root->parent != NULL)
{
BalanceTree(root->parent);
}

at the end of the BalanceTree function. So, i thought this should balance z, and all its parents up to the root, but no names print at all.

Do you mean i can't do it in the insert function?
I still can't figure it out. I tried replacing (in the insert function):

Code:

BalanceTree(root);

with:

Code:

while(z->parent != NULL)
{
z = z->parent;
BalanceTree(z);
}

For that to work I think you'd need to declare z as a reference. I'd have to try it out myself though.

I thought this should balance every node from z (the inserted node) up till the root node.
But, when i run the program, only the root prints. So, it's like the tree is not linked together anymore or something.

I also tried BalanceTree(z) at the end of the insert function, and adding:

Code:

if(root->parent != NULL)
{
root = root->parent;
BalanceTree(root);
}

at the end of the BalanceTree function.
So, i thought it should call itself recursively and balance every node from z (the inserted) untill the root. But i get the same result as above.

Nope you shouldn't have to touch the BalanceTree function.

Edit* - I also tried calling BalanceTree recursively with its parent every time. I added:

Code:

BalanceTree(z);

to the end of the insert function, and:

Code:

if(root->parent != NULL)
{
BalanceTree(root->parent);
}

at the end of the BalanceTree function. So, i thought this should balance z, and all its parents up to the root, but no names print at all.

It's TreeInsert that needs to be made recursive if you want to do the recursive solution, not BalanceTree. I've only done the recursive solution, and haven't made one that uses parent pointers. Parent pointers can aid in traversing back up the tree, but at the cost of adding more complexity to the tree rebalancing.

I would recommend writing a tree-verification routine (an invariant checker) that you can call at specific times like before and after a rebalance to check that the tree is in a good state.
Such a verification routine could recursively check the following:
Left pointer is either NULL or
Left pointer points to a node whose value is not greater than the current node.
Left pointer's parent is this node.
Left node's height is the parent node's height minus 1
Right pointer is either NULL or
Right pointer points to a node whose value is not less than the current node.
Right pointer's parent is this node.
Right node's height is the parent node's height minus 1
You could even count all the nodes too.