vendredi 13 mars 2020

Insert function for an AVL tree

I'm working with an AVL tree but im having the hardest time connecting all the pieces together especially for when it comes to the cases where the tree needs to rotate to self balance. So far I have functions that rotate both left and Right and also an insert function that will need to hold these. My approach is different from those that i have seen for example geeks for geeks and thats why i sought out for guidance here...

This is my Right rotate:

void RightRotate(NODE* Parent, NODE* N)
  {

    NODE* L = N->Left;
    NODE* A = L->Left;
    NODE* B = L->Right;
    NODE* C = N->Right;

      L->Right = N;
      N->Left = B;



      if(Parent == nullptr){
        Root = L;
      }
      else if (L->Key < Parent->Key){
        Parent->Left = L;
      }
      else{
        Parent->Right = L;
      }

      // 4. Update N's height:
      int HA , HB , HC;

      if(A == nullptr){
        HA = -1;
        }
      else{
          HA = A->Height;;
      }
      if(B == nullptr){
        HB = -1;
        }
      else{
          HB = B->Height;;
      }
      if(C == nullptr){
        HC = -1;
        }
      else{
          HC = C->Height;;
      }

      N->Height = 1 + max(HB , HC);



      L->Height = 1 + max(HA, N->Height);

  }// end of rightrotate(...)

Along with my left rotate:

void LeftRotate(NODE* Parent, NODE* N)
{

    NODE* R = N->Right;
    NODE* A = N->Left;
    NODE* B = R->Left;
    NODE* C = R->Right;

    //2. Rotate:
    R->Left = N;
    N->Right = B;

    //3. Update Parent to the link:
    if(Parent == nullptr){
      Root = R;
      }
    else if(R->Key < Parent->Key){
      Parent->Left = R;
      }
    else{
      Parent->Right = R;
      }

    // Update N's height:
    int HA, HB, HC;

    if(A == nullptr){
        HA = -1;
        }
    else{
        HA = A->Height;
    }
    if(B == nullptr){
        HB = -1;
        }
    else{
        HB = B->Height;
    }
    if(C == nullptr){
        HC = -1;
        }
    else{
        HC = C->Height;
    }

    N->Height = 1 + max(HA , HB);



    R->Height = 1 + max(HC, N->Height);
} 

This is the insert that I'm working with and need help connecting everything all together with the cases of the rotations:

  void insert(TKey key, TValue value)
  {
    NODE* prev = nullptr;
    NODE* cur = Root;

    stack<NODE*> nodes;

    //
    // 1. Search to see if tree already contains key:
    //
    while (cur != nullptr)
    {
      if (key == cur->Key)  // already in tree
        return;

      nodes.push(cur);  // stack so we can return later:

      if (key < cur->Key)  // search left:
      {
        prev = cur;
        cur = cur->Left;
      }
      else
      {
        prev = cur;
        cur = cur->Right;
      }
    }//while

    //
    // 2. if we get here, key is not in tree, so allocate
    // a new node to insert:
    // 
    NODE* newNode;
    newNode = new NODE();
    newNode->Key = key;
    newNode->Value = value;
    newNode->Height = 0;  // leaf node -> sub-tree of height 0:
    newNode->Left = nullptr;
    newNode->Right = nullptr;

    //
    // 3. link in the new node:

    if (prev == nullptr)
      Root = newNode;
    else if (key < prev->Key)
      prev->Left = newNode;
    else
      prev->Right = newNode;

    // 
    // 4. update size:
    //
    Size++;

    while( !nodes.empty() ){
       NODE* cur;
       cur = nodes.top();
       nodes.pop();
       int HL;
       int HR;
       if(cur->Left == nullptr){
          HL = -1;
          }
       else{
          HL = cur->Left->Height;
          }
       if(cur->Right == nullptr){
          HR = -1;
          }
       else{
           HR =  cur->Right->Height;
          }
      int newH = 1 + max(HL,HR);

      if(cur->Height == newH){
        break;
      }
      else if( (cur->Height) - (newH) == 1 || (cur->Height) - (newH) == -1 || (cur->Height) - (newH) == 0 ) {
        cur->Height = newH; // updates the new height.
        continue;
      }

      else{ // TODO Case conditions:

        if(cur->Left->Height > cur->Right->Height){ // case 1 and 2

        }
        else{ // case 3 and 4

        }

      }


    }// end while

  }

Aucun commentaire:

Enregistrer un commentaire