in this code node structure and insertion and inorder traversal code has
been written and whenever i compile this code it doesn't give me any error
or exception but when i run this code this only takes one input and then
show me that there is an error thats why it can not be continued further.
#include <iostream>
using namespace std;
struct node //structure for node
{
int data;
node *left;
node *right;
};
in this code node structure and insertion and inorder traversal code has
been written and whenever i compile this code it doesn't give me any error
or exception but when i run this code this only takes one input and then
show me that there is an error thats why it can not be continued further.
class bst
{
public:
node *root;
node *temp;
int key;
bst()
{
root = temp = NULL;
key = 0;
}
bst insertion(node*);
Kindly oversee this function and tell me
what is the error in this function and the function of inorder
bst inorder(node*);
};
bst bst::insertion(node *temp) this function is for insertion
{
if(root==NULL) check if the root is null
{
cout<<"Enter Root Ki info:"<<endl;
cin>>root->data;
root->left = root->right = NULL;
you must say that i should return over here but the return function
can not be available here and i am unable to correct this one.
}
cout<<"Enter Data:"<<endl;
cin>>key;
if(key>root->data) check if key is greater than the root's data
{
temp->right = new node; it makes new node from right
temp->right->data= key;
temp->right->left = NULL;
temp->right->right = NULL;
if i return temp over here it says me that this can not be done because of the node * pointer can not be converted into bst pointer and etc.
}
else if(key<root->data) check if key is below than the root'data
{
temp->left = new node;
temp->left->data = key;
temp->left->left = NULL;
temp->left->right = NULL;
}
}
bst bst :: inorder(node * temp)此函数用于显示inorder方案中的节点详细信息
{
if(root==NULL)
{
cout<<"Nothing to be found"<<endl;
}
this is for inorder in which the the root element will be shown after the left nodes and at the end right nodes of the root will be shown and similarly post and preorder traversal can be done but this is not showing me any thing can you describe what will be the exact reason and i have tried to oversee all the elements by varying the pointer of struct in the main call but it doesn't make any difference
inorder(temp->left);
cout<<temp->data<<'\t';
inorder(temp->right);
这是为了按顺序显示根元素将在左节点之后显示,并且在根节点的右端节点将显示,并且类似地发布和预订遍历可以完成但是这并没有向我显示任何事情你能描述什么将是确切的原因,我试图通过改变主调用中struct的指针来监督所有元素,但它没有任何区别}
这是为了按顺序显示根元素将在左节点之后显示,并且在根节点的右端节点将显示,并且类似地发布和预订遍历可以完成但是这并没有向我显示任何事情你能描述什么将是确切的原因,我试图通过改变主调用中的struct的指针来监督所有元素,但它没有任何区别int main()
{
bst b;
for(int i=0;i<5;i++)
{
b.insertion(b.root); error occurs when this is running
}
b.inorder(b.root); it doesn't reach till this point
system("Pause");
it is demanding more and more detail all is this what should i describe moe kindly tell me that also [enter link description here][1]
}
2 回答
抱歉,这个代码并不是很正确 .
设计错了 . 您应该在main函数中询问密钥,然后将密钥作为参数传递给
insertion
方法 . 将临时节点作为参数传递没有意义 . 如果insertion
需要一个临时节点,那么它应该为自己创建一个 .另外
insertion
不需要返回BST,它只需要修改this
指针指向的BST . 所以insertion
看起来应该是这样的主要应该看起来像这样
方法
bst::inorder
有类似的错误 .另外,bst不需要键和临时节点的成员变量 . 所以删除
来自
class bst
. 如果在方法中需要这些变量,则在不在类中的方法中声明它们 . 该类唯一需要的是指向根的指针 .insertion
中的算法是错误的 . 将节点添加到BST需要某种循环 . 你必须遍历树,直到你到达一个空闲插槽,然后才能插入节点 . 你的insertion
代码没有循环,所以它不对 .最后你的代码崩溃的原因就在这里
如果root为NULL,那么
root->data
是一个错误,可能会导致程序崩溃 .这段代码有太多问题 . 在您尝试编写代码之前,我认为您需要更好地了解如何使用 design 类 . 抱歉 .
//这是最终的代码,它是正确执行的整个代码的答案,并且总是给出正确答案,因为它应该是#include using namespace std; struct node {node * left;节点权利; int数据; }; class bst {public:node * root;节点 temp; bst(){temp = root = NULL;