1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#ifndef _TREE_H_ #define _TREE_H_ #include <stdbool.h> typedef struct item { char petname[20]; char petkind[20]; } Item; #define MAXITEMS 10 typedef struct node { Item item; struct node * left; struct node * right; } Node; typedef struct tree { Node * root; //指向树根的指针 int size; //树中项目的个数 } Tree; /* 操作:把一个树初始为空树 操作前:ptree指向一个树 操作后:该树已被初始化为空树 */ void InitializeTree(Tree * ptree); /* 操作:确定树是否为空 操作前:ptree指向一个树 操作后:如果树为空则返回true,否则返回false */ bool TreeIsEmpty(const Tree * ptree); /* 操作:确定树是否已满 操作前:ptree指向一个树 操作后:如果树已满则返回true,否则返回false */ bool TreeIsFull(const Tree * ptree); /* 操作:确定树中项目的个数 操作前:ptree指向一个树 操作后:返回树中项目的个数 */ int TreeItemCount(const Tree * ptree); /* 操作:向树中添加一个项目 操作前:pi是待添加的项目的地址 ptree指向一个已经初始化的树 操作后:如果可能,函数把该项目添加到 树中并返回true,否则返回false */ bool AddItem(const Item * pi, Tree * ptree); /* 操作:在树中查找一个项目 操作前:pi指向一个项目 ptree指向一个已经初始化的树 操作后:如果该项目在树中,则函数返回true, 否则返回false */ bool InTree(const Item * pi, const Tree * ptree); /* 操作:从树中删除一个项目 操作前:pi是待删除的项目的地址 ptree指向一个已经初始化的树 操作后:如果可能,函数从树中删除该项目 并返回true,否则返回false */ bool DeleteItem(const Item * pi, Tree * ptree); /* 操作:把一个函数作用于树中每一个项目 操作前:ptree指向一棵树 pfun指向一个没有返回值的函数 该函数接受一个Item作为参数 操作后:pfun指向的函数被作用于树中每个项目一次 */ void Traverse(const Tree * ptree, void(* pfun)(Item item)); /* 操作:从树中删除所有节点 操作前:ptree指向一个已经初始化的树 操作后:该树为空树 */ void DeleteAll(Tree * ptree); #endif |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
#include <string.h> #include <stdio.h> #include <stdlib.h> #include "tree.h" //局部数据类型 typedef struct pair { Node * parent; Node * child; } Pair; static Node * MakeNode(const Item * pi); static bool ToLeft(const Item * i1, const Item * i2); static bool ToRight(const Item * i1, const Item * i2); static void AddNode(Node * new_node, Node * root); static void InOrder(const Node * root, void(*pfun)(Item item)); static Pair SeekItem(const Item * pi, const Tree * ptree); static void DeleteNode(Node **ptr); static void DeleteAllNodes(Node * ptr); void InitializeTree(Tree * ptree) { ptree->root = NULL; ptree->size = 0; } bool TreeIsEmpty(const Tree * ptree) { if (ptree->root == NULL) { return true; } else { return false; } } bool TreeIsFull(const Tree * ptree) { if (ptree->size == MAXITEMS) { return true; } else { return false; } } int TreeItemCount(const Tree * ptree) { return ptree->size; } bool AddItem(const Item * pi, Tree * ptree) { Node * new_node; if (TreeIsFull(ptree)) { fprintf(stderr, "Tree is full\n"); return false; } if (SeekItem(pi, ptree).child != NULL) { fprintf(stderr, "Attempted to add duplicate item\n"); return false; } new_node = MakeNode(pi); if (new_node == NULL) { fprintf(stderr, "Could't create Node\n"); return false; } ptree->size++; if (ptree->root == NULL) //树为空树 { ptree->root = new_node; } else { AddNode(new_node, ptree->root); } return true; } bool InTree(const Item * pi, const Tree * ptree) { return (SeekItem(pi, ptree).child == NULL) ? false : true; } bool DeleteItem(const Item * pi, Tree * ptree) { Pair look; look = SeekItem(pi, ptree); if (look.child == NULL) { return false; } if (look.parent == NULL) //删除根项目 { DeleteNode(&ptree->root); } else if(look.parent->left == look.child) { DeleteNode(&look.parent->left); } else { DeleteNode(&look.parent->right); } ptree->size--; return true; } void Traverse(const Tree * ptree, void(*pfun)(Item item)) { if (ptree != NULL) { InOrder(ptree->root, pfun); } } void DeleteAll(Tree * ptree) { if (ptree != NULL) { DeleteAllNodes(ptree->root); } ptree->root = NULL; ptree->size = 0; } static void InOrder(const Node * root, void(*pfun)(Item item)) { if (root != NULL) { InOrder(root->left, pfun); (*pfun)(root->item); InOrder(root->right, pfun); } } static void DeleteAllNodes(Node * root) { Node * pright; if (root != NULL) { pright = root->right; DeleteAllNodes(root->left); free(root); DeleteAllNodes(pright); } } static void AddNode(Node * new_node, Node * root) { // new_node->item的指针,不是new_node的指针->item if (ToLeft(&new_node->item, &root->item)) { if (root->left == NULL) //空子数 { root->left = new_node; //因此把节点添加到此处 } else { AddNode(new_node, root->left); //否则处理该子树 } } else if (ToRight(&new_node->item, &root->item)) { if (root->right == NULL) { root->right = new_node; } else { AddNode(new_node, root->right); } } else { //不允许含有相同的项目 fprintf(stderr, "location error in AddNode()\n"); exit(1); } } static bool ToLeft(const Item * i1, const Item * i2) { int comp1; if ((comp1 = strcmp(i1->petname, i2->petname)) < 0) { return true; } else if (comp1 == 0 && strcmp(i1->petkind, i2->petkind) < 0) { return true; } else { return false; } } static bool ToRight(const Item * i1, const Item * i2) { int comp1; if ((comp1 = strcmp(i1->petname, i2->petname)) > 0) { return true; } else if (comp1 == 0 && strcmp(i1->petkind, i2->petkind) > 0) { return true; } else { return false; } } static Node * MakeNode(const Item * pi) { Node * new_node; new_node = (Node *) malloc(sizeof(Node)); if (new_node != NULL) { new_node->item = *pi; new_node->left = NULL; new_node->right = NULL; } return new_node; } static Pair SeekItem(const Item * pi, const Tree * ptree) { Pair look; look.parent = NULL; look.child = ptree->root; if (look.child == NULL) { return look; } while(look.child != NULL) { if (ToLeft(pi, &(look.child->item))) { look.parent = look.child; look.child = look.child->left; } else if (ToRight(pi, &(look.child->item))) { look.parent = look.child; look.child = look.child->right; } else { //break出循环,说明在树中找到了相关项 //如果循环到look.child == NULL,表示没有找到相关项 break; } } return look; } static void DeleteNode(Node **ptr) //ptr是指向目标节点的父节点指针成员的地址 { Node * temp; puts((*ptr)->item.petname); if((*ptr)->left == NULL) { // 左节点为空,将其右节点顶上 temp = *ptr; *ptr = (*ptr)->right; } else if ((*ptr)->right == NULL) { // 右节点为空,将其左节点顶上 temp = *ptr; *ptr = (*ptr)->left; } else { //被删除的节点有两个子节点 //从左节点起,一路向子节点的右节点寻找,直到找到一个空位(NULL) //将右节点顶在左节点的最右子节点的空位上 //将左节点顶为根节点 for (temp = (*ptr)->left; temp->right != NULL; temp = temp->right) { continue; } temp->right = (*ptr)->right; temp = *ptr; *ptr = (*ptr)->left; } //释放需要删除的节点(地址指向临时节点) free(temp); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <stdio.h> #include <string.h> #include <ctype.h> #include "tree.h" char menu(void); void addpet(Tree * pt); void droppet(Tree * pt); void showpets(const Tree * pt); void findpet(const Tree * pt); void printitem(Item item); void uppercase(char * str); int main(int argc, char const *argv[]) { /* 数组 链表 优点: C对其直接支持 运行时决定其大小 提供随机访问 快速插入和删除元素 缺点: 编译时决定其大小 不能随机访问(如果你要 插入和删除元素很费时 查找链表中的某个值,需要 (需要在数组头部或中间位置, 遍历链表,直到找到为止) 插入或删除元素时,需要调整 用户必须提供编程支持 后面的所有元素) 总结:如果需要频繁插入和删除元素,并且不需要经常搜索,链表是更好的选择 如果列表基本稳定,只是偶尔插入或删除一些元素,但却需要经常搜索,则数组是更好的选择 如果需要两者兼备,二叉搜索树,可能正是你所需要的 二叉搜索树(binary search tree)是一种结合了折半搜索策略的链接结构 */ Tree pets; char choice; InitializeTree(&pets); while((choice = menu()) != 'q') { switch(choice) { case 'a': addpet(&pets);break; case 'l': showpets(&pets);break; case 'f': findpet(&pets);break; case 'n': printf("%d pets in club\n", TreeItemCount(&pets));break; case 'd': droppet(&pets);break; default : puts("switching error"); } } DeleteAll(&pets); puts("Bye."); return 0; } char menu(void) { int ch; puts("Nerfville Pet Club Membership Program"); puts("Enter the letter corresponding to your choice"); puts("a) add a pet"); puts("l) show list of pets"); puts("n) number of pets"); puts("f) find pets"); puts("d) delete a pet"); puts("q) quit"); while((ch = getchar()) != EOF) { while(getchar() != '\n') continue; //丢弃输入行的剩余部分 ch = tolower(ch); if (strchr("alrfndq", ch) == NULL) { puts("Please enter an a,l,f,n,d or q"); } else { break; } } if (ch == EOF) //令EOF导致程序退出 { ch = 'q'; } return ch; } void addpet(Tree * pt) { Item temp; if (TreeIsFull(pt)) { puts("No room in the club!"); } else { puts("Please enter name of pet"); gets(temp.petname); puts("Please enter pet kind"); gets(temp.petkind); uppercase(temp.petname); uppercase(temp.petkind); AddItem(&temp, pt); } } void showpets(const Tree * pt) { if (TreeIsEmpty(pt)) { puts("No entries!"); } else { Traverse(pt, printitem); } } void printitem(Item item) { printf("Pet:%-19s Kind:%-19s\n", item.petname, item.petkind); } void findpet(const Tree * pt) { Item temp; if (TreeIsEmpty(pt)) { puts("No entries!"); return; } puts("Please enter name of pet you wish to find"); gets(temp.petname); puts("Please enter pet kind"); gets(temp.petkind); uppercase(temp.petname); uppercase(temp.petkind); printf("%s the %s\n", temp.petname, temp.petkind); if (InTree(&temp, pt)) { printf("is a member\n"); } else { printf("is not a member\n"); } } void droppet(Tree * pt) { Item temp; if (TreeIsEmpty(pt)) { puts("No entries"); return; } puts("Please enter name of pet you wish to delete"); gets(temp.petname); puts("Please enter pet kind"); gets(temp.petkind); uppercase(temp.petname); uppercase(temp.petkind); printf("%s the %s\n", temp.petname, temp.petkind); if (DeleteItem(&temp, pt)) { printf("is dropped from the club\n"); } else { printf("is not a member\n"); } } void uppercase(char * str) { while(*str) { *str = toupper(*str); str++; } } |