C++ 二进制搜索树的插入函数出错
我试着用代码块编译这个程序,但它给出了一条错误消息 无法将void转换为treenode 在insert函数中,虽然我在参数部分本身指定了声明的t。我试着删除那一行,但程序没有给出任何输出。有什么建议吗C++ 二进制搜索树的插入函数出错,c++,C++,我试着用代码块编译这个程序,但它给出了一条错误消息 无法将void转换为treenode 在insert函数中,虽然我在参数部分本身指定了声明的t。我试着删除那一行,但程序没有给出任何输出。有什么建议吗 #include<iostream> #include<malloc.h> using namespace std; struct treenode; typedef struct treenode *position; typedef struct treenode *
#include<iostream>
#include<malloc.h>
using namespace std;
struct treenode;
typedef struct treenode *position;
typedef struct treenode *searchtree;
typedef int elementtype;
position find(elementtype x,searchtree t);
position findmin(searchtree t);
position findmax(searchtree t);
searchtree insert(elementtype x,searchtree t);
searchtree delete1(elementtype x,searchtree t);
struct treenode
{
elementtype element;
searchtree left;
searchtree right;
};
position find(elementtype x,searchtree t)
{
if(t==NULL)
return NULL;
else if(x<t->element)
return find(x,t->left);
else if(x>t->element)
return find(x,t->right);
else
return t;
}
position findmin(searchtree t)
{
if(t==NULL)
return NULL;
else if(t->left==NULL)
return t;
else
return findmin(t->left);
}
position findmax(searchtree t)
{
if(t!=NULL)
while(t->right!=NULL)
t=t->right;
return t;
}
searchtree insert(elementtype x,searchtree t)
{
if(t==NULL)
{
t=malloc(sizeof(struct treenode)); //gives me error on compiling
if(t==NULL)
{
cout<<"\n Out of space";
exit(0);
}
t->element=x;
t->right=t->left=NULL;
}
else if(x<t->element)
t->left=insert(x,t->left);
else if(x>t->element)
t->right=insert(x,t->right);
return t;
}
void display(searchtree t)
{
if(t==NULL)
return;
display(t->left);
cout<<t->element;
display(t->right);
}
searchtree deletion(elementtype x,searchtree t)
{
position tmpcell;
if(t==NULL)
{
cout<<"\nElement not found";
return NULL;
}
else if(x<t->element)
t->left=deletion(x,t->left);
else if(x>t->element)
t->right=deletion(x,t->right);
else if(t->left && t->right)
{
tmpcell=findmin(t->right);
t->element=tmpcell->element;
t->right=deletion(t->element,t->right);
}
else
{
tmpcell=t;
if(t->left==NULL)
t=t->right;
else if(t->right==NULL)
t=t->left;
free(tmpcell);
}
return t;
}
int main()
{
int ch,x;
position p;
searchtree t=NULL,min,max;
while(1)
{
cout<<"\n1. Insert\n2. Delete\n3.Find min\n4. Find max\n5. Display\n6. Exit";
cout<<"\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the element:";
cin>>x;
t=insert(x,t);
break;
case 2:
cout<<"\nEnter the element to be deleted:";
cin>>x;
t=deletion(x,t);
break;
case 3:
min=findmin(t);
if(min)
cout<<"\nMinimum element is "<<min->element;
else
cout<<"\nEmpty tree";
break;
case 4:
max=findmax(t);
if(max)
cout<<"\nMaximum element is "<<max->element;
else
cout<<"\nEmpty tree";
break;
case 5:
display(t);
break;
case 6:
exit(0);
default :
cout<<"\nWrong choice";
}
}
return 0;
}
在C中,指针void*自动安全地升级为任何其他指针类型。因此,例如,不需要将malloc输出强制转换为指针
但是,在C++中,需要输出Maloc的输出,例如
既然使用C++,必须显式地将空穴*指针指向所需的指针类型:
// valid in C, but not valid in C++
struct treenode *t = malloc(sizeof(struct treenode));
// C++
t = (searchtree)(malloc(sizeof(struct treenode)));
// or
t = (struct treenode*)(malloc(sizeof(struct treenode)));
// or
t = static_cast<searchtree>(malloc(sizeof(struct treenode)));
注:存在未使用的可变位置p;如果你真的想要C++,所有有SkestTrutt参数的函数都会找到,FixMax,…应该是treenode的类成员。如果没有T-T>,代码将更好。在C++中,只使用Neal/Debug,因为构造函数/析构函数代码否则不会运行……@ Soren,我同意。我开始解释malloc错误,后来我意识到这都是关于一个结构实例的。谢谢大家。该程序现在可以使用新的treenode命令,不过只需对输出进行少量的预打印。除了新函数,我还可以使用malloc和显式指针转换。我的理解正确吗?
t = new treenode; // instead of malloc
...
delete tmpcell; // instead of free(tmpcell);