非常感谢任何帮助。 :)
以下是我在运行时获得的错误:FinalProject.exe中0x000D87D6处的未处理异常:0xC0000005:访问冲突读取位置0x00000013。 当我尝试插入第三个单词时,它会在这里打破: http://i.imgur.com/30brTkl.png
查看“无法读取内存”的位置?我无法弄清楚原因。
FinalProject.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;
}
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;
}
答案 0 :(得分:1)
编译:
g++ -Wall -Wextra -g -I. *.cpp
代码:
// ----- 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; }