主题:能人帮忙啊
typedef int keytype;
#define MAX 6
typedef struct
{keytype key;
/* datatype others;*/
}elemtype;
typedef struct
{elemtype *elem;
int length;
}stable;
int seqsearch(stable ST,keytype key)
{int i;
for( (1) ;i>0;i--)
if( (2) )
return i;
return 0;
}
int binsearch(stable ST,keytype key)
{int low=1,high=ST.length,mid;
while( (3) )
{mid= (4) ;
if(ST.elem[mid].key==key) return mid;
if(key<ST.elem[mid].key) high=mid-1;
else low=mid+1;
}
return 0;
}
void create_seq(stable *ST)
{int i=1;
ST->length=MAX;
for(i=1;i<=ST->length;i++)
{printf("Input the %d element:",i);
scanf("%d", (5) );
}
}
main()
{stable ST; keytype k;
create_seq(&ST);
printf("The searched element is:");
scanf("%d",&k);
printf("The location is %d!\n", (6) );
/*调用seqsearch函数查找无序顺序表中是否有k存在,并输出其位置*/
create_seq(&ST);
printf("The searched element is:");
scanf("%d",&k);
printf("The location is %d!\n", (7) );
/*调用binsearch函数查找有序顺序表中是否有k存在,并输出其位置*/
}
2.编写程序,实现二叉排序树的建立。
#include "stdio.h"
typedef int keytype;
typedef struct node
{keytype data;
struct node *left,*right;
}bitnode,*bitree;
void searchbst(bitree T,bitree *F,bitree *C,keytype key)
{while(T!=NULL)
if( (1) ) /*找到值为key的结点*/
{*C=T;
break;
}
else if(key<T->data) /*如果要查找的元素比根结点值小,则在其左子树中继续*/
{*F=T;
(2) ;
}
else /*如果要查找的元素比根结点值大,则在其右子树中继续*/
{*F=T;
(3) ;
}
}
int insertbst(bitree *T,keytype key)
{bitree F=NULL,C=NULL,s;
searchbst(*T,&F,&C,key); /*T为二级指针,存储根结点地址的指针*/
if( (4) ) /*如果在当前二叉排序树中存在key值时,不做插入操作*/
return 0;
s=(bitree)malloc(sizeof(bitnode));
s->data=key;
s->left=s->right=NULL;
if( (5) ) /*插入结点为根结点*/
*T=s;
else if(key<F->data)
F->left=s; /*插入新结点*/
else
F->right=s;
return 1;
}
void creatbst(bitree *T) /*根结点的初值为空*/
{keytype key;
*T=NULL;
scanf("%d",&key);
while(key!=-1) /*设-1为输入数据结束标志*/
{insertbst(T,key);
scanf("%d",&key);
}
}
void InOrderTraverse(bitree T)
{if(T!=NULL)
{InOrderTraverse(T->left);
printf("%5d",T->data);
InOrderTraverse(T->right);
}
}
main()
{bitree T;
creatbst(&T);
InOrderTraverse(T);
}
#define MAX 6
typedef struct
{keytype key;
/* datatype others;*/
}elemtype;
typedef struct
{elemtype *elem;
int length;
}stable;
int seqsearch(stable ST,keytype key)
{int i;
for( (1) ;i>0;i--)
if( (2) )
return i;
return 0;
}
int binsearch(stable ST,keytype key)
{int low=1,high=ST.length,mid;
while( (3) )
{mid= (4) ;
if(ST.elem[mid].key==key) return mid;
if(key<ST.elem[mid].key) high=mid-1;
else low=mid+1;
}
return 0;
}
void create_seq(stable *ST)
{int i=1;
ST->length=MAX;
for(i=1;i<=ST->length;i++)
{printf("Input the %d element:",i);
scanf("%d", (5) );
}
}
main()
{stable ST; keytype k;
create_seq(&ST);
printf("The searched element is:");
scanf("%d",&k);
printf("The location is %d!\n", (6) );
/*调用seqsearch函数查找无序顺序表中是否有k存在,并输出其位置*/
create_seq(&ST);
printf("The searched element is:");
scanf("%d",&k);
printf("The location is %d!\n", (7) );
/*调用binsearch函数查找有序顺序表中是否有k存在,并输出其位置*/
}
2.编写程序,实现二叉排序树的建立。
#include "stdio.h"
typedef int keytype;
typedef struct node
{keytype data;
struct node *left,*right;
}bitnode,*bitree;
void searchbst(bitree T,bitree *F,bitree *C,keytype key)
{while(T!=NULL)
if( (1) ) /*找到值为key的结点*/
{*C=T;
break;
}
else if(key<T->data) /*如果要查找的元素比根结点值小,则在其左子树中继续*/
{*F=T;
(2) ;
}
else /*如果要查找的元素比根结点值大,则在其右子树中继续*/
{*F=T;
(3) ;
}
}
int insertbst(bitree *T,keytype key)
{bitree F=NULL,C=NULL,s;
searchbst(*T,&F,&C,key); /*T为二级指针,存储根结点地址的指针*/
if( (4) ) /*如果在当前二叉排序树中存在key值时,不做插入操作*/
return 0;
s=(bitree)malloc(sizeof(bitnode));
s->data=key;
s->left=s->right=NULL;
if( (5) ) /*插入结点为根结点*/
*T=s;
else if(key<F->data)
F->left=s; /*插入新结点*/
else
F->right=s;
return 1;
}
void creatbst(bitree *T) /*根结点的初值为空*/
{keytype key;
*T=NULL;
scanf("%d",&key);
while(key!=-1) /*设-1为输入数据结束标志*/
{insertbst(T,key);
scanf("%d",&key);
}
}
void InOrderTraverse(bitree T)
{if(T!=NULL)
{InOrderTraverse(T->left);
printf("%5d",T->data);
InOrderTraverse(T->right);
}
}
main()
{bitree T;
creatbst(&T);
InOrderTraverse(T);
}