**
二叉检索树
**
源代码的调试
#define MAXSIZE 100
#define NULL 0
#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
typedef int elemtype;
typedef struct node {
keytype key; /*关键字域*/
elemtype other;/*其他数据域*/
struct node *lchild,*rchild; /*左、右孩子指针*/
} bilist; /*二叉检索树的结点结构*/
void insert(bilist **r,bilist *s) { /* 将*s结点插入到一棵二叉检索树*r中*/
bilist *q,*p;
if((*r)==NULL) (*r)=s;
else if(s->key<(*r)->key) insert(&((*r)->lchild),s);
else insert(&((*r)->rchild),s);
}
bilist *creat(keytype r[],int n) { /*二叉检索树的构造函数算法*/
int i;
bilist *s,*t;
t=NULL;
for(i=0; i<n; i++) {
s=(bilist *)malloc(sizeof(bilist));
s->key=r[i];
s->other=NULL;
s->rchild=NULL;
s->lchild=NULL;
insert(&t,s);
}
return t;
}
bilist *search(bilist *t,keytype k) { /*二叉检索树检索算法*/
if((t==NULL)||(t->key==k)) return t;
else if(t->key<k)
return (search(t->rchild,k));
else
return(search(t->lchild,k));
}
main() {
keytype A[MAXSIZE];
int i,data,n=10;
bilist *root;
for(i=0; i<n; i++)
scanf("%d",&A[i]);
printf("\n");
root=creat(A,n);
printf("please input the search key:");
scanf("%d",&data);
if(search(root,data)!=NULL) printf("search succeed! \n");
else printf("search failed!\n ");
}
(1) 把二叉排序树的插入函数的递归算法改写为非递归算法,并在主函数中调用它,调试好程序并分析其运行结果。
#define MAXSIZE 100
#define NULL 0
#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
typedef int elemtype;
typedef struct node {
keytype key; /*关键字域*/
elemtype other;/*其他数据域*/
struct node *lchild,*rchild; /*左、右孩子指针*/
} bilist; /*二叉检索树的结点结构*/
bilist *INSERTBST(bilist *t,bilist *s){
bilist *f,*p;
p = t;
while(p!=NULL){
f = p;
if(s->key == p->key) return t;
if(s->key < p->key) p = p->lchild;
else p = p->rchild;
}
if(t==NULL) return s;
if(s->key < f->key) f->lchild = s;
else f->rchild = s;
return t;
}
bilist *creat(keytype r[],int n) { /*二叉检索树的构造函数算法*/
int i;
bilist *s,*t;
t=NULL;
for(i=0; i<n; i++) {
s=(bilist *)malloc(sizeof(bilist));
s->key=r[i];
s->other=NULL;
s->rchild=NULL;
s->lchild=NULL;
t = INSERTBST(t,s);
}
return t;
}
bilist *search(bilist *t,keytype k) { /*二叉检索树检索算法*/
if((t==NULL)||(t->key==k)) return t;
else if(t->key<k)
return (search(t->rchild,k));
else
return(search(t->lchild,k));
}
main() {
keytype A[MAXSIZE];
int i,data,n=10;
bilist *root;
for(i=0; i<n; i++)
scanf("%d",&A[i]);
printf("\n");
root=creat(A,n);
printf("please input the search key:");
scanf("%d",&data);
if(search(root,data)!=NULL) printf("search succeed! \n");
else printf("search failed!\n ");
}
(2) 把二叉排序树的插入函数的递归算法改写为非递归算法,并在主函数中调用它,调试好程序并分析其运行结果。
#define MAXSIZE 100
#define NULL 0
#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
typedef int elemtype;
typedef struct node {
keytype key; /*关键字域*/
elemtype other;/*其他数据域*/
struct node *lchild,*rchild; /*左、右孩子指针*/
} bilist; /*二叉检索树的结点结构*/
bilist *INSERTBST(bilist *t,bilist *s) {
bilist *f,*p;
p = t;
while(p!=NULL) {
f = p;
if(s->key == p->key) return t;
if(s->key < p->key) p = p->lchild;
else p = p->rchild;
}
if(t==NULL) return s;
if(s->key < f->key) f->lchild = s;
else f->rchild = s;
return t;
}
bilist *CREATBST(keytype B[],int n) { //建立二叉排序树
int i;
bilist *t,*s;
t=NULL;
for(i=0; i<n; i++) {
s=(bilist *)malloc(sizeof(bilist));
s->lchild=NULL;
s->rchild=NULL;
s->key=B[i];
t=INSERTBST(t,s);
}
return t;
}
bilist *search(bilist *t,keytype k) { /*二叉检索树检索算法*/
if((t==NULL)||(t->key==k)) return t;
else if(t->key<k)
return (search(t->rchild,k));
else
return(search(t->lchild,k));
}
main() {
keytype A[MAXSIZE];
int i,data,n=10;
bilist *root;
for(i=0; i<n; i++)
scanf("%d",&A[i]);
printf("\n");
root=CREATBST(A,n);
printf("please input the search key:");
scanf("%d",&data);
if(search(root,data)!=NULL) printf("search succeed! \n");
else printf("search failed!\n ");
}
(3) 把二叉排序树的检索函数的递归算法改写为非递归算法,并在主函数中调用它,调试好程序并分析其运行结果。
#define MAXSIZE 100
#define NULL 0
#include<stdio.h>
#include<stdlib.h>
typedef int keytype;
typedef int elemtype;
typedef struct node {
keytype key; /*关键字域*/
elemtype other;/*其他数据域*/
struct node *lchild,*rchild; /*左、右孩子指针*/
} bilist; /*二叉检索树的结点结构*/
bilist *INSERTBST(bilist *t,bilist *s) {
bilist *f,*p;
p = t;
while(p!=NULL) {
f = p;
if(s->key == p->key) return t;
if(s->key < p->key) p = p->lchild;
else p = p->rchild;
}
if(t==NULL) return s;
if(s->key < f->key) f->lchild = s;
else f->rchild = s;
return t;
}
bilist *CREATBST(keytype B[],int n) { //建立二叉排序树
int i;
bilist *t,*s;
t=NULL;
for(i=0; i<n; i++) {
s=(bilist *)malloc(sizeof(bilist));
s->lchild=NULL;
s->rchild=NULL;
s->key=B[i];
t=INSERTBST(t,s);
}
return t;
}
bilist *search(bilist *t,keytype k) { /*二叉检索树检索算法*/
while(t != NULL) {
if(t->key == k)
return t;
if(t->key > k)
t = t->lchild;
else t = t->rchild;
}
return NULL;
}
main() {
keytype A[MAXSIZE];
int i,data,n=10;
bilist *root;
for(i=0; i<n; i++)
scanf("%d",&A[i]);
printf("\n");
root=CREATBST(A,n);
printf("please input the search key:");
scanf("%d",&data);
if(search(root,data)!=NULL) printf("search succeed! \n");
else printf("search failed!\n ");
}