二叉排序树上的查找

#include <stdio.h>
#include <stdlib.h>
#define null 0
#define n 100
typedef int keytype;
typedef struct node{
  keytype key;//二叉排序树结点的关键字域
  struct node *lchild,*rchild;
 }bstnode;//二叉排序树结点结构
typedef bstnode *bstree;//二叉排序树类型定义

bstnode *searching(bstree tree,keytype key)
 {//在tree二叉排序树中查找关键字值为key的结点
  if((tree==null)||(key==tree->key))//若二叉排序树tree为空或根结点的关键字与key相同
   return tree;//返回根结点指针
  if (key<tree->key)//若key大于根结点的关键字的值
    return searching(tree->lchild,key);//在根的左子树中查找
  else//若key小于根结点的关键字的值
    return searching(tree->rchild,key);//在根的右子树中查找
 }//end of searching 

main()//主函数
 {
  bstree t=null,q=null;
  keytype k;
  createbst(&t);//创建一棵二叉排序树t
  printf("/n bst data:");
  inorder(t);//输出二叉排序树t的中序遍历序列
  printf("/n");
  printf("input search key:/n");
  scanf("%d",&k);//输入待查找的关键字k
  q=searching(t,k);//在二叉排序树t中查找值为k的结点,
          //返回找到结点的指针,若查找失败返回null
  if (q==null)//若返回null,输出查找失败信息
    printf("search %d default!/n",k);
  else//若q不为空,则输出查找成功的信息
    printf("search %d success!/n",q->key);
 }//end of main 


insertbst(bstree *tptr,keytype key)
 {//在二叉排序树tptr中,插入值为key的结点
  bstnode *f,*p=*tptr;
  while(p){//若*p非空
    if (p->key==key) //若*p结点的数据域等于key
     return;//返回
    f=p;//f指针指向*p,目的是让f指针指向待比较结点的双亲结点
    p=(key<p->key)?p->lchild:p->rchild;//若key>p->key,
             //p指向*p的左孩子,否则指向右孩子
      }//end of if
  p=(bstnode *)malloc(sizeof(bstnode));//找到插入位置后,申请结点空间*p
  p->key=key;p->lchild=p->rchild=null;//将key放入*p的数据域中,并将其左右孩子置空
  if(*tptr==null)//若二叉排序树为空
    *tptr=p;//直接将结点*p作为根结点
  else//若待插入的二叉排序树非空
  if (key<f->key)//若key小于f->key
    f->lchild=p;//将*p结点作为*f结点的左孩子插入
  else f->rchild=p;//将*p结点作为*f结点的右孩子插入
 }//end of insertbst

createbst(bstree *t)
 {//创建一棵二叉排序树t
  keytype key;
  int j,i,m;
  printf("1--random data/n");
  printf("2--inscree data/n");
  printf("3--descree data/n");
  printf("4--input data/n");
  scanf("%d",&j);//选择一种数据输入方式

    if (j==1) {//随机输入100个以内的数据生成二叉排序树
    m=random(100);
    printf("random source data:");
    for(i=1;i<=m;i++){
      key=random(100);printf("%d ",key);
      insertbst(t,key);//将关键字key插入到二叉排序树t中
     }
    printf("/n");
   }

  if (j==2){//生成一个递增序列产生的二叉排序树
    m=random(100);
    printf("inscree source data:");
    for(i=1;i<=m;i++){
      insertbst(t,i);
      printf("%d ",i);
     }
    printf("/n");
   }

  if (j==3){//生成一个递减序列产生的二叉排序树
    m=random(100);
    printf("inscree source data:");
    for(i=1;i<=m;i++){
      insertbst(t,m-i+1);
      printf("%d ",m-i+1);
     }
    printf("/n");
   }

  if (j==4){//生成一棵根据键盘输入数据序列产生的二叉排序树
    printf("input data end of 0:/n");
    scanf("%d",&key);
    while(key){
      insertbst(t,key);
      scanf("%d",&key);
     }//end of while
   }//end of if
 }//end of createbst 


inorder(bstree t)
 {//对二叉排序树t进行中序遍历
  if (t){
    inorder(t->lchild);//中序遍历左子树
    printf(" %d",t->key);//访问根结点
    inorder(t->rchild);//中序遍历右子树
   }//end of if 
 }//end of inorder


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值