【问题标题】:AVL Tree Insertion Without Recursion C++无递归 C++ 的 AVL 树插入
【发布时间】:2017-01-01 21:29:58
【问题描述】:

我正在使用以下代码来实现 AVL 树插入,但它没有按正确的顺序显示,也没有更新高度我还留下了一些功能,因为当插入完成时,我将能够完成这些功能

AVLNode.cpp

  #include <iostream>
    #include <string>
    #include "AVLNode.h"
    using namespace std;

    AVLNode::AVLNode(string ss, string na){
        ssn = ss;
        name = na;
        height = 0;
        left = NULL;
        right = NULL;
        parent = NULL;
    }

AVLNode.h

    #include <iostream>
    #include <string>
    using namespace std;

    struct AVLNode{
      string ssn;
      string name;
      AVLNode *left;
      AVLNode *right;
      AVLNode *parent;
      int height;

      AVLNode(string ss, string na);
    };

AVLTree.cpp

#include <iostream>
#include <string>
#include <stdio.h>
#include "AVLTree.h"
#include <iomanip>
#include <queue>
using namespace std;

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

AVLTree::~AVLTree(){


}

AVLNode* AVLTree::getRoot(){
    return root;
}


// search value ss in the AVL tree
bool AVLTree::find(string ss){
    if (root == NULL) {
        return false;
    }

    AVLNode* node = root;

    while (node != NULL) {
        if (ss.compare(node->ssn) == 0) {
            return true;
        }
        if (ss.compare(node->ssn) < 0) {
            node = node->left;
        }
        else{
            node = node->right;
        }
    }
    return false;
}

// return the height of the subtree rooted at node
// if subtree is empty, height is -1
// if subtree has one node, height is 0
int AVLTree::height(AVLNode* node){

    if(node != NULL){
        return node->height;
    }
    else{
        return -1;
    }
}

// return the balance factor of the node
int AVLTree::balanceFactor(AVLNode* node){
    return height(node->left) - height(node->right);
}

// update the height of the node
// this should be done whenever the tree is modified
void AVLTree::updateHeight(AVLNode* node){
    int hl = height(node->left);
    int hr = height(node->right);
    node->height = (hl > hr ? hl : hr) + 1;
}


// rotate right the subtree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::rotateRight(AVLNode* node){
    AVLNode* lp = node->left;      // left child of node
    if (node->parent != NULL) {  // node is not root
        if (node->parent->left == node) { // node is a left child
            node->parent->left = lp;
        }else{
            node->parent->right = lp;     // node is a right child
        }
    }

    if (lp->right != NULL) {           // pointer update
        lp->right->parent = node;
    }

    lp->parent = node->parent;
    node->left = lp->right;
    lp->right = node;
    node->parent = lp;
    updateHeight(node);                   // after rotation, update height
    updateHeight(lp);                     // after rotation, update height
    if (node == root) {
        root = lp;
    }
    return lp; // lp is the new root of the subtree
}


// rotate left the subtree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::rotateLeft(AVLNode* node){
    AVLNode* rp = node->right;
    if (node->parent!=NULL) {
        if (node->parent->left == node) {
            node->parent->left = rp;
        }else{
            node->parent->right = rp;
        }
    }

    if (rp->left != NULL) {
       rp->left->parent = node;
    }

    rp->parent = node->parent;

    node->right = rp->left;
    rp->left = node;
    node->parent = rp;
    node->parent = rp;
    updateHeight(node);
    updateHeight(rp);
    if (node == root) {
        root = rp;
    }
    return rp;
}


// rebalance a tree rooted at node
// return the new root of the subtree
AVLNode* AVLTree::balance(AVLNode* node){
    updateHeight(node);
    if (balanceFactor(node) == 2) {
        if (balanceFactor(node->left) < 0) {
            node->left = rotateLeft(node->left); // for left right case
        }

        AVLNode* temp = rotateRight(node);
        updateHeight(temp);
        return temp;
    }

    if (balanceFactor(node) == -2) {
        if (balanceFactor(node->right) > 0) {
            node->right = rotateRight(node->right);  // for right left case
        }
        AVLNode* temp2 = rotateLeft(node);
        updateHeight(temp2);
        return temp2;
    }
    return node;
}

// insert a new node with (ss, na) to the AVL tree
// if there exists ss value, return false
// otherwise, insert it, balance the tree, return true
bool AVLTree::insert(string ss, string na){
     AVLNode *newNode=new AVLNode(ss,na);

     AVLNode *Iterator;
     if(root==NULL){
       cout<<"Root Node Inserted"<<endl;
       root=newNode;

     } else {

         Iterator = root; 
         int rootTempValue = atoi((Iterator->ssn).c_str()); 
         int addTempValue = atoi((newNode->ssn).c_str());



         if(rootTempValue <= addTempValue  ){
              // Right Portion of the tree
              while(Iterator->right != NULL){

                         cout << "In the Right portion" <<endl;


                       int rootTempValue2 = atoi((Iterator->right->ssn).c_str()); 
                       int addTempValue2 = atoi((newNode->ssn).c_str()) ;

                       if(rootTempValue2 <= addTempValue2 )             
                            Iterator = Iterator->right;
                       else 
                            Iterator = Iterator->left;       


                   //Iterator = Iterator->right;


              }

              Iterator->right = newNode ;
              newNode->parent = Iterator ;


         } else {


              // Left Portion of the tree
              while(Iterator->left != NULL){
                   //Iterator = Iterator->left;



                    int rootTempValue2 = atoi((Iterator->left->ssn).c_str()); 
                    int addTempValue2 = atoi((newNode->ssn).c_str()) ;

                    if(rootTempValue2 <= addTempValue2 )             
                          Iterator = Iterator->right;
                    else 
                         Iterator = Iterator->left;       



              }
              newNode->parent = Iterator;
              newNode->right = NULL ;
              newNode->left = NULL;
              Iterator->left = newNode ;

              cout << "In the left portion : " <<Iterator->left->ssn<<endl;

         }

     }

     balance(newNode);
     updateHeight(newNode);


    return true;
}


AVLNode* AVLTree::maxOfSubtree(AVLNode* node){
    if (node == NULL) {
        return NULL;
    }
    while (node->right != NULL) {
        node = node->right;
    }
    return node;
}

// delete the node containing value ss
// if there is not such node, return false
// otherwise, delete the node, balance the tree, return true
bool AVLTree::deleteNode(string ss){

    // please implement here
    return true;

}

// internal function
// do not call it directly
void AVLTree::print(AVLNode* x, int indent){
    if(x == NULL) 
         return;
    if (x->right != NULL) {
        print(x->right, indent+4);
    }

    if (indent != 0) {
        cout << std::setw(indent) << ' ';
    }

    if(x->right != NULL){
        cout << " /\n" << std::setw(indent) << ' ';
    }

    cout << x->ssn << endl;

    if (x->left != NULL) {
        cout << std::setw(indent) << ' ' <<" \\\n";
        print(x->left, indent+4);
    }

}

// print out the structure of the binary tree
// use it for debugging, I love this function
void AVLTree::print(){
    int count = 0;
    print(root, count);
}


// it does not level order traversal
// it prints out the number of node
// use it mainly for debugging
void AVLTree::levelOrder(){

// please implement it
}

ma​​in.cpp

#include <iostream>
#include "AVLTree.h"


int main(int argc, char** argv) {
  AVLTree temp;
  temp.insert("05", "a");
  temp.insert("04", "b");
  temp.insert("09", "c");
  //temp.insert("03", "d");
  //temp.insert("06", "d");
 // temp.insert("07", "d");
  //temp.insert("02", "d");

  temp.print();
  cout<<endl;
  cout<<"The Height Of The Tree is :" << temp.height(temp.getRoot())<<endl;

   cin.get();
    return 0;
}

【问题讨论】:

  • 一些备注:AVLTree.h 不在您的问题中。 AVLTree 的析构函数应该删除树拥有的节点。要调试这样的类,您可以添加一个bool invariant() const 方法来测试您的内部结构是否一致。然后,您可以在任何插入之前和之后调用它,以检测哪个插入破坏了您的不变量。然后您可以使用调试器更高效地进行调试。
  • 你能写那个函数,我可以检查如何测试它
  • 我已经写在答案里了。它未经测试,因此您可能会更正编译错误。

标签: c++ binary-tree binary-search-tree avl-tree


【解决方案1】:

为什么不直接使用std::stack?递归基本上只是按原样循环调用堆栈。

if (!root)
   root = new AVLNode(ss, na);
else
{
   AVLNode *current = root;
   AVLNode *previous = NULL;
   std::stack<AVLNode*> rstack;

   while (current != NULL)
   {
      previous = current;
         //Use String Compare instead of cast
      if (ss.compare(current->ssn) < 0) //If ss less than current
          ...

      rstack.push(previous);
   }
   ...
   ...

   while (!rstack.empty())
   {
      rstack.top() = balance(rstack.top());
      rstack.pop();
   }
} 

【讨论】:

    【解决方案2】:

    您的AVLTree 有一个复杂的类不变量,表达它通常是一个高效调试的好主意。

    如果你写一个类似的方法

    bool
    AVLTree::invariant() const {
      if (root == NULL)
        return true;
    
      std::stack<AVLNode*> stack;
      stack.push_back(root);
      while (!stack.empty()) {
        AVLNode* currentNode = stack.back();
        int leftHeight = -1, rightHeight = -1;
        if (currentNode->left) {
          leftHeight = currentNode->left->height;
          if (currentNode->left->parent != currentNode)
            return false;
          if (currentNode->left.height+1 != currentNode->height)
            return false;
        }
        if (currentNode->right) {
          rightHeight = currentNode->right->height;
          if (currentNode->left->parent != currentNode)
            return false;
          if (currentNode->left.height+1 != currentNode->height)
            return false;
        }
        if (leftHeigth > rightHeigth+1 || rightHeight > leftHeight+1)
          return false;
        if (currentNode->left)
          stack.push_back(currentNode->left);
        else {
          do {
            stack.pop_back();
            AVLNode* parentNode = !stack.empty() ? stack.back() : NULL;
            if (currentNode && parentNode->right != currentNode && parentNode->right) {
              stack.push_back(parentNode->right);
              break;
            };
            currentNode = parentNode;
          } while (currentNode);
        };
      };
      return true;
    }
    

    然后您可以通过添加以下代码来调试您的main 函数

    assert(temp.invariant());
    temp.insert("05", "a");
    assert(temp.invariant());
    temp.insert("04", "b");
    assert(temp.invariant());
    temp.insert("09", "c");
    assert(temp.invariant());
    

    一旦您确定了失败的插入,您只需在执行的invariant 方法中中断return false;。至此,您应该可以了解 bug 的由来了。

    【讨论】:

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