回 帖 发 新 帖 刷新版面

主题:二叉排序树的建立---请高手帮忙看一下,改一改,谢先!

我的程序是在VC下运行的,运行时,显示
error C2664: 'InsertBsTree' : 
          cannot convert parameter 1 from 'struct node' to 'struct node *' 

请帮忙修改一下:错在那?

#include "stdafx.h"
#include "stdio.h"
#include "malloc.h"

typedef int KeyType;
typedef struct node
{
    KeyType key;
    struct node *lchild,*rchild;
}BsTree;

BsTree *InsertBsTree(BsTree *BST,KeyType k)
{//在树BST中,插入关键字为k的节点
    BsTree *f,*p;f=BST;//f初值指向根节点
    p=(BsTree *)malloc(sizeof(BsTree));//创建一新记录
    p->key=k;
    p->lchild=p->rchild=NULL;
    if(BST==NULL) BST=p;//原链表为空时,新插入的记录为新的根节点
    else if(k<f->key) InsertBsTree(f->lchild,k);//原树非空时,将*p作为*f 的左孩子插入 
          else  InsertBsTree(f->rchild,k);

    return BST;
}

BsTree *CreatBsTree(KeyType A[],int n)
{//创建二叉平衡树
    BsTree *root=NULL;
    int i;
    for(i=0;i<n;i++)
    root=InsertBsTree(*root,A[i]);/*   <----这显示的:error C2664: 'InsertBsTree' : 
          cannot convert parameter 1 from 'struct node' to 'struct node*'    */
    return root;
}

void preorder(BsTree *BST)
{
    if(BST)
    {
        preorder(BST->lchild);
        printf("%d->",BST->key );
        preorder(BST->rchild);
    }
}

int main(int argc, char* argv[])
{
    BsTree *root;
    int i,a[30];
    printf("请输入10个字符的一串整数:\n");
    for(i=0;i<10;i++)
        scanf("%d",&a[i]);
    root=CreatBsTree(a,10);
    printf("构造的二叉排序树的前序序列为:");
    preorder(root);

    return 0;
}

回复列表 (共1个回复)

沙发

root=InsertBsTree(*root,A[i])
传参有错
你的root本身已经是指针类型,调用的时候应该直接传
root=InsertBsTree(root,A[i])

我来回复

您尚未登录,请登录后再回复。点此登录或注册