[数据结构]第七次作业:二叉排序树

/* ==============  Program Description  ============= */
/*               Freshare's 7th of dswork             */
/* ================================================== */

#include "stdlib.h"
#include "stdio.h"
#define MAX 255
#define TRUE 1
#define FALSE 0
#define ElemType int

typedef struct BitNode
{
 ElemType data;
 struct BitNode *lchild,*rchild;
}BitNode,*BiTree;

BiTree find (BiTree root, ElemType key)
{
 BiTree p,f;
 p=root;
 while (p!=NULL)
  if (key<p->data) {f=p;p=p->lchild;}
  else {f=p;p=p->rchild;}
 return (f);
}

BiTree SearchBST(BiTree root ,int k)
{
 BiTree p;
 p=root;
 while(p!=NULL)
 if (p->data==k)  return(p);
  else if (k<p->data)  p=p->lchild;
  else p=p->rchild;
 return (NULL);
}

BiTree creadBiTree()
{
 BiTree root,f,p;
 int data;
 scanf("%d",&data);
 root=(BiTree)malloc(sizeof(BitNode));
 root->data=data;
 root->lchild=root->rchild=NULL;
 scanf("%d",&data);
 while(data!=9999)
 {
  f=find(root,data);
  p=(BitNode*)malloc(sizeof(BitNode));
  p->data=data;
  p->lchild=p->rchild=NULL;
  if(data<f->data)  f->lchild=p;
  else f->rchild=p;
  scanf("%d",&data);
 }
 return(root);
}

void main()
{
 BiTree root,p;
 int test;
 printf("输入数字,以9999结束:/n");
 root=creadBiTree();
 printf("请输入需要查找数据:/n");
    scanf("%d",&test);
 p=SearchBST(root,test);
 if (p!=NULL)  printf("%d被找到啦!/n",p->data);
 else printf("没有到啦!/n");
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值