Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6213993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:47:53+00:00 2026-05-24T06:47:53+00:00

I’m working on implementing an AVL Search tree. So far I’ve finished the coding

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T06:47:55+00:00Added an answer on May 24, 2026 at 6:47 am

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.