数据结构个人电话号码查询系统实验报告 幼教玩具批发厂家电话号码查询
数据结构个人电话号码查询系统实验报告
实验目的及要求目的:通过设计一个《个人电话号码查询系统》,进一步熟悉一些二叉树的概念、以及基本知识和技能,利用所学的基本知识和技能解决简单的面向对象的程序设计问题。实现根据用户输入的信息进行快速的查询。
要求:实现一个简单的个人电话号码查询系统,根据用户输入的信息(例如姓名等),对数据进行二叉排序并实现平衡二叉树,对数据进行快速查询。编程完成通讯录的一般性管理工作,如通讯录中记录的增加、修改、查找、删除、输出等功能。每个记录包含姓名、电话号码、住址等个人基本信息。
(1)在外存上,用文件保存电话号码信息
(2)在内存中,设计数据结构存储电话号码信息
(3)提供查询功能,如根据姓名实现快速查询
(4)提供其他维护功能,例如插入、删除、修改等。
实验步骤1.实验问题分析
需要用到数据结构课上学到的平衡二叉树的知识,实现删除和插入,增加功能。理解关于二叉树的相关的基本算法。将输入的信息保存入文件和从文件输出。
2.实验概要分析
1.用结构体存储联系人的信息;
2.初始化二叉树;
3.记录结点的高度,并解决结点不平衡问题;
4.实现增加联系人,查询功能;
5.主函数。
实验内容代码的实现如下:
#include#includestructstu_num{charstu_name[30]//姓名intnum//电话intnumber//编号}//定义一个二叉树的结构体typedefstructperson_list{structperson_list*lchild//左孩子structperson_list*rchild//右孩子intheightstructstu_numperfor_info//结构体类型的数据}person_node//初始化二叉树person_node*request_person_node(conststructstu_num*value){person_node*new_nodenew_node=malloc(sizeof(person_node))if(new_node==NULL){perror("未申请到二叉树节点 ")returnNULL}if(value!=NULL){new_node->perfor_info=*valuenew_node->lchild=NULLnew_node->rchild=NULL}returnnew_node}//记录节点的高度#defineMAX(A,B)((A)>(B)?(A):(B))intget_tree_height(person_node*root){if(root==NULL)return0returnMAX(get_tree_height(root->lchild),get_tree_height(root->rchild))+1}//解决左左不平衡person_node*tree_node_rotate_right(person_node*root){person_node*tmptmp=root->lchildroot->lchild=tmp->rchildtmp->rchild=roottmp->height=get_tree_height(tmp)root->height=get_tree_height(root)returntmp//返回新的root的地址给调用这个函数的用户,用于更新新的root地址}//解决右右不平衡person_node*tree_node_rotate_left(person_node*root){person_node*tmptmp=root->rchildroot->rchild=tmp->lchildtmp->lchild=roottmp->height=get_tree_height(tmp)root->height=get_tree_height(root)returntmp}//解决左右不平衡person_node*tree_node_rotate_left_right(person_node*root){root->lchild=tree_node_rotate_left(root->lchild)root=tree_node_rotate_right(root)returnroot}//解决右左不平衡person_node*tree_node_rotate_right_left(person_node*root){root->rchild=tree_node_rotate_right(root->rchild)root=tree_node_rotate_left(root)returnroot}//增加插入个人电话信息person_node*insert_node_to_tree(person_node*root,person_node*new_node){if(root==NULL)returnnew_nodeif(root->perfor_info.number>new_node->perfor_info.number){root->lchild=insert_node_to_tree(root->lchild,new_node)}else{root->rchild=insert_node_to_tree(root->rchild,new_node)}if(get_tree_height(root->lchild)-get_tree_height(root->rchild)==2)//左不平衡{//如果插入的数据比跟的左树节点的数据要小,那他肯定是插入到root->lchild的左边去,出现了左左不平衡if(new_node->perfor_info.number4.实验结果
5.实验总结分析
通过这次实验,体会到要掌握以下几点内容:
1.要学会先做好模块,从大到小,进行细化。 2.编写好函数,并进行测试与调试。
3.写程序时要善于使用库函数,可以提高效率。
4.定义函数时,应选好参数的个数和数据类型。
但仍需进一步的学习二叉树地相关知识,才能够更加熟练的运用数据结构(C++)来设计系统。