no_russion 2019-03-12 18:41 采纳率: 33.3%
浏览 283
已采纳

还是被二叉搜索树难住了,结果出不来,求教各位大佬到底哪错了

这次是用插入法创建二叉搜索树

#include<stdio.h>
#include<stdlib.h>

int flag;

typedef int elemtype;

typedef struct B
{
    elemtype data;
    struct B *lchild,*rchild;
}BiTNode,*BiTree;

void Init_BST(BiTree *BST)
{
    *BST=NULL;
}


int Search_BST(BiTree *BST,BiTree *p,BiTree *q,elemtype key)
{
    //int flag=0;
    if(*BST==NULL)
        {
        return 0;
        }


    else if((*BST)->data ==key)
    {
        *q=*BST;
        return 1;
    }

    else if(key>(*BST)->data)
        {
        *p=*BST;
        Search_BST(&(*BST)->rchild,p,q,key);
        }

    else if(key<(*BST)->data)
        {
        *p=*BST;
        Search_BST(&(*BST)->lchild,p,q,key);
        }
}


int Insert_BST(BiTree *BST,elemtype key)
{
    //int flag=0;
    BiTree p,q,s;
    p=*BST;
    if(Search_BST(BST,&p,&q,key))
        {
        printf("¸Ãkey=%dÒÑ´æÔÚ\n",key); 
        flag=0;
        }

    else 
    {
        s=(BiTree)malloc(sizeof(BiTNode));
        s->data=key;
        s->lchild=s->rchild=NULL;
        if(p==NULL)
        {   
        *BST=s;
        }
        else if(key<p->data)
        {
            p->lchild=s;
        }
        else
        {
            p->rchild=s;
        }
        flag=1;
    }
    return flag;
}

int Create_BST(BiTree *BST,elemtype kx[],int n)
{
    for(int i;i<n;i++)
     {
     Insert_BST(BST,kx[i]);
     }
     return 1;
}

int main()
{
    BiTree BST;
    Init_BST(&BST);
    BiTree root=BST;
    int kx[5]={1,2,3,4,5};
    Create_BST(&BST,kx,5);
    printf("%d\n",root->data);
    return 0;
}
  • 写回答

1条回答

  • no_russion 2019-03-12 22:17
    关注

    检查一遍发现错的太多了,知道解决方案了

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播