C++ AVL树插入-访问冲突读取位置(无法读取内存)

C++ AVL树插入-访问冲突读取位置(无法读取内存),c++,memory,insert,tree,C++,Memory,Insert,Tree,任何帮助都将不胜感激。:) 以下是我在运行时遇到的错误:FinalProject.exe中0x000D87D6处的未处理异常:0xC0000005:访问冲突读取位置0x00000013。 当我尝试插入第三个单词时,它在此处中断: 看到哪里写着“无法读取内存”了吗?我不明白为什么 最终项目.cpp: #include "FileReaderAscii.h" #include "AVL.h" int main() { std::string FileName = "I.txt";

任何帮助都将不胜感激。:)

以下是我在运行时遇到的错误:FinalProject.exe中0x000D87D6处的未处理异常:0xC0000005:访问冲突读取位置0x00000013。 当我尝试插入第三个单词时,它在此处中断:

看到哪里写着“无法读取内存”了吗?我不明白为什么

最终项目.cpp

#include "FileReaderAscii.h"
#include "AVL.h"

int main()
{
    std::string FileName = "I.txt";

    FileReaderAscii F;
    AVL A;

    std::set<std::string> ISet = F.ReadFile(FileName);

    for (std::set<std::string>::iterator it = ISet.begin(); it != ISet.end(); it++) {
        A.root = A.insert(A.root, *it);
    }

    //3.    Display the top three levels of your AVL tree
    A.displayAVL(A.root, 3);

    return 0;
}
#包括“FileReaderAscii.h”
#包括“AVL.h”
int main()
{
std::string FileName=“I.txt”;
FileReaderAscii F;
AVL A;
std::set ISet=F.ReadFile(文件名);
for(std::set::iterator it=ISet.begin();it!=ISet.end();it++){
A.root=A.insert(A.root,*it);
}
//3.显示AVL树的前三级
A.displayAVL(A.root,3);
返回0;
}
AVL.cpp:

#include "AVL.h"

int AVL::heightAVL(AVLNode *temp) {
    int iFinalHeight = 0;
    if (temp != NULL) {
        int lHeight = heightAVL(temp->left);
        int rHeight = heightAVL(temp->right);
        int topHeight = std::max(lHeight, rHeight);
        iFinalHeight = topHeight + 1;
    }
    return iFinalHeight;
}

int AVL::diffAVL(AVLNode *temp) {
    int lHeight = heightAVL(temp->left);
    int rHeight = heightAVL(temp->right);
    int iBalance = lHeight - rHeight;

    return iBalance;
}

AVL::AVLNode *AVL::rrRotation(AVLNode *parent) {
    AVLNode *temp;
    temp = parent->right;
    parent->right = temp->left;
    temp->left = parent;
    return temp;
}

AVL::AVLNode *AVL::llRotation(AVLNode *parent) {
    AVLNode *temp;
    temp = parent->left;
    parent->left = temp->right;
    temp->right = parent;
    return temp;
}

AVL::AVLNode *AVL::lrRotation(AVLNode *parent) {
    AVLNode *temp;
    temp = parent->left;
    parent->left = rrRotation(temp);
    return llRotation(parent);
}

AVL::AVLNode *AVL::rlRotation(AVLNode *parent) {
    AVLNode *temp;
    temp = parent->right;
    parent->right = llRotation(temp);
    return rrRotation(parent);
}

AVL::AVLNode *AVL::balance(AVLNode *temp) {
    int iBalance = diffAVL(temp);

    if (iBalance > 1) {
        if (diffAVL(temp->left) > 0) {
            temp = llRotation(temp);
        }
        else {
            temp = lrRotation(temp);
        }
    }
    else if (iBalance < -1) {
        if (diffAVL(temp->right) > 0) {
            temp = rlRotation(temp);
        }
        else {
            temp = rrRotation(temp);
        }

        return temp;
    }
}

AVL::AVLNode *AVL::insert(AVLNode *root, std::string insertion) {
    if (root == NULL) {
        root = new AVLNode;
        root->inputData = insertion;
        root->left = NULL;
        root->right = NULL;

        //insertion is complete, escape function
        return root;
    }
    else if (insertion < root->inputData) {
        root->left = insert(root->left, insertion);
        root = balance(root);
    }
    else if (insertion >= root->inputData) {
        root->right = insert(root->right, insertion);
        root = balance(root);
    }

    return root;
}

void AVL::displayAVL(AVLNode *p, int l) {
    if (p != NULL) {
        displayAVL(p->right, l + 1);

        std::cout << std::endl;

        if (p == root) {
            std::cout << "Root > ";
        }

        for (int i = 0; i < l && p != root; i++) {
            std::cout << "     ";
        }

        std::cout << p->inputData;

        displayAVL(p->left, l + 1);
    }
}

AVL::AVL() {
    root = NULL;
}
#包括“AVL.h”
内部AVL::高度AVL(AVLNode*temp){
int-iFinalHeight=0;
如果(温度!=NULL){
内部高度=高度AVL(温度->左侧);
int rHeight=高度AVL(温度->右侧);
int TOPHEIGH=std::max(左高、右高);
iFinalHeight=顶高+1;
}
返回高度;
}
int AVL::diffAVL(AVLNode*temp){
内部高度=高度AVL(温度->左侧);
int rHeight=高度AVL(温度->右侧);
int iBlance=lHeight-rHeight;
回归平衡;
}
AVL::AVLNode*AVL::rrRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->右侧;
父级->右=临时->左;
临时->左=父级;
返回温度;
}
AVL::AVLNode*AVL::llRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->左;
父项->左=临时->右;
临时->右=父级;
返回温度;
}
AVL::AVLNode*AVL::lrRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->左;
父级->左=rr旋转(温度);
返回(父级);
}
AVL::AVLNode*AVL::RL旋转(AVLNode*父级){
AVLNode*温度;
临时=父级->右侧;
父级->右侧=旋转(温度);
返回(父级);
}
AVL::AVLNode*AVL::余额(AVLNode*temp){
int iBalance=diffAVL(温度);
如果(iBlance>1){
如果(diffAVL(温度->左)>0){
温度=旋转(温度);
}
否则{
温度=LR旋转(温度);
}
}
否则如果(iBalance<-1){
如果(diffAVL(温度->右侧)>0){
温度=旋转(温度);
}
否则{
温度=旋转(温度);
}
返回温度;
}
}
AVL::AVLNode*AVL::insert(AVLNode*根,std::字符串插入){
if(root==NULL){
根=新的AVLNode;
根->输入数据=插入;
根->左=空;
root->right=NULL;
//插入完成,退出功能
返回根;
}
else if(插入inputData){
根->左=插入(根->左,插入);
根=平衡(根);
}
else if(插入>=根->输入数据){
根->右=插入(根->右,插入);
根=平衡(根);
}
返回根;
}
void AVL::displayAVL(AVLNode*p,intl){
如果(p!=NULL){
显示AVL(p->右,l+1);
std::cout编译时使用:

g++ -Wall -Wextra  -g -I. *.cpp
代码:

/----avl.h-----
#包括
结构AVL{
结构AVLNode{
AVLNode*左;
AVLNode*右;
std::string inputData;};
内部高度avl(AVLNode*temp);
int diffAVL(AVLNode*temp);
AVLNode*rrRotation(AVLNode*parent);
AVLNode*llRotation(AVLNode*parent);
AVLNode*lrRotation(AVLNode*父节点);
AVLNode*RL旋转(AVLNode*父节点);
AVLNode*平衡(AVLNode*温度);
AVLNode*插入(AVLNode*根,标准::字符串插入);
无效显示AVL(AVLNode*p,int l);
AVL();
AVLNode*root;};
//----avl.cpp-----
#包括“avl.h”
#包括
内部AVL::高度AVL(AVLNode*temp){
int-iFinalHeight=0;
如果(温度!=NULL){
内部高度=高度AVL(温度->左侧);
int rHeight=高度AVL(温度->右侧);
int TOPHEIGH=std::max(左高、右高);
iFinalHeight=topHeight+1;}
返回iFinalHeight;}
int AVL::diffAVL(AVLNode*temp){
内部高度=高度AVL(温度->左侧);
int rHeight=高度AVL(温度->右侧);
int iBlance=lHeight-rHeight;
返回iBalance;}
AVL::AVLNode*AVL::rrRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->右侧;
父级->右=临时->左;
临时->左=父级;
返回温度;}
AVL::AVLNode*AVL::llRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->左;
父项->左=临时->右;
临时->右=父级;
返回温度;}
AVL::AVLNode*AVL::lrRotation(AVLNode*父级){
AVLNode*温度;
临时=父级->左;
父级->左=rr旋转(温度);
返回llRotation(父级);}
AVL::AVLNode*AVL::RL旋转(AVLNode*父级){
AVLNode*温度;
临时=父级->右侧;
父级->右侧=旋转(温度);
返回(父级);}
AVL::AVLNode*AVL::余额(AVLNode*temp){
int iBalance=diffAVL(温度);
如果(iBlance>1){
如果(diffAVL(温度->左)>0){
温度=旋转(温度);}
否则{
temp=lr旋转(temp);}
否则如果(iBalance<-1){
如果(diffAVL(温度->右侧)>0){
温度=旋转(温度);}
否则{
temp=rrRotation(temp);}
返回温度;}
AVL::AVLNode*AVL::insert(AVLNode*根,std::字符串插入){
if(root==NULL){
根=新的AVLNode;
根->输入数据=插入;
根->左=空;
root->right=NULL;
返回根;}
else if(插入inputData){
根->左=插入(根->左,插入);}
else{/*if(插入>=根->输入数据)*/
根->右=插入(根->右,插入);}
根=平衡(根);
返回根;}
void AVL::displayAVL(AVLNode*p,intl){
如果(p!=NULL){
显示AVL(p->右,l+1);
标准::cout>v){
d、 插入(v);}
返回d;}
int main(){
AVL A;
std::set IndepSet=testData();
for(std::set::iterator it=IndepSet.begin();
it!=IndepSet.end();it
// ----- avl.h -----
#include <string>

struct AVL {

  struct AVLNode {
    AVLNode* left;
    AVLNode* right;
    std::string inputData; };

  int heightAVL(AVLNode* temp);
  int diffAVL(AVLNode* temp);
  AVLNode* rrRotation(AVLNode* parent);
  AVLNode* llRotation(AVLNode* parent);
  AVLNode* lrRotation(AVLNode* parent);
  AVLNode* rlRotation(AVLNode* parent);
  AVLNode* balance(AVLNode* temp);
  AVLNode* insert(AVLNode* root, std::string insertion);
  void displayAVL(AVLNode* p, int l);
  AVL();

  AVLNode* root; };

// ----- avl.cpp -----
#include "avl.h"
#include <iostream>

int AVL::heightAVL(AVLNode* temp) {
  int iFinalHeight = 0;

  if (temp != NULL) {
    int lHeight = heightAVL(temp->left);
    int rHeight = heightAVL(temp->right);
    int topHeight = std::max(lHeight, rHeight);
    iFinalHeight = topHeight + 1; }

  return iFinalHeight; }


int AVL::diffAVL(AVLNode* temp) {
  int lHeight = heightAVL(temp->left);
  int rHeight = heightAVL(temp->right);
  int iBalance = lHeight - rHeight;
  return iBalance; }


AVL::AVLNode* AVL::rrRotation(AVLNode* parent) {
  AVLNode* temp;
  temp = parent->right;
  parent->right = temp->left;
  temp->left = parent;
  return temp; }


AVL::AVLNode* AVL::llRotation(AVLNode* parent) {
  AVLNode* temp;
  temp = parent->left;
  parent->left = temp->right;
  temp->right = parent;
  return temp; }


AVL::AVLNode* AVL::lrRotation(AVLNode* parent) {
  AVLNode* temp;
  temp = parent->left;
  parent->left = rrRotation(temp);
  return llRotation(parent); }


AVL::AVLNode* AVL::rlRotation(AVLNode* parent) {
  AVLNode* temp;
  temp = parent->right;
  parent->right = llRotation(temp);
  return rrRotation(parent); }


AVL::AVLNode* AVL::balance(AVLNode* temp) {
  int iBalance = diffAVL(temp);

  if (iBalance > 1) {
    if (diffAVL(temp->left) > 0) {
      temp = llRotation(temp); }
    else {
      temp = lrRotation(temp); } }
  else if (iBalance < -1) {
    if (diffAVL(temp->right) > 0) {
      temp = rlRotation(temp); }
    else {
      temp = rrRotation(temp); } }

  return temp; }


AVL::AVLNode* AVL::insert(AVLNode* root, std::string insertion) {
  if (root == NULL) {
    root = new AVLNode;
    root->inputData = insertion;
    root->left = NULL;
    root->right = NULL;
    return root; }
  else if (insertion < root->inputData) {
    root->left = insert(root->left, insertion); }
  else { /*if (insertion >= root->inputData)*/
    root->right = insert(root->right, insertion); }

  root = balance(root);
  return root; }


void AVL::displayAVL(AVLNode* p, int l) {
  if (p != NULL) {
    displayAVL(p->right, l + 1);
    std::cout << std::endl;

    if (p == root) {
      std::cout << "Root > "; }

    for (int i = 0; i < l && p != root; i++) {
      std::cout << "     "; }

    std::cout << p->inputData;
    displayAVL(p->left, l + 1); } }


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

// ----- main.cpp -----
#include "avl.h"
#include <fstream>
#include <set>
#include <string>

std::set<std::string> testData() {
  std::ifstream fin("avl.cpp");
  std::set<std::string> d;
  std::string v;

  while (fin >> v) {
    d.insert(v); }

  return d; }


int main() {
  AVL A;
  std::set<std::string> IndepSet = testData();

  for (std::set<std::string>::iterator it = IndepSet.begin();
       it != IndepSet.end(); it++) {
    A.root = A.insert(A.root, *it); }

  //3.    Display the top three levels of your AVL tree
  A.displayAVL(A.root, 3);
  return 0; }