【问题标题】:Insert function for an AVL treeAVL 树的插入函数
【发布时间】:2020-03-14 03:00:07
【问题描述】:

我正在使用 AVL 树,但我很难将所有部分连接在一起,尤其是在树需要旋转以实现自我平衡的情况下。到目前为止,我有向左和向右旋转的函数以及需要保存它们的插入函数。我的方法与我所见过的不同,例如极客极客,这就是我在这里寻求指导的原因......

这是我的右旋转:

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(...)

随着我的左转:

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);
} 

这是我正在使用的插入物,需要帮助将所有内容与旋转案例连接在一起:

  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

  }

【问题讨论】:

  • 对一些功能代码试试这个:github.com/bdpdx/AVL ...我建议你在那里实现单元测试,然后逐步检查你的代码,看看出了什么问题。

标签: c++11 avl-tree


【解决方案1】:
  1. 为案例 1 - 4 找到一些尽可能简单的示例
  2. 对触发每个案例的操作序列进行编码
  3. 画出或写下每种情况的处理方式。
  4. 实施每个案例

为了测试,

  1. 从第 2 点开始运行测试用例
  2. 实现一个测试 AVL 约束(高度差和排序)的函数
  3. 对树运行大量随机操作(插入和删除),同时检查约束以识别关键设置

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多