二叉搜索树的插入函数出错

时间:2015-09-26 16:22:07

标签: c++

我尝试在代码块中编译此程序,但它会显示错误消息

  

无法将void转换为treenode

(在插入函数中),虽然我在参数部分本身中指定了声明的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;
}

1 个答案:

答案 0 :(得分:0)

在C指针中,void *被自动安全地提升为任何其他指针类型。因此,不需要将malloc输出转换为指针,例如Do I cast the result of malloc?

但是,在C ++中,需要输出malloc()的输出 Why does C++ require a cast for malloc() but C doesn't?

由于您使用的是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);