论坛: 编程破解 标题: 二叉排序树 复制本贴地址    
作者: morrel [morrel]    论坛用户   登录
#include "stdio.h"
typedef int keytype;
typedef struct node{
keytype key;
struct node *lchild,*rchild;
}bitnode,*bstree;

insbst(bstree *t,keytype key)
{bstree p,f;
p=(*t);
while(p){
  if (p->key==key) return;
  f=p;
  p=(key<p->key)? p->lchild:p->rchild;
  }
p=(bitnode *)malloc(sizeof(bitnode));
p->key=key;p->lchild=p->rchild=NULL;
if(!(*t)) (*t)=p;
else if(key<f->key) f->lchild=p;
      else f->rchild=p;
}


create(void)
{bstree t;
int key;
t=NULL;
printf("\nintput some keys(end:0):\n");
scanf("%d",&key);
while(key){
insbst(&t,key);
scanf("%d",&key);
}
return t;
}

inordertraverse(bstree t){
  if (t){ inordertraverse(t->lchild);
        printf("%5d",t->key);
        inordertraverse(t->rchild);}
}


void searchbst(bstree t,keytype key)
{ bstree p=t;
  while (p){
  if(p->key==key)
  {printf("\nfind success!\n"); return ;}
  p=(key<p->key)? p->lchild:p->rchild;
  }

printf("\nfind unsuccess!\n");
}


main()
{bstree t;
keytype key;
t=create();
printf("\ninordertraverse the bstree is:");
inordertraverse(t);
printf("\nintput want to find key:\n");
scanf("%d",&key);
searchbst(t,key);
} 

地主 发表时间: 04-05-28 17:56

论坛: 编程破解

20CN网络安全小组版权所有
Copyright © 2000-2010 20CN Security Group. All Rights Reserved.
论坛程序编写:NetDemon

粤ICP备05087286号