调试如图:
/* tree.h 于2016-8-14完成 */ #ifndef _TREE_H_ #define _TREE_H_ #include<stdbool.h> typedef struct item { char name[40]; short score; }Item; typedef struct node { struct node* left; struct node* right; Item item; }Node; typedef struct tree { Node *root; int size; }Tree; /************************** **函数原型 **操作:把一个树初始化为空树 **操作前:ptree指向一个树 **操作后:该树已被初始化为空树 ***************************/ void InitializeTree(Tree *ptree); /************************** **函数原型 **操作:确定数是否为空 **操作前:ptree指向一个数 **操作后:树为空返回true;否则返回false ***************************/ bool TreeIsEmpty(const Tree *ptree); /************************** **操作:把一个树初始化为空树 **操作前:ptree指向一个树 **操作后:该树已被初始化为空树 ***************************/ void InitializeTree(Tree *ptree); /************************** **操作:确定树中项目的个数 **操作前:ptree指向一个树 **操作后:函数返回树中项目的个数 ***************************/ int TreeItemCount(const Tree *ptree); /************************** **操作:向数添加一个项目 **操作前:item是待添加的项目 ptree指向已经初始化的数 **操作后:函数返回树中项目的个数 ***************************/ void AddItem( const Item *item,Tree *ptree); /************************** **操作:把一个函数作用于树中的每一个项目 **操作前:ptree指向一个树 pfun指向一个没有返回值的函数 该函数接受一个Item作为参数 **操作后:pfun指向的函数被作用于树中每一个项目一次 ***************************/ void Traverse(const Tree* ptree, void(*pfun)(Item item)); /************************** **操作:从树中删除所有节点 **操作前:ptree指向一个已经初始化的树 **操作后:该数为空树 ***************************/ void DeleteAll(Tree *ptree); /************************** **操作:查找指定分数的学生 **操作前:ptree指向一个树;socre为要查找的分数 **操作后:输出分数为socre的学生姓名 ***************************/ void FindByScore(int score, Tree *ptree); #endif // !
/* tree.c 于2016-8-14完成 */ #define _CRT_SECURE_NO_WARNINGS #include"tree.h" #include<stdlib.h> #include<string.h> static bool Toleft(const Item root_item, const Item new_item); static bool ToRight(const Item root_item, const Item new_item); static void AddNode(Node *new_node, Node *root); static void FreeNode(Node *root); static void LookForByScore(int socre, Node *root); void InitializeTree( Tree * ptree) { ptree->root = NULL; ptree->size = 0; } int TreeItemCount(const Tree * ptree) { return ptree->size; } void AddItem(const Item *item, Tree * ptree) { Node* new_node = (Node *)malloc(sizeof(Node)); strcpy(new_node->item.name, item->name); new_node->item.score = item->score; new_node->left = new_node->right = NULL; if (ptree->root == NULL) { ptree->root = new_node; ptree->size++; return; } ptree->size++; AddNode(new_node, ptree->root); } void Traverse(const Tree * ptree, void(*pfun)(Item item)) { } void DeleteAll(Tree * ptree) { FreeNode(ptree->root); ptree->root = NULL; } void FindByScore(int score, Tree * ptree) { if (ptree->root->item.score == score) printf("%s、", ptree->root->item.name); LookForByScore(score, ptree->root); } bool TreeIsEmpty(const Tree * ptree) { if(ptree->size==0) return true; else return false; } bool Toleft(const Item root_item, const Item new_item) { if (root_item.score >= new_item.score) return true; else return false; } bool ToRight(const Item root_item, const Item new_item) { if (root_item.score < new_item.score) return true; else return false; } void AddNode(Node *new_node, Node * root) { if (Toleft(root->item, new_node->item)) { if (root->left == NULL) { root->left = new_node; return; } AddNode(new_node, root->left); } else if (ToRight(root->item, new_node->item)) { if (root->right == NULL) { root->right = new_node; return; } AddNode(new_node, root->right); } } void FreeNode(Node * root) { if (root->left != NULL) { FreeNode(root->left); free(root->left); } if (root->right != NULL) { FreeNode(root->right); free(root->right); } } void LookForByScore(int score, Node * root) { if (root->left != NULL) { if (root->left->item.score == score) printf("%s、", root->left->item.name); LookForByScore(score, root->left); } if (root->right != NULL) { if (root->right->item.score == score) printf("%s、", root->right->item.name); LookForByScore(score, root->right); } }
/* main.c 于2016-8-14完成 */ #define _CRT_SECURE_NO_WARNINGS #include"tree.h" #include<string.h> #include<stdlib.h> #include<stdio.h> int main(void) { Tree *tree = (Tree*)malloc(sizeof(Tree)); Item *item = (Item*)malloc(sizeof(Item)); char name[40]; int score; printf("请输入学生名字(直接回车退出,英文):"); InitializeTree(tree); while (gets_s(name, 6)!=NULL&&name[0]!='\0') { strcpy(item->name, name); printf("请输入学生分数:"); scanf("%d", &score); while (getchar() != '\n'); item->score = score; AddItem(item, tree); printf("请输入学生名字:"); } printf("请输入您要查找的学生的分数:"); scanf("%d", &score); FindByScore(score, tree); DeleteAll(tree); return 0; }
