I'm working on implementing an AVL Search tree. So far I've finished the coding part and I've started testing it for bugs. I found out that my node rotation methods are bugged and for god's sake I can't understand what's the problem.
The algorithm works as it should on paper but when executed on a machine it well...leaks tree nodes.
This is the method used to rotate a node to the left: http://pastebin.com/mPHj29Af
bool avl_search_tree::avl_tree_node::rotate_left()
{
if (_right_child != NULL) {
avl_tree_node *new_root = _right_child;
if (_parent != NULL) {
if (_parent->_left_child == this) {
_parent->_left_child = new_root;
} else {
_parent->_right_child = new_root;
}
}
new_root->_parent = _parent;
_parent = new_root;
_right_child = new_root->_left_child;
new_root->_left_child = this;
if (_right_child != NULL) {
_right_child->_parent = this;
}
//update heights
update_height();
new_root->update_height();
return true;
}
return false;
}
In my insertion method I commented the AVL balancing part and instead I'm just trying to rotate the newly inserted node to the left. The result for inserting integers in ascending order: my tree only contains the initial root (first node inserted) and all other nodes are leaked.
Any help in identifying the problem is highly appreciated as I'm starting to go mad.
For the record: if I don't use any rotations the tree won't leak nodes and it works as a normal unbalanced binary search tree (for insertion and lookup).
Edit: Due to AJG85's comment I'll add the observations:
I added printf 'checks' to the destructor method of avl_search_tree::avl_tree_node that will print the key value (in my case 32 bit integers) before cleanup and, to the insert method of the avl_search_tree that will print the key just inserted.
Then in the program's entrypoint I allocate an avl_search_tree on the heap and add keys to it in ascending order and then delete it.
With AVL Balancing enabled I get the following output in the terminal:
bool avl_search_tree::insert(const int&) : 1
bool avl_search_tree::insert(const int&) : 2
bool avl_search_tree::insert(const int&) : 3
bool avl_search_tree::insert(const int&) : 4
bool avl_search_tree::insert(const int&) : 5
bool avl_search_tree::insert(const int&) : 6
bool avl_search_tree::insert(const int&) : 7
bool avl_search_tree::insert(const int&) : 8
avl_search_tree::avl_tree_node::~avl_tree_node() : 1
Which means thatall insertions were successful but only the root has been deleted.
With the AVL Balancing commented out it works like a normal binary search tree. The terminal output is:
bool avl_search_tree::insert(const int&) : 1
bool avl_search_tree::insert(const int&) : 2
bool avl_search_tree::insert(const int&) : 3
bool avl_search_tree::insert(const int&) : 4
bool avl_search_tree::insert(const int&) : 5
bool avl_search_tree::insert(const int&) : 6
bool avl_search_tree::insert(const int&) : 7
bool avl_search_tree::insert(const int&) : 8
avl_search_tree::avl_tree_node::~avl_tree_node() : 1
avl_search_tree::avl_tree_node::~avl_tree_node() : 2
avl_search_tree::avl_tree_node::~avl_tree_node() : 3
avl_search_tree::avl_tree_node::~avl_tree_node() : 4
avl_search_tree::avl_tree_node::~avl_tree_node() : 5
avl_search_tree::avl_tree_node::~avl_tree_node() : 6
avl_search_tree::avl_tree_node::~avl_tree_node() : 7
avl_search_tree::avl_tree_node::~avl_tree_node() : 8
Which means that everything is properly cleaned up.
Now...how did I come to the conclusion that the rotation methods are the issues? Under the commented AVL balancing subroutine I added a line that rotates every newly inserted node to the left. The result? The same as if the AVL Balancing subroutine was enabled.
And regarding the update_height() method, It doesn't alter the tree's structure in any way.
I hope this will clarify it.
Edit 2:
To clarify some more things, his is how the avl_tree_node destructor is implemented:
avl_search_tree::avl_tree_node::~avl_tree_node()
{
printf("%s : %d\n", __PRETTY_FUNCTION__, *_key);
if (_left_child != NULL) {
delete _left_child;
}
if (_right_child != NULL) {
delete _right_child;
}
if (_key != NULL) {
delete _key;
}
}
_left_child and _right_child are pointers to avl_tree_node objects allocated on the heap.
Edit 3:
Thanks to AGJ85's 2nd comment I found the issue. In my rotate methods I forgot that I actually have to update the tree's root pointer to the new root whenever the root was shifted.
Basically the tree's root was always pointing to the first inserted node and without updating the pointer when needed, my rotate methods would leak the new tree's root which was actually configured right. :)
Thank you AGJ85!