(4) tree : tsearch tfind tdelete

来源:互联网 发布:阿里巴巴直接传淘宝 编辑:程序博客网 时间:2024/06/05 12:40


1.

#include <search.h>#include <stdlib.h>#include <stdio.h>#include <time.h>/* * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry. * * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *)); * tsearch() returns a pointer to a matching item in the tree, or to the newly added item, * or NULL if  there was  insufficient  memory to add the item. * * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *)); * tfind() returns a pointer to the item, or NULL if no match is found. * If there are multiple elements that match the key, the element returned is unspecified. * * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth)); * * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *)); * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found. */struct stu{int stu_no;char *name;};typedef struct stu stu_t;void *root = NULL;void *xmalloc(unsigned n) {void *p;p = malloc(n);if (p) return p;fprintf(stderr, "malloc failure, insufficient memory.\n");exit(1);}int compare(const void *pa, const void *pb) {return ((stu_t*)pa)->stu_no - ((stu_t*)pb)->stu_no;}/*typedefenum {preorder,postorder,endorder,leaf} VISIT;*/void action(const void *nodep, const VISIT which, const int depth) {switch(which) {case preorder:break;case postorder:printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);break;case endorder:break;case leaf:printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);break;}}int main() {stu_t *ptr;srand(time(NULL));int i;for (i = 0; i < 12; i++) {ptr = (stu_t *)xmalloc(sizeof(stu_t));//ptr->stu_no = (int *)xmalloc(sizeof(int));ptr->stu_no = rand()&0xFF;ptr->name = (char *)xmalloc(20);sprintf(ptr->name, "shanghai_%d", ptr->stu_no);//tsearch = search + create(add)stu_t** rtp = (stu_t**)tsearch(ptr, &root, compare);if (rtp == NULL) {printf("malloc failure....\n");exit(1);}else{printf("tsearch: No. = %6d, Name = %s\n", (*rtp)->stu_no, (*rtp)->name);}}//add a node manuallyptr = (stu_t *)xmalloc(sizeof(stu_t));ptr->stu_no = 88;ptr->name = (char *)xmalloc(20);sprintf(ptr->name, "shanghai_%d", ptr->stu_no);tsearch(ptr, &root, compare);//tfind "88"stu_t** rt_find = tfind(ptr, &root, compare);if (rt_find == NULL) {printf("not found.\n");}else{printf("tfind: No. = %6d, Name = %s\n", (*rt_find)->stu_no, (*rt_find)->name);}//first twalkputs("first twalk, there is '88'");twalk(root, action);// tdelete "88"//returns a pointer to the parent of the item deleted, or NULL if the item was not found.stu_t ** rt_delete = (stu_t **)tdelete(ptr, &root, compare);if (rt_delete == NULL) {printf("'88' is not found to delete.\n");}else{printf("tdelete successful. its parent is: No. = %6d, Name = %s\n", (*rt_delete)->stu_no, (*rt_delete)->name);}//second twalkputs("second twalk, there is no '88'");twalk(root, action);return 0;}


2.

#include <search.h>#include <stdlib.h>#include <stdio.h>#include <time.h>/* * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry. * * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *)); * tsearch() returns a pointer to a matching item in the tree, or to the newly added item, * or NULL if  there was  insufficient  memory to add the item. * * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *)); * tfind() returns a pointer to the item, or NULL if no match is found. * If there are multiple elements that match the key, the element returned is unspecified. * * * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *)); * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found. * * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth)); */void *root = NULL;void *xmalloc(unsigned n) {void *p;p = malloc(n);if (p) return p;fprintf(stderr, "insufficient memory\n");exit(1);}int compare(const void *pa, const void *pb) {if (*(int *)pa < *(int *)pb) return -1;if (*(int *)pa > *(int *)pb) return 1;return 0;}void action(const void *nodep, const VISIT which, const int depth) {int *datap;switch(which) {case preorder:break;case postorder:datap = *(int **)nodep;printf("%6d\n", *datap);break;case endorder:break;case leaf:datap = *(int **)nodep;printf("%6d\n", *datap);break;}}int main() {int i, *ptr;srand(time(NULL));for (i = 0; i < 12; i++) {ptr = (int *)xmalloc(sizeof(int));*ptr = rand()&0xff;//returns a pointer to a matching item in the tree, or to the newly added item,//or NULL if  there was  insufficient  memory to add the item.int **val = (int *)tsearch((void *)ptr, &root, compare);if (val == NULL) {exit(1);}else{printf("value = %d\n", **val);}}twalk(root, action);return 0;}




原创粉丝点击