2

I've built an AVL Tree out of an old Binary Tree in C++ for practice and it is not working correctly. I think it may not be updating the height of the nodes correctly but I can't figure out the root of the issue ( I'm 90% sure it has to do with the helper functions getHeight, setHeight, getBalance).

Some example test runs..:

adding 6,3,4 causes a Right Rotation it should cause a RL rotation

similarly adding 20,30,25 causes a LL rotation when it should cause a LR rotation

adding 20,30,10,5,6 causes two seperate Right rotations when it should cause a RL rotation

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstdlib>

using namespace std;

struct intNode {
   int data;
   intNode * Left;
   intNode * Right;
   int height;

   intNode(int input, intNode * iLeft= NULL, intNode * iRight = NULL) {
      data = input;
      Left = iLeft;
      Right = iRight;
      height = 0;
   }

};    

int getHeight(intNode* temp) {
   if (temp == NULL) 
       return 0;
   return temp->height;    
}

void setHeight(intNode * temp) 
{
    int hLeft = getHeight(temp->Left);
    int hRight = getHeight(temp->Right);          
    temp->height = 1 + max(hLeft, hRight); 
}

int getBalance(intNode * temp) {

   if (temp == NULL) {
      return 0;
   } else {
       return getHeight(temp->Left) - getHeight(temp->Right);
   }
}  


struct intTree {
    intNode * root;

    intTree();
    void addNode(int input);
    void addNode(int input, intNode *& root);
    void print();
    void print(intNode *input);

    intNode * balanceTree(intNode *& sub);
    intNode * rotateRight(intNode *& sub);
    intNode * rotateLeft(intNode *& sub);

}; 

 void intTree::print(intNode *input) {

    if (input != NULL) {
        print(input->Left);
        cout << input->data << endl;
        print(input->Right);
    }

}

void intTree::print() {
   print(root);
}

intNode * intTree::rotateRight(intNode *& subTree) {
   intNode * newRoot = subTree->Left;
   subTree->Left = newRoot->Right;   
   newRoot->Right = subTree;
   setHeight(subTree);    
   setHeight(newRoot);    

   return newRoot;
}

 intNode * intTree::rotateLeft(intNode *& subTree) {
   intNode * newRoot = subTree->Right;
   subTree->Right = newRoot->Left;   
   newRoot->Left = subTree;
   setHeight(subTree);    
   setHeight(newRoot);    

   return newRoot;
}

intNode* intTree::balanceTree(intNode *& subTree) {
   setHeight(subTree);

      if (getBalance(subTree) == 2 && getBalance(subTree->Right) < 0) {                     
         cout << "RL" << endl;
         subTree->Left = rotateLeft(subTree->Left);
         return rotateRight(subTree);

      } else if (getBalance(subTree) == 2) {                    // RR
        cout << "RR" << endl;
        return rotateRight(subTree);

      } else if (getBalance(subTree) == -2 && getBalance(subTree->Left) > 0) {  // LR
        cout << "LR" << endl;
        subTree->Right = rotateRight(subTree->Right);
        return rotateLeft(subTree);

      } else if (getBalance(subTree) == -2) {                   // LL
        cout << "LL" << endl;
        return rotateLeft(subTree);

      } else {
        return subTree;                                 // balanced
      }

}

intTree::intTree() {
   root = NULL;
}

void intTree::addNode(int input, intNode *& temp) {
   if (temp == NULL) {
       temp = new intNode(input);
   } else if (input < temp->data) {
       cout <<" added to the left" << endl;
       addNode(input,temp->Left);
   } else if (input > temp->data) {
       cout <<" added to the right" << endl;
       addNode(input, temp->Right);
   }

   temp = balanceTree(temp);

}

void intTree::addNode(int input) {
   addNode(input, root); 
}

void read() {
   string num;
   int balance, input;
   int i = 0;

   intTree * intBST = new intTree();       

   while (i != 10) {
      cout << "number?" << endl;
      cin >> input;


      intNode *tempInt= new intNode(input);
      intBST->addNode(input);

      i++; 
   }     

   cout << "Finished reading in files" << endl;

   while (true) {
      string userInput;
      cin >> userInput;    

      if (userInput == "Exit") {
         cout << "Goodbye!" << endl;
         return;
     }

     if (userInput == "Print") {
         intBST->print();
         return;
     }

  }
}


void main() {
  read();

  return 0;
}

I would appreciate any tips/help on further figuring this out. Let me know if I can provide any more information to help.

Thank you.

BitShift
  • 39
  • 3

0 Answers0