给出分段错误的二叉搜索树实现 这是C++代码,实现程序获取SEG的二叉搜索树。故障: #include<iostream> #include<cstdlib> #include<stack> struct BSTNod
给出分段错误的二叉搜索树实现给出分段错误的二叉搜索树实现 这是C++代码,实现程序获取SEG的二叉搜索树。故障: #include<iostream> #include<cstdlib> #include<stack> struct BSTNod,c++,data-structures,binary-search-tree,recursive-datastructures,C++,Data Structures,Binary Search Tree,Recursive Datastructures,给出分段错误的二叉搜索树实现 这是C++代码,实现程序获取SEG的二叉搜索树。故障: #include<iostream> #include<cstdlib> #include<stack> struct BSTNode { int info; struct BSTNode *left, *right; }; class BST { private: //BSTNode *root; public:
这是C++代码,实现程序获取SEG的二叉搜索树。故障:
#include<iostream>
#include<cstdlib>
#include<stack>
struct BSTNode
{
int info;
struct BSTNode *left, *right;
};
class BST
{
private:
//BSTNode *root;
public:
void rpreorder(BSTNode*);
void rinorder(BSTNode*);
void rpostorder(BSTNode*);
BSTNode * search(BSTNode*, int);
BSTNode* insert(BSTNode*, int);
};
void BST:: rpreorder(BSTNode *root)
{
if(root == NULL)
return ;
std::cout<<" "<<root->info;
rpreorder(root->left);
rpreorder(root->right);
}
void BST:: rinorder(BSTNode *root)
{
if(root = NULL)
return ;
rinorder(root->left);
std::cout<<" "<<root->info;
rinorder(root->right);
}
void BST:: rpostorder(BSTNode *root)
{
if(root == NULL)
return ;
rpostorder(root->left);
rpostorder(root->right);
std::cout<<" "<<root->info;
}
BSTNode * BST:: search(BSTNode* root, int data)
{
if(root == NULL)
return root;
if(data < root->info)
return search(root->left, data);
else if(data > root->info)
return search(root->right, data);
return root;
}
BSTNode* BST:: insert(BSTNode *root, int data)
{
BSTNode* parent;
BSTNode* newNode = new BSTNode;
newNode->info = data;
newNode->right = NULL;
newNode->left = NULL;
if(root == NULL)
root = newNode;
else
{
parent = root;
while(parent != NULL)
{
if(data < parent->info)
{
if(parent->left == NULL)
parent->left = newNode;
parent = parent->left;
}
else if(data > parent->info)
{
if(parent->right == NULL)
parent->right = newNode;
parent = parent->right;
}
}
}
return root;
}
int main()
{
int choice, value;
BST obj;
BSTNode *root = NULL;
while(1)
{
std::cout<<"\n Enter the choice : \n 1. To insert \n 2. To search \n 3. recursive preorder \n 4. recursive inorder \n 5. recursive \
postorder \n 6. EXIT \n : ";
std::cin>>choice;
switch(choice)
{
case 1:
std::cout<<"\n Enter element to insert : ";
std::cin>>value;
root = obj.insert(root, value);
break;
case 2:
std::cout<<"\n Enter the element to search : ";
std::cin>>value;
{
BSTNode* temp;
temp = obj.search(root, value);
if(temp == NULL)
std::cout<<"\n Element not found "<<std::endl;
else
std::cout<<"\n The element found ";
}
break;
case 3:
obj.rpreorder(root);
break;
case 4:
obj.rinorder(root);
break;
case 5:
obj.rpostorder(root);
break;
case 6:
exit(0);
}
}
return 0;
}
#包括
#包括
#包括
结构节点
{
国际信息;
结构节点*左,*右;
};
BST级
{
私人:
//节点*根;
公众:
void rproorder(BSTNode*);
无效订单(BSTNode*);
void rpostorder(BSTNode*);
BSTNode*搜索(BSTNode*,int);
BSTNode*插入(BSTNode*,int);
};
void BST::rproorder(BSTNode*root)
{
if(root==NULL)
返回;
标准:coutleft);
std::coutright);
std::cout root->info)
返回搜索(根->右,数据);
返回根;
}
BSTNode*BST::插入(BSTNode*root,int数据)
{
BSTNode*父节点;
BSTNode*newNode=新BSTNode;
新建节点->信息=数据;
newNode->right=NULL;
newNode->left=NULL;
if(root==NULL)
根=新节点;
其他的
{
父=根;
while(父级!=NULL)
{
如果(数据<父项->信息)
{
如果(父项->左==NULL)
父节点->左=新节点;
父项=父项->左;
}
else if(数据>父级->信息)
{
如果(父->右==NULL)
父->右=新节点;
父=父->右;
}
}
}
返回根;
}
int main()
{
智力选择、价值;
BST-obj;
BSTNode*root=NULL;
而(1)
{
std::coutchoice;
开关(选择)
{
案例1:
std::coutvalue;
根=对象插入(根,值);
打破
案例2:
std::coutvalue;
{
BSTNode*温度;
temp=对象搜索(根,值);
if(temp==NULL)
std::coutUseif(root==NULL)
而不是if(root=NULL)
中的if(root=NULL)
。我不确定这是否是唯一的问题。我不会发这篇文章,因为这是一个简单的打字错误。哦..谢谢…你能谈谈问题的第二部分吗?@Singham,你说的是什么意思“我无法在BST中插入更多元素"?为什么不能插入?发生了什么?程序崩溃了吗?@Singham您的插入中有一个无限循环。您在上找到一个空指针,例如,parent->left
,因此您分配了新节点,但不要中断
。当您点击parent=parent->left
时,您现在有了指向新节点的parent
插入t,由于其值必须等于输入测试值,因此两个测试(小于或大于)都不是真的。因此父级不移动,循环无限重复。调试:这是晚餐(tm)@WhozCraig:Thank:)你的解决方案真是太棒了。但是我没有收到你最后一句话调试:这是晚餐的东西->“(tm)“使用if(root==NULL)
而不是if(root=NULL)
在BST::rinorder()中
。我不确定这是否是唯一的问题。我不会发布这篇文章,因为这是一个简单的打字错误。哦……谢谢……你能谈谈问题的第二部分吗?@Singham,你说“我不能在BST中插入更多元素”是什么意思?为什么不能插入?发生了什么?程序崩溃了吗?@Singham您的插入中有一个无限循环。您在上找到一个空指针,例如,parent->left
,因此您分配了新节点,但不要中断
。当您点击parent=parent->left
时,您现在有了指向新节点的parent
插入t,由于其值必须等于输入测试值,因此两个测试(小于或大于)都不是真的。因此父级不移动,循环无限重复。调试:这是晚餐(tm)@WhozCraig:谢谢:)你的解决方案真是太棒了。但我没有收到你最后一句话调试:这是晚饭吃的东西->(tm)
void BST:: rinorder(BSTNode *root)
{
if(root = NULL)
return ;
rinorder(root->left); // <- this line gives seg. fault
std::cout<<" "<<root->info;
rinorder(root->right);
}