[英]Error in insert function for binary search tree
我尝试在代码块中编译该程序,但给出错误消息
无法将void转换为treenode
(在insert函数中),尽管我在参数部分本身中指定了声明的t。 我尝试删除该行,但该程序未提供任何输出。 有什么建议么?
#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
输出转换为指针,例如,我是否转换malloc的结果?
但是,在C ++中,需要强制转换 malloc()
输出,例如, 为什么C ++要求对malloc()强制转换,但C不需要?
由于您使用的是C ++,因此必须将void*
指针显式转换为所需的指针类型:
// 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)));
当然,对于C ++,考虑使用本机C ++功能是有意义的。 您正在为一个结构实例分配内存,因此可以使用new
:
t = new treenode; // instead of malloc
...
delete tmpcell; // instead of free(tmpcell);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.