C++ C++;添加节点时出现二进制搜索树状态访问冲突错误
我在做一个二叉树的程序。向树中添加新节点时出错。我可以添加一个节点,但在添加另一个节点后,我会得到一个状态\u访问\u冲突错误。我认为错误可能与处理搜索函数的函数参数有关。如果可以的话,请帮助我 这是我写的he.h文件:C++ C++;添加节点时出现二进制搜索树状态访问冲突错误,c++,add,binary-search-tree,C++,Add,Binary Search Tree,我在做一个二叉树的程序。向树中添加新节点时出错。我可以添加一个节点,但在添加另一个节点后,我会得到一个状态\u访问\u冲突错误。我认为错误可能与处理搜索函数的函数参数有关。如果可以的话,请帮助我 这是我写的he.h文件: #ifndef P4_H #define P4_H #include <iostream> #include <iomanip> #include <fstream> #include <cctype> #include <
#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
\ifndef P4\u H
#定义P4_H
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
struct TreeNode//ListNode结构和组件
{
int acctNum;
TreeNode*左;
TreeNode*对;
};
类型定义树节点*nodePtr//定义指向treenode结构的指针
类树
{
私人:
无柄根;
国际货币基金组织;
公众:
树()
{root=NULL;numElements=0;}
bool treeEmpty()
{return(numElements==0);}
void addNode()
{
int键;
cout>键;
正确;
}
}
发现退货;
}
void deleteNode()
{
int键;
nodePtr location=NULL,parent=NULL;
cout>键;
不能离开;
最左侧->右侧=位置->右侧;
leftmostParent->left=NULL;
如果(父项->会计科目->位置->会计科目)
父->左=最左;
其他的
父->右=最左;
删除位置;
位置=空;
}
else if(位置->左!=NULL&&location->右==NULL)
{//删除只有左子树的节点
如果(父项->会计科目->位置->会计科目)
父->左=位置->左;
其他的
父项->右=位置->左;
删除位置;
位置=空;
}
else if(位置->左==NULL&&location->右!=NULL)
{//删除只有右子树的节点
nodePtr leftmost=location->right,leftmostpart=NULL;
while(最左侧->左侧!=NULL)
{
leftmostParent=最左侧;
最左侧=最左侧->左侧;
}
最左侧->右侧=位置->右侧;
leftmostParent->left=NULL;
如果(父项->会计科目->位置->会计科目)
父->左=最左;
其他的
父->右=最左;
删除位置;
位置=空;
}
其他的
{//删除叶节点
如果(位置->会计科目->父项->会计科目)
父->右=空;
其他的
父->左=空;
删除位置;
位置=空;
}
}
void displayscend(nodePtr p,int count)
{
如果(p->left!=NULL)
显示上升(p->左,计数);
不正确!=空)
显示上升(p->右,计数);
cout在searchTree中,在循环之前将parent设置为NULL,然后在循环中取消引用它。在哪一行获得访问冲突?可能的重复应该真正将添加的信息编辑到原始问题中,而不是发布另一个几乎相同的问题。但是现在我在显示节点时出错请提供一些细节,也许有人能帮你。