二叉树的排序与查找上机实验
- 格式:doc
- 大小:95.50 KB
- 文档页数:3
第 二 次实验报告
实验名称: 二叉排序树的查找
实验日期: 2014年6月20日
一:实验目的
掌握和更好的了解二叉树的排序及其查找的相关内容,知道如何在一些简单程序中去使用这些算法。
二:实验内容、步骤及结果
代码清单如下:
#include
#include
#include
typedef struct student
{
int num1;
char num2[10];
char name[10];
int classa;
char sex;
}
LISTU;
typedef struct tnode
{
int key;
LISTU data;
struct tnode*lchild,*rchild;
}TNODE;
int main()
{
TNODE *create_binary_sort_tree(LISTU*stu);
TNODE *bstsearch(TNODE*root,int k);
void print_out(TNODE*data);
struct student stu[8]={
{26,"110610125","WXD",111,'M'},
{25,"110610124","WQH",111,'M'},
{41,"110610210","CL",112,'M'},
{8,"110610107","YTT",111,'F'},
{68,"110610313","LBZ",113,'M'},
{44,"110610215","HHF",112,'M'},
{36,"110610205","WY",112,'F'},
{38,"110610207","ZLF",112,'F'}
};
TNODE*search1;
TNODE*search2;
TNODE*tree;
tree=create_binary_sort_tree(stu);
printf("根节点信息是:\n");
print_out(tree);
search1=bstsearch(tree,8);
printf("序号为8的学生的信息为:\n");
print_out(search1);
search2=bstsearch(tree,38);
printf("序号为38的学生的信息为:\n");
print_out(search2);
return 0;
}
TNODE *create_binary_sort_tree(LISTU*stu )
{
int i;
TNODE*head,*s,*p,*q;
LISTU*t;
head=NULL;
for(i=0;i<8;i++)
{
t=stu;
s=(TNODE*)malloc(sizeof(TNODE));
s->key=(t+i)->num1;
s->data=*(t+i);
s->lchild=NULL;
s->rchild=NULL;
if(head==NULL)
head=s;
else
{
p=head;
while(p!=NULL)
{
q=p;
if(s->key
p=p->lchild;
else
p=p->rchild;
}
if(s->key
q->lchild=s;
else
q->rchild=s;
}
}
return head;
}
TNODE *bstsearch(TNODE*root,int k)
{
if(root==NULL)
return NULL;
else if (root ->key==k)
return root;
else if(root->key>k)
return bstsearch(root->lchild,k);
else
return bstsearch(root->rchild,k);
}
void print_out(TNODE*data)
{
printf("%d\n",data->data.num1);
printf("%s\n",data->data.num2);
printf("%s\n",data->data.name);
printf("%d\n",data->data.classa);
printf("%c\n",data->data.sex);
}
截图:
三:小结
通过本次实验,对二叉树的排序以及查找的相关内容能够更好的掌握和消化,更应注意对所学知识在实际
中应用,以达到学习的目的,实验中也应注意细节。