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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:35:39+00:00 2026-05-24T10:35:39+00:00

Update: I can’t get Balancing to work, because I cannot get doAVLBalance to recognize

  • 0

Update: I can’t get “Balancing” to work, because I cannot get “doAVLBalance” to recognize the member functions “isBalanced()”, “isRightHeavy()”, “isLeftHeavy”. And I don’t know why! I tried Sash’s example(3rd answer) exactly but I get “deceleration is incompatible” and I couldn’t fix that…so I tried doing it my way…and it tells me those member functions don’t exist, when they clearly do.

“Error: class “IntBinaryTree:TreeNode” has no member “isRightHeavy”.
I’m stuck after trying for the last 4 hours :(. Updated code below, help would be much appreciated!!

I’m creating a String based Binary Search Tree and need to make it a “Balanced” tree. How do I do this?* Help please!! Thanks in advance!

BinarySearchTree.cpp:

    bool IntBinaryTree::leftRotation(TreeNode *root)
    {
        //TreeNode *nodePtr = root;  // Can use nodePtr instead of root, better?
        // root, nodePtr, this->?

        if(NULL == root)
        {return NULL;}

        TreeNode *rightOfTheRoot = root->right;
        root->right = rightOfTheRoot->left;
        rightOfTheRoot->left = root;

        return rightOfTheRoot;
    }

    bool IntBinaryTree::rightRotation(TreeNode *root)
    {
        if(NULL == root)
        {return NULL;}
        TreeNode *leftOfTheRoot = root->left;
        root->left = leftOfTheRoot->right;
        leftOfTheRoot->right = root;

        return leftOfTheRoot;
    }

    bool IntBinaryTree::doAVLBalance(TreeNode *root)
    {


        if(NULL==root)
            {return NULL;}
        else if(root->isBalanced()) // Don't have "isBalanced"
            {return root;}

        root->left = doAVLBalance(root->left);
        root->right = doAVLBalance(root->right);

        getDepth(root); //Don't have this function yet

        if(root->isRightHeavy()) // Don't have "isRightHeavey"
        {
            if(root->right->isLeftheavey())
            {
                root->right = rightRotation(root->right);
            }
            root = leftRotation(root);
        }
        else if(root->isLeftheavey()) // Don't have "isLeftHeavey"
        {
            if(root->left->isRightHeavey())
            {
                root->left = leftRotation(root->left);
            }
            root = rightRotation(root);
        }
        return root;
    }

    void IntBinaryTree::insert(TreeNode *&nodePtr, TreeNode *&newNode)
    {
        if(nodePtr == NULL)
            nodePtr = newNode;                  //Insert node
        else if(newNode->value < nodePtr->value)
            insert(nodePtr->left, newNode);     //Search left branch
        else
            insert(nodePtr->right, newNode);    //search right branch
    }

//
// Displays the number of nodes in the Tree


int IntBinaryTree::numberNodes(TreeNode *root)
{
    TreeNode *nodePtr = root;

    if(root == NULL)
        return 0;

    int count = 1; // our actual node
    if(nodePtr->left !=NULL)
    { count += numberNodes(nodePtr->left);
    }
    if(nodePtr->right != NULL)
    {
        count += numberNodes(nodePtr->right);
    }
    return count;
} 

    // Insert member function

    void IntBinaryTree::insertNode(string num)
    {
        TreeNode *newNode; // Poitner to a new node.

        // Create a new node and store num in it.
        newNode = new TreeNode;
        newNode->value = num;
        newNode->left = newNode->right = NULL;

        //Insert the node.
        insert(root, newNode);
    }

    // More member functions, etc.

BinarySearchTree.h:

class IntBinaryTree
{
private:
    struct TreeNode
    {
        string value; // Value in the node
        TreeNode *left; // Pointer to left child node
        TreeNode *right; // Pointer to right child node
    };

    //Private Members Functions
    // Removed for shortness
    void displayInOrder(TreeNode *) const;


public:
    TreeNode *root;
    //Constructor
    IntBinaryTree()
        { root = NULL; }
    //Destructor
    ~IntBinaryTree()
        { destroySubTree(root); }

    // Binary tree Operations
    void insertNode(string);
    // Removed for shortness

    int numberNodes(TreeNode *root);
    //int balancedTree(string, int, int); // TreeBalanced

    bool leftRotation(TreeNode *root);
    bool rightRotation(TreeNode *root);
    bool doAVLBalance(TreeNode *root); // void doAVLBalance();
    bool isAVLBalanced();

    int calculateAndGetAVLBalanceFactor(TreeNode *root);

    int getAVLBalanceFactor()
    {
        TreeNode *nodePtr = root; // Okay to do this? instead of just
        // left->mDepth
        // right->mDepth

        int leftTreeDepth = (left !=NULL) ? nodePtr->left->Depth : -1;
        int rightTreeDepth = (right != NULL) ? nodePtr->right->Depth : -1;
        return(leftTreeDepth - rightTreeDepth);
    }

    bool isRightheavey() { return (getAVLBalanceFactor() <= -2); }

    bool isLeftheavey() { return (getAVLBalanceFactor() >= 2); }


    bool isBalanced()
    {
        int balanceFactor = getAVLBalanceFactor();
        return (balanceFactor >= -1 && balanceFactor <= 1);
    }


    int getDepth(TreeNode *root); // getDepth

    void displayInOrder() const
        { displayInOrder(root); }
    // Removed for shortness
};
  • 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-24T10:35:40+00:00Added an answer on May 24, 2026 at 10:35 am

    Programmers use AVL Tree concepts to balance binary trees. It is quite simple. More information can be found online. Quick wiki link

    Below is the sample code which does tree balance using AVL algorithm.

    Node *BinarySearchTree::leftRotation(Node *root)
    {
        if(NULL == root)
        {
            return NULL;
        }
        Node *rightOfTheRoot = root->mRight;
        root->mRight = rightOfTheRoot->mLeft;
        rightOfTheRoot->mLeft = root;
    
        return rightOfTheRoot;
    }
    
    Node *BinarySearchTree::rightRotation(Node *root)
    {
        if(NULL == root)
        {
            return NULL;
        }
        Node *leftOfTheRoot = root->mLeft;
        root->mLeft = leftOfTheRoot->mRight;
        leftOfTheRoot->mRight = root;
    
        return leftOfTheRoot;
    }
    
    Node *BinarySearchTree::doAVLBalance(Node *root)
    {
        if(NULL == root)
        {
            return NULL;
        }
        else if(root->isBalanced())
        {
            return root;
        }
    
        root->mLeft  = doAVLBalance(root->mLeft);
        root->mRight = doAVLBalance(root->mRight);
    
        getDepth(root);
    
        if(root->isRightHeavy())
        {
            if(root->mRight->isLeftHeavy())
            {
                root->mRight = rightRotation(root->mRight);
            }
            root = leftRotation(root);
        }
        else if(root->isLeftHeavy())
        {
            if(root->mLeft->isRightHeavy())
            {
                root->mLeft = leftRotation(root->mLeft);
            }
            root = rightRotation(root);
        }
    
        return root;
    }
    

    Class Definition

    class BinarySearchTree
    {
    public:
        // .. lots of methods 
        Node *getRoot();
        int getDepth(Node *root);
    
        bool isAVLBalanced();
        int calculateAndGetAVLBalanceFactor(Node *root);
        void doAVLBalance();
    
    private:
         Node *mRoot;
    };
    
    class Node
    {
    public:
        int  mData;
        Node *mLeft;
        Node *mRight;
        bool mHasVisited;
        int mDepth;
    public:
    
        Node(int data)
        : mData(data),
          mLeft(NULL),
          mRight(NULL),
          mHasVisited(false),
          mDepth(0)
        {
        }
    
        int getData()              { return mData; }
    
        void setData(int data)     { mData = data;  }
    
        void setRight(Node *right) { mRight = right;}
    
        void setLeft(Node *left)   { mLeft = left; }
    
        Node * getRight()          { return mRight; }
    
        Node * getLeft()           { return mLeft; }
    
        bool hasLeft()             { return (mLeft != NULL);  }
    
        bool hasRight()            { return (mRight != NULL); }
    
        bool isVisited()           { return (mHasVisited == true); }
    
        int getAVLBalanceFactor()
        {
            int leftTreeDepth = (mLeft != NULL) ? mLeft->mDepth : -1;
            int rightTreeDepth = (mRight != NULL) ? mRight->mDepth : -1;
            return(leftTreeDepth - rightTreeDepth);
        }
    
        bool isRightHeavy() { return (getAVLBalanceFactor() <= -2); }
    
        bool isLeftHeavy()  { return (getAVLBalanceFactor() >= 2);  }
    
        bool isBalanced()
        {
            int balanceFactor = getAVLBalanceFactor();
            return (balanceFactor >= -1 && balanceFactor <= 1);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can update code correctly, but cannot commit code. I get the following error
Update: I can't delete this question, because the answer has been upvoted, yet it
How can update a input hidden inside an UpdatePanel on an AsyncPostBack? The user
I know I can update a single record like this - but then how
I see that EF can update a model based on an existing database schema.
I am trying to figure out how I can update my sql query dynamically.
I'm generating ICalendar (.ics) files. Using the UID and SEQUENCE fields I can update
I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the
By adding a custom handler to the SiteMapResolve event, I can update sitemap url's
How can I update some Windows Service Seperated Assemblies without restarting the service? Note:

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.