私はバイナリツリーで動作するプログラムを開発中です。ツリーに新しいノードを追加するとエラーが発生します。私は1つのノードを追加することができますが、別のノードを追加した後、STATUS_ACCESS_VIOLATIONエラーが発生します。私は、エラーが検索関数を扱う関数の引数であると思う。あなたができるなら私を助けてください。ここでC++バイナリ検索ツリーのステータスアクセス違反でノードを追加するとエラーが発生する
が、彼は私が書いたファイル.Hです:searchTreeで
#ifndef P4_H
#define P4_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <string>
using namespace std;
struct TreeNode //ListNode structure with components
{
int acctNum;
TreeNode *left;
TreeNode *right;
};
typedef TreeNode* nodePtr; //defines a pointer to a treenode struct
class Tree
{
private:
nodePtr root;
int numElements;
public:
Tree()
{ root = NULL; numElements = 0; }
bool treeEmpty()
{ return (numElements == 0); }
void addNode()
{
int key;
cout << "Enter account number to add: ";
cin >> key;
cout << endl << endl;
nodePtr location = NULL, parent = NULL, p = NULL;
bool found = searchTree(key, &location, &parent);
cout << found << " " << location << " " << parent << endl << endl;
if (found)
{
cout << "Error! Account number: " << key << " already exists within"
<< " the tree." << endl << endl;
}
else
{
if (parent == NULL)
{
root = new TreeNode;
root->acctNum = key;
}
else
{
if (parent->acctNum > key)
{
parent->left = new TreeNode;
p = parent->left;
p->acctNum = key;
}
else
{
parent->right = new TreeNode;
p = parent->right;
p->acctNum = key;
}
}
}
}
bool searchTree(int key, nodePtr *location, nodePtr *parent)
{
bool found = false;
nodePtr p = root;
*location = root;
parent = NULL;
while (p != NULL && !found)
{
*location = p;
if (key == p->acctNum)
found = true;
else if (key < p->acctNum)
{
*parent = p;
p = p->left;
}
else if (key > p->acctNum)
{
*parent = p;
p = p->right;
}
}
return found;
}
void deleteNode()
{
int key;
nodePtr location = NULL, parent = NULL;
cout << "Enter account number to delete: ";
cin >> key;
cout << endl << endl;
bool found = searchTree(key, &location, &parent);
if (!found)
{
cout << "Error! Account number: " << key << " was not found."
<< endl << endl;
}
else if (location->left != NULL && location->right != NULL)
{ //delete node with left and right subtrees
nodePtr leftmost = location->right, leftmostParent = NULL;
while (leftmost->left != NULL)
{
leftmostParent = leftmost;
leftmost = leftmost->left;
}
leftmost->left = location->left;
leftmost->right = location->right;
leftmostParent->left = NULL;
if (parent->acctNum > location->acctNum)
parent->left = leftmost;
else
parent->right = leftmost;
delete location;
location = NULL;
}
else if (location->left != NULL && location->right == NULL)
{ //delete node with only a left subtree
if (parent->acctNum > location->acctNum)
parent->left = location->left;
else
parent->right = location->left;
delete location;
location = NULL;
}
else if (location->left == NULL && location->right != NULL)
{ //delete node with only a right subtree
nodePtr leftmost = location->right, leftmostParent = NULL;
while (leftmost->left != NULL)
{
leftmostParent = leftmost;
leftmost = leftmost->left;
}
leftmost->right = location->right;
leftmostParent->left = NULL;
if (parent->acctNum > location->acctNum)
parent->left = leftmost;
else
parent->right = leftmost;
delete location;
location = NULL;
}
else
{ //delete a leaf node
if (location->acctNum > parent->acctNum)
parent->right = NULL;
else
parent->left = NULL;
delete location;
location = NULL;
}
}
void displayAscend(nodePtr p, int count)
{
if (p->left != NULL)
displayAscend(p->left, count);
cout << count << ". " << p->acctNum << endl;
count ++;
if (p->right != NULL)
displayAscend(p->right, count);
}
void displayDescend(nodePtr p, int count)
{
if (p->right != NULL)
displayAscend(p->right, count);
cout << count << ". " << p->acctNum << endl;
count ++;
if (p->left != NULL)
displayAscend(p->left, count);
}
void readFile()
{
char filename[50];
cout << "Enter name of file to open: ";
cin.getline(filename,51);
cout << endl << endl;
ifstream inFile;
inFile.open(filename);
while (!inFile)
{
cout << "Error opening file! Please try again." << endl << endl;
cout << "Enter name of file: ";
cin.getline(filename,51);
cout << endl << endl;
}
int num;
while (!inFile.eof())
{
inFile >> num;
nodePtr location = NULL, parent = NULL, p = NULL;
bool found = searchTree(num, &location, &parent);
if (found)
{
cout << "Error! Account number: " << num << " already exists"
<< " within the tree." << endl << endl;
}
else
{
if (parent == NULL)
{
root = new TreeNode;
root->acctNum = num;
}
else
{
if (parent->acctNum > num)
{
parent->left = new TreeNode;
p = parent->left;
p->acctNum = num;
}
else
{
parent->right = new TreeNode;
p = parent->right;
p->acctNum = num;
}
}
}
}
}
};
#endif
アクセス違反はどの行にありますか? – JaredPar
[C++バイナリ検索ツリーのエラー:ノードの追加でエラーが発生する](http://stackoverflow.com/questions/10355294/c-binary-search-tree-error-with-adding-nodes)追加された情報を実際に編集する必要がありますほぼ同一の別のものを投稿するのではなく、元の質問に変換します。 –