C++ 二叉搜索树--删除函数不工作 #包括 #包括 #包括 使用名称空间std; 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); void LoadTree(const char文件[]); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 BinaryTree::BinaryTree() { Root=NULL; } 模板 void BinaryTree::insertNode(T键,字符串Val) { 树节点**温度=&根节点; TreeNode*temp1=NULL; 如果(*temp==NULL) { 根=新树节点(键,Val); 返回; } 其他的 { while(*temp!=NULL) { temp1=*temp; 如果(temp1->key>key) { temp=&(*temp)->LeftChild; } else if(temp1->keyRightChild; } } } *temp=新树节点(键,Val); (*temp)->Parent=temp1; } 模板 void BinaryTree::LoadTree(const char*文件) { 流鳍; 打开(文件); 字符串缓冲区; T浅黄色; 而(!fin.eof()) { getline(fin,buffer,“~”); 鳍>>浅黄色; 如果(!buff) 继续; insertNode(buff,buffer); } fin.close(); } void BinaryTree::deleteNode(T键) { 树节点*温度=根节点; 树节点*temp1=温度; while(temp!=NULL) { 如果(临时->键==键) { temp1=temp; if(temp==根) { TreeNode*temp2=根; temp2=temp2->RightChild; while(temp2->LeftChild!=NULL) { temp2=temp2->LeftChild; } temp->key=temp2->key; temp->value=temp2->value; 删除temp2; } else if(temp->RightChild==NULL) { TreeNode*temp2=温度; temp2=temp->Parent; if(temp2->RightChild==temp) { temp2->RightChild=temp->LeftChild; (temp->LeftChild)->Parent=temp2; } else if(temp2->LeftChild==temp) { temp2->LeftChild=temp->LeftChild; (temp->LeftChild)->Parent=temp2; } 删除temp1; 删除临时文件; 返回; } else if(temp->LeftChild==NULL) { TreeNode*temp2=温度; temp2=temp->Parent; if(temp2->RightChild==temp) { temp2->RightChild=temp->RightChild; (temp->RightChild)->Parent=temp2; } else if(temp2->LeftChild==temp) { temp2->LeftChild=temp->RightChild; (temp->RightChild)->Parent=temp2; } 删除temp1; 删除临时文件; 返回; } 如果(temp->RightChild!=NULL),则为else { TreeNode*tmp=温度; temp=temp->RightChild; if(temp->LeftChild!=NULL) { while(temp->LeftChild!=NULL) { tmp=温度; temp=temp->LeftChild; } } 如果(tmp!=temp1) tmp->LeftChild==NULL; 其他的 temp1->RightChild==NULL; temp1->key=temp->key; temp1->value=temp->value; 删除临时文件; 返回; } } 如果(临时->按键>按键) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } 返回; }
这是我正在制作的二进制搜索树代码的一部分。当我运行删除功能时,我遇到了一个问题,即对于某些节点,我收到错误“testing.exe已停止工作”.testing是我的主文件的名称。我已经检查了一遍又一遍,但似乎没有发现问题。有人看到问题了吗?感谢在您删除C++ 二叉搜索树--删除函数不工作 #包括 #包括 #包括 使用名称空间std; 模板 树状结构{ 字符串值; T键; TreeNode*父代; TreeNode*LeftChild; TreeNode*右儿童; 树节点(Tk,字符串值) { 该->值=Val; 该->键=k; 此->父项=空; 此->LeftChild=NULL; 此->RightChild=NULL; } }; 模板 类二叉树{ 私人: 树根; 公众: 二叉树(); void LoadTree(const char文件[]); ~BinaryTree(); void insertNode(T键,字符串Val); 无效删除节点(T键); 字符串搜索节点(T键); void UpdateKey(T newkey,T oldkey); 内部高度(TreeNode*节点); int高度(); }; 模板 BinaryTree::BinaryTree() { Root=NULL; } 模板 void BinaryTree::insertNode(T键,字符串Val) { 树节点**温度=&根节点; TreeNode*temp1=NULL; 如果(*temp==NULL) { 根=新树节点(键,Val); 返回; } 其他的 { while(*temp!=NULL) { temp1=*temp; 如果(temp1->key>key) { temp=&(*temp)->LeftChild; } else if(temp1->keyRightChild; } } } *temp=新树节点(键,Val); (*temp)->Parent=temp1; } 模板 void BinaryTree::LoadTree(const char*文件) { 流鳍; 打开(文件); 字符串缓冲区; T浅黄色; 而(!fin.eof()) { getline(fin,buffer,“~”); 鳍>>浅黄色; 如果(!buff) 继续; insertNode(buff,buffer); } fin.close(); } void BinaryTree::deleteNode(T键) { 树节点*温度=根节点; 树节点*temp1=温度; while(temp!=NULL) { 如果(临时->键==键) { temp1=temp; if(temp==根) { TreeNode*temp2=根; temp2=temp2->RightChild; while(temp2->LeftChild!=NULL) { temp2=temp2->LeftChild; } temp->key=temp2->key; temp->value=temp2->value; 删除temp2; } else if(temp->RightChild==NULL) { TreeNode*temp2=温度; temp2=temp->Parent; if(temp2->RightChild==temp) { temp2->RightChild=temp->LeftChild; (temp->LeftChild)->Parent=temp2; } else if(temp2->LeftChild==temp) { temp2->LeftChild=temp->LeftChild; (temp->LeftChild)->Parent=temp2; } 删除temp1; 删除临时文件; 返回; } else if(temp->LeftChild==NULL) { TreeNode*temp2=温度; temp2=temp->Parent; if(temp2->RightChild==temp) { temp2->RightChild=temp->RightChild; (temp->RightChild)->Parent=temp2; } else if(temp2->LeftChild==temp) { temp2->LeftChild=temp->RightChild; (temp->RightChild)->Parent=temp2; } 删除temp1; 删除临时文件; 返回; } 如果(temp->RightChild!=NULL),则为else { TreeNode*tmp=温度; temp=temp->RightChild; if(temp->LeftChild!=NULL) { while(temp->LeftChild!=NULL) { tmp=温度; temp=temp->LeftChild; } } 如果(tmp!=temp1) tmp->LeftChild==NULL; 其他的 temp1->RightChild==NULL; temp1->key=temp->key; temp1->value=temp->value; 删除临时文件; 返回; } } 如果(临时->按键>按键) { temp=temp->LeftChild; } 否则如果(temp->keyRightChild; } } 返回; },c++,C++,这是我正在制作的二进制搜索树代码的一部分。当我运行删除功能时,我遇到了一个问题,即对于某些节点,我收到错误“testing.exe已停止工作”.testing是我的主文件的名称。我已经检查了一遍又一遍,但似乎没有发现问题。有人看到问题了吗?感谢在您删除temp1和temp的两种情况下,它们都指向相同的地址。在您删除temp1和temp的两种情况下,它们都指向相同的地址相同的地址。您的代码肯定有不止一个问题 假设要删除的密钥存储在根目录中: #include <iostream> #i
temp1
和temp
的两种情况下,它们都指向相同的地址。在您删除temp1
和temp
的两种情况下,它们都指向相同的地址相同的地址。您的代码肯定有不止一个问题
假设要删除的密钥存储在根目录中:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
template <class T>
struct TreeNode{
string value;
T key;
TreeNode<T> *Parent;
TreeNode<T> *LeftChild;
TreeNode<T> *RightChild;
TreeNode (T k,string Val)
{
this->value=Val;
this->key=k;
this->Parent=NULL;
this->LeftChild=NULL;
this->RightChild=NULL;
}
};
template <class T>
class BinaryTree{
private:
TreeNode<T> *Root;
public:
BinaryTree();
void LoadTree(const char file[]);
~BinaryTree();
void insertNode(T Key,string Val);
void deleteNode(T Key);
string searchNode(T Key);
void UpdateKey(T newkey,T oldkey);
int Height(TreeNode<T> *node);
int height();
};
template <class T>
BinaryTree<T>::BinaryTree()
{
Root=NULL;
}
template <class T>
void BinaryTree<T>::insertNode(T Key,string Val)
{
TreeNode<T> **temp=&Root;
TreeNode<T> *temp1=NULL;
if (*temp==NULL)
{
Root=new TreeNode<T>(Key,Val);
return;
}
else
{
while (*temp!=NULL)
{
temp1=*temp;
if (temp1->key>Key)
{
temp=&(*temp)->LeftChild;
}
else if (temp1->key<Key)
{
temp=&(*temp)->RightChild;
}
}
}
*temp=new TreeNode<T>(Key,Val);
(*temp)->Parent=temp1;
}
template <class T>
void BinaryTree<T>::LoadTree(const char *file)
{
ifstream fin;
fin.open(file);
string buffer;
T buff;
while (!fin.eof())
{
getline(fin,buffer,'~');
fin>>buff;
if (!buff)
continue;
insertNode(buff,buffer);
}
fin.close();
}
void BinaryTree<T>::deleteNode(T Key)
{
TreeNode<T> *temp=Root;
TreeNode<T> *temp1=temp;
while (temp!=NULL)
{
if (temp->key==Key)
{
temp1=temp;
if (temp==Root)
{
TreeNode<T> *temp2=Root;
temp2=temp2->RightChild;
while (temp2->LeftChild!=NULL)
{
temp2=temp2->LeftChild;
}
temp->key=temp2->key;
temp->value=temp2->value;
delete temp2;
}
else if (temp->RightChild==NULL)
{
TreeNode<T> *temp2=temp;
temp2=temp->Parent;
if (temp2->RightChild==temp)
{
temp2->RightChild=temp->LeftChild;
(temp->LeftChild)->Parent=temp2;
}
else if (temp2->LeftChild==temp)
{
temp2->LeftChild=temp->LeftChild;
(temp->LeftChild)->Parent=temp2;
}
delete temp1;
delete temp;
return;
}
else if (temp->LeftChild==NULL)
{
TreeNode<T> *temp2=temp;
temp2=temp->Parent;
if (temp2->RightChild==temp)
{
temp2->RightChild=temp->RightChild;
(temp->RightChild)->Parent=temp2;
}
else if (temp2->LeftChild==temp)
{
temp2->LeftChild=temp->RightChild;
(temp->RightChild)->Parent=temp2;
}
delete temp1;
delete temp;
return;
}
else if (temp->RightChild!=NULL)
{
TreeNode<T> *tmp=temp;
temp=temp->RightChild;
if (temp->LeftChild!=NULL)
{
while (temp->LeftChild!=NULL)
{
tmp=temp;
temp=temp->LeftChild;
}
}
if (tmp!=temp1)
tmp->LeftChild==NULL;
else
temp1->RightChild==NULL;
temp1->key=temp->key;
temp1->value=temp->value;
delete temp;
return;
}
}
if (temp->key>Key)
{
temp=temp->LeftChild;
}
else if (temp->key<Key)
{
temp=temp->RightChild;
}
}
return;
}
你的代码肯定有不止一个问题 假设要删除的密钥存储在根目录中:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
template <class T>
struct TreeNode{
string value;
T key;
TreeNode<T> *Parent;
TreeNode<T> *LeftChild;
TreeNode<T> *RightChild;
TreeNode (T k,string Val)
{
this->value=Val;
this->key=k;
this->Parent=NULL;
this->LeftChild=NULL;
this->RightChild=NULL;
}
};
template <class T>
class BinaryTree{
private:
TreeNode<T> *Root;
public:
BinaryTree();
void LoadTree(const char file[]);
~BinaryTree();
void insertNode(T Key,string Val);
void deleteNode(T Key);
string searchNode(T Key);
void UpdateKey(T newkey,T oldkey);
int Height(TreeNode<T> *node);
int height();
};
template <class T>
BinaryTree<T>::BinaryTree()
{
Root=NULL;
}
template <class T>
void BinaryTree<T>::insertNode(T Key,string Val)
{
TreeNode<T> **temp=&Root;
TreeNode<T> *temp1=NULL;
if (*temp==NULL)
{
Root=new TreeNode<T>(Key,Val);
return;
}
else
{
while (*temp!=NULL)
{
temp1=*temp;
if (temp1->key>Key)
{
temp=&(*temp)->LeftChild;
}
else if (temp1->key<Key)
{
temp=&(*temp)->RightChild;
}
}
}
*temp=new TreeNode<T>(Key,Val);
(*temp)->Parent=temp1;
}
template <class T>
void BinaryTree<T>::LoadTree(const char *file)
{
ifstream fin;
fin.open(file);
string buffer;
T buff;
while (!fin.eof())
{
getline(fin,buffer,'~');
fin>>buff;
if (!buff)
continue;
insertNode(buff,buffer);
}
fin.close();
}
void BinaryTree<T>::deleteNode(T Key)
{
TreeNode<T> *temp=Root;
TreeNode<T> *temp1=temp;
while (temp!=NULL)
{
if (temp->key==Key)
{
temp1=temp;
if (temp==Root)
{
TreeNode<T> *temp2=Root;
temp2=temp2->RightChild;
while (temp2->LeftChild!=NULL)
{
temp2=temp2->LeftChild;
}
temp->key=temp2->key;
temp->value=temp2->value;
delete temp2;
}
else if (temp->RightChild==NULL)
{
TreeNode<T> *temp2=temp;
temp2=temp->Parent;
if (temp2->RightChild==temp)
{
temp2->RightChild=temp->LeftChild;
(temp->LeftChild)->Parent=temp2;
}
else if (temp2->LeftChild==temp)
{
temp2->LeftChild=temp->LeftChild;
(temp->LeftChild)->Parent=temp2;
}
delete temp1;
delete temp;
return;
}
else if (temp->LeftChild==NULL)
{
TreeNode<T> *temp2=temp;
temp2=temp->Parent;
if (temp2->RightChild==temp)
{
temp2->RightChild=temp->RightChild;
(temp->RightChild)->Parent=temp2;
}
else if (temp2->LeftChild==temp)
{
temp2->LeftChild=temp->RightChild;
(temp->RightChild)->Parent=temp2;
}
delete temp1;
delete temp;
return;
}
else if (temp->RightChild!=NULL)
{
TreeNode<T> *tmp=temp;
temp=temp->RightChild;
if (temp->LeftChild!=NULL)
{
while (temp->LeftChild!=NULL)
{
tmp=temp;
temp=temp->LeftChild;
}
}
if (tmp!=temp1)
tmp->LeftChild==NULL;
else
temp1->RightChild==NULL;
temp1->key=temp->key;
temp1->value=temp->value;
delete temp;
return;
}
}
if (temp->key>Key)
{
temp=temp->LeftChild;
}
else if (temp->key<Key)
{
temp=temp->RightChild;
}
}
return;
}
信息不足。是否声明BinaryTree的实例,然后填充它,然后执行搜索?以调试模式运行程序(Visual Studio中的F5),这将显示它崩溃的线路。是的,我将发布code@Peter我已经添加了BinaryTree的构造函数和插入节点并填充树的函数。1.为什么不做一个注释呢?2.给代码添加一些注释如何?我看不懂…3.先学习调试好ol的方法,通过打印凝灰岩?这需要更多的工作,而不是要求其他人为你做,但这是值得的…没有足够的信息。是否声明BinaryTree的实例,然后填充它,然后执行搜索?在调试模式下运行程序(Visual Studio中的F5),这将显示它崩溃的线路。是的,我将发布code@Peter我已经添加了BinaryTree的构造函数和插入节点并填充树的函数。1.为什么不做一个?2.添加这样的函数怎么样
if (temp->key==Key)
{
temp1=temp;
if (temp->RightChild==NULL)
{
// do something
}
else if (temp->LeftChild==NULL)
{
// do something
}
else if (temp->RightChild!=NULL)
{
// do something
}
else if (temp->LeftChild!=NULL && temp->RightChild==NULL)
{
// code never reached
}
}