AVL树和分段错误

时间:2016-10-26 06:51:35

标签: c++ avl-tree

我正在尝试做一个AVL树并且遇到了分段错误。在这里做了一些研究后,我知道我试图访问我无法访问的内存。问题是我无法弄清楚我做错了什么。

我尝试调试,命令提示符说

  

警告:GDB:无法设置控制终端:不允许操作

这是我的头文件

#ifndef AVLTREE_H_
#define AVLTREE_H_

#include <queue>
#include <iostream>

using namespace std;
static const int ALLOWED_IMBALANCE = 1;
template<class T>
class AVLNode {
public:
    // Default blank AVLNode constructor
    AVLNode() {
        left = right = nullptr;
        height = 0;
    }

    // Constructor with provided element (data) and children
    AVLNode(const T& el, AVLNode *l = nullptr, AVLNode *r = nullptr) {
        key = el;
        left = l;
        right = r;
        height = 0;
    }

    T key;                  // Key to compare/insert on of type <T>
    AVLNode *left, *right;  // Children of this node
    int height;             // Height of this node in the tree
};

template<class T>
class AVLTree {
  public:

    AVLTree() { root = nullptr; }

    void insert(const T& key) { insert(root, key); }

    void printTree() { printTree(root); }

    bool contains( const T& key ){ return(contains(root, key)); }

    void remove( const T& key ){ remove(root, key); }

    int Height(AVLNode<T> * &key) {
    return key == nullptr ? -1:key->height;}

    void balance(const T& key);



  private:
    AVLNode<T>* root;

    AVLNode<T> *findMin(AVLNode<T>* &node){
          if (node !=nullptr){
             while( node->left !=nullptr){
                node = node->left;
              }
            }
       return node;
    }

     void  rightRotation(AVLNode<T>* &node) {
        AVLNode<T>* left = node->left;

       node->left = left->right;
       left->right = node;
       node = left;
    }


      void leftRotation(AVLNode<T>* &node) {
         AVLNode<T>* right = node->right;

         node->right = right->left;
         right->left = node;
         node = right;
       }



     void insert(AVLNode<T>* &node, const T& key) {
         if(node == nullptr) {
            node = new AVLNode<T> (key);
          }
         else if(key > node->key) {
            insert(node->right, key);
          }
         else {
            insert(node->left, key);
          }
      balance( node);
     } 


   void   balance(AVLNode<T> * &node){
    if (node == nullptr){
        return;}

    if (Height(node->left) - Height(node->right) > ALLOWED_IMBALANCE )                   {
        if(Height (node->left->left) >= Height(node->left->right)){
            rightRotation(node);
        }
        else{
           leftRotation(node);}

    }

    else{
        if (Height(node->right) - Height(node->left) > ALLOWED_IMBALANCE ){
             if(Height (node->right->right) >= Height(node->right->left)){
            leftRotation(node);
        }
        else{
           rightRotation(node);}
        }
    }

    node->height = max( Height(node->left),Height(node->right))+1;
    }




//check contain of the tree
    bool contains(AVLNode<T>* root, const T& key) {


   if (root = nullptr){
   return false;}

   else if ( key < root->key){
    return contains (root, key);

    }

    else if (key > root->key){
    return contains(root, key);
    }

    else
    return true;
    }

    // remove contain of a tree

     void remove( AVLNode<T>* root, const T& key) {

        AVLNode<T> *oldNode = root;
      if ( root == nullptr){
      return;
      }

      if(key < root->key){
            remove(root->left, key);
      }

      else if (key > root->key){
        remove (root->right,key);
      }

      else if( (root->left != nullptr && root->right != nullptr)){
        root->key = findMin( root->right)->key;
        remove(root->right, root->key);
      }

      else{


        root = ( root->left !=nullptr) ? root->left : root->right;
        delete oldNode;
      }

      balance(root);
    }

    // Should do a level order printout with actual depth (no alignment)
     void printTree(AVLNode<T>* node) {
      queue<AVLNode<T>*> bufQueue;
      int curr_height = node->height;
      bufQueue.push(node);
      while( !bufQueue.empty() ) {
        AVLNode<T>* curr = bufQueue.front();
        if( curr->left  != nullptr ){ bufQueue.push(curr->left ); }
        if( curr->right != nullptr ){ bufQueue.push(curr->right); }
        if( curr->height < curr_height ){
          cout << endl;
          curr_height = curr->height;
        }
        cout << curr->key << " ";
        bufQueue.pop();
      }
      cout << endl;
    }


  // end private
};

#endif

和主要

  #include <iostream>
  #include <fstream>
  #include <vector>
  #include <stdlib.h>  // rand
  #include <time.h>    // time for srand
  #include "AVL.h"

int main() {
  AVLTree<int> myTree;

  cout << "Sample series of inserts. Will do all 4 cases of rotates." << endl;
  myTree.insert(20);
  myTree.insert(10);
  myTree.insert(5);
  myTree.insert(30);
  myTree.insert(40);
  myTree.insert(15);
  myTree.insert(18);
  myTree.insert(13);
  myTree.insert(4);
  myTree.insert(19);
  myTree.printTree();

  if( myTree.contains(15) == true){
    cout << "Tree contains 15" << endl;
  }else{
    cout << "Tree does not contain 15" << endl;
  }

  myTree.remove(15);
  if( myTree.contains(15) == true){
    cout << "Tree contains 15" << endl;
  }else{
    cout << "Tree does not contain 15" << endl;
  }


  myTree.printTree();


  return(0);
}

0 个答案:

没有答案
相关问题