Hello, I am fairly new to programming, so any help would be much appreciated.
Basically this program gets a file with the contents like:
hello
one
one
Tt then sorts these into a binary tree, i can do that fine, however what i am struggling with is converting this to another binary tree sorted by a different property.
I'm getting a "Unhandled exception at 0x104a942c (msvcp90d.dll) : 0xC0000005: Access violation reading location 0xcdcdcde5." error in like line 1671 of xstring :|
> return (_Mysize);
Your help would be much appreciated <3 p.s. don't be too harsh on me, im only learning ;_;
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct textStats
{
int totalWords;
int distinctWords;
int uniqueWords;
};
struct text
{
string word;
int count;
};
struct Node
{
text value;
Node* pLeft;
Node* pRight;
};
//alpha
Node* alphaAdd(Node* tree, text value);
Node* alphaAddNode(Node* tree, Node* toAdd);
Node* alphaMake (string fin, Node* tree);
// Freq
Node* freqAdd(Node* tree, text value);
Node* freqAddNode(Node* tree, Node* toAdd);
Node* freqMake(Node* tree);
// Other
Node* find(Node* tree, string word);
void display(Node* tree);
void main()
{
string fin;
Node* alphaTree = 0;
Node* freq = 0;
text value;
bool fileLoaded = false;
while (!fileLoaded)
{
cout << "Enter the input file (including the .ptx file extension)" << endl;
cin >> fin;
ifstream _fin(fin.c_str());
if (!_fin)
{
cout << "Error opening input file, does the file exsist?" << endl;
}
else
{
cout << "File Loaded" << endl;
fileLoaded = true;
}
_fin.close();
}
alphaTree = alphaMake (fin, alphaTree);
display(alphaTree);
freq = freqMake(alphaTree);
display(freq);
}
Node* alphaMake (string fin, Node* tree)
{
string str;
ifstream _fin(fin.c_str());
Node* found;
text value;
value.count = 0;
while(_fin >> str)
{
value.word = str;
found = find(tree, str);
if (found != 0)
{
found->value.count++;
}
else
{
value.word =str;
value.count = 1;
tree = alphaAdd(tree, value);
}
}
return tree;
}
Node* alphaAdd(Node* tree, text value)
{
Node* tempTree = new Node;
tempTree->value = value;
tempTree->pLeft = 0;
tempTree->pRight = 0;
return alphaAddNode(tree, tempTree);
}
Node* alphaAddNode(Node* tree, Node *toAdd)
{
if (toAdd == 0)
{
return tree;
}
else if (tree ==0)
{
return toAdd;
}
else
{
if (toAdd->value.word < tree->value.word)
{
tree->pLeft = alphaAddNode(tree->pLeft, toAdd);
return tree;
}
else
{
tree->pRight = alphaAddNode(tree->pRight, toAdd);
return tree;
}
}
}
Node* find(Node* tree, string word)
{
if (tree == 0)
{
return tree;
}
else if (tree->value.word == word)
{
return tree;
}
else if (word < tree->value.word)
{
return find(tree->pLeft, word);
}
else
{
return find(tree->pRight, word);
}
}
void display(Node* tree)
{
if (tree !=0)
{
display(tree->pLeft);
cout << tree->value.word << "( " << tree->value.count << " )" << endl;
display(tree->pRight);
}
}
Node* freqMake(Node* tree)
{
text value;
Node* freqTree = new Node;
if (tree != 0)
{
freqMake(tree->pLeft);
value = tree->value;
freqTree = freqAdd(freqTree, value);
freqMake(tree->pRight);
}
return freqTree;
}
Node* freqAdd(Node* tree, text value)
{
Node* temp = new Node;
temp->value = value;
temp->pLeft = 0;
temp->pRight = 0;
return alphaAddNode(tree, temp);
}
Node* freqAddNode(Node* tree, Node *toAdd)
{
if (toAdd == 0)
{
return tree;
}
else if (tree ==0)
{
return toAdd;
}
else
{
if (toAdd->value.word > tree->value.word)
{
tree->pLeft = freqAddNode(tree->pLeft, toAdd);
return tree;
}
else
{
tree->pRight = freqAddNode(tree->pRight, toAdd);
return tree;
}
}
}