AVL Tree Insertion - Access Violation Reading Location (Unable to read memory)

0

Any help would be much appreciated. :)

Here is the error I am getting at run-time: Unhandled exception at 0x000D87D6 in FinalProject.exe: 0xC0000005: Access violation reading location 0x00000013. When I try to insert the third word, it breaks here: http://i.imgur.com/30brTkl.png

See where it says "Unable to read memory"? I can't figure out why.

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;
}
c++
memory
insert
tree
asked on Stack Overflow Jul 12, 2014 by Spikke • edited Jul 12, 2014 by Spikke

1 Answer

1

Compile with:

g++ -Wall -Wextra  -g -I. *.cpp

Code:

// ----- 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; }
answered on Stack Overflow Jul 12, 2014 by Adam Burry

User contributions licensed under CC BY-SA 3.0