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 967289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:17:30+00:00 2026-05-16T02:17:30+00:00

I am trying to implement an AVL tree and not sure about the best

  • 0

I am trying to implement an AVL tree and not sure about the best way to do insert and track each node’s parent. This is educational so please do not suggest “use boost” 🙂

This compiles but I am not convinced of its correctness or that it is the best way.
Specifically this

insert(someNumber,root,root);

Also, I will redo the height part when I rebalance and move up the tree.

I insert like this

myTree.insert(someNumber);

Here is the method. I am not sure what my second param should be here. I would have thought NULL but the compiler does not like that (different function definition).

void Tree::insert(int someNumber){
    insert(someNumber,root,root);
}

Here is my insert

void Tree::insert(int someNumber,Node*& subTreeRoot,Node*& parent){
    if(subTreeRoot==NULL)
    {
        subTreeRoot = new Node(someNumber,parent);
        if(subTreeRoot->myParent)   
    }
    else if (someNumber<subTreeRoot->myNumber)
    {
        if((subTreeRoot->right==NULL)||((subTreeRoot->left!=NULL)&&(subTreeRoot->right!=NULL)))
            subTreeRoot->height++;
        insert(someNumber,subTreeRoot->left,subTreeRoot);
    }
    else
    {
        if((subTreeRoot->left==NULL)||((subTreeRoot->left!=NULL)&&(subTreeRoot->right!=NULL)))
            subTreeRoot->height++;
        insert(someNumber,subTreeRoot->right,subTreeRoot);
    }
}

–

  • 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-16T02:17:30+00:00Added an answer on May 16, 2026 at 2:17 am

    Since you are doing this for education, I would suggest that you work some cases out by hand, then code them into tests of the form

     Insert(6);
     Insert(11);
     // ...
     Insert(3);
    
     Node test = root;
     assert(root->height == 3);
     assert(root->value == 6);
     test = root->right;
     assert( ... );
    

    the numbers are completely made up.

    This will

    1. Give you more than a feeling as to whether or not your code is working. (Hint: if your are not sure your code is working, it probably isn’t)
    2. Give you a place to start figuring out what is going wrong.
    3. Develop the habit of testing. It is counterintuitive to some people that writing twice as much code (tests + code) is faster than just writing the code in the first place, but try it. Plus writing more code = more practice.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently trying to implement an AVL tree in C++, so far I've done
Am trying to implement a generic way for reading sections from a config file.
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I am working on an assignment that asks me to implement an AVL tree.
Trying to implement a shell, mainly piping. I've written this test case which I
All I am currently trying implement something along the lines of dim l_stuff as
trying to implement a dialog-box style behaviour using a separate div section with all
I am trying to implement string unescaping with Python regex and backreferences, and it
I'm trying to implement a data compression idea I've had, and since I'm imagining
I have been trying to implement Win32's MessageBox using GTK. The app uses SDL/OpenGL,

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.