二叉查找树系统操作------tsearch tfind tdelete

604 篇文章 8 订阅
579 篇文章 5 订阅
  1. #include <search.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <time.h>  
  5. /* 
  6.  * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry. 
  7.  * 
  8.  * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *)); 
  9.  * tsearch() returns a pointer to a matching item in the tree, or to the newly added item, 
  10.  * or NULL if  there was  insufficient  memory to add the item. 
  11.  * 
  12.  * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *)); 
  13.  * tfind() returns a pointer to the item, or NULL if no match is found. 
  14.  * If there are multiple elements that match the key, the element returned is unspecified. 
  15.  * 
  16.  * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth)); 
  17.  * 
  18.  * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *)); 
  19.  * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found. 
  20.  */  
  21. struct stu{  
  22.     int stu_no;  
  23.     char *name;  
  24. };  
  25.   
  26. typedef struct stu stu_t;  
  27.   
  28. void *root = NULL;  
  29.   
  30. void *xmalloc(unsigned n) {  
  31.     void *p;  
  32.     p = malloc(n);  
  33.     if (p) return p;  
  34.     fprintf(stderr, "malloc failure, insufficient memory.\n");  
  35.     exit(1);  
  36. }  
  37.   
  38. int compare(const void *pa, const void *pb) {  
  39.     return ((stu_t*)pa)->stu_no - ((stu_t*)pb)->stu_no;  
  40. }  
  41.   
  42. /* 
  43. typedef enum { 
  44.     preorder, 
  45.     postorder, 
  46.     endorder, 
  47.     leaf 
  48. } VISIT; 
  49. */  
  50. void action(const void *nodep, const VISIT which, const int depth) {  
  51.     switch(which) {  
  52.         case preorder:  
  53.             break;  
  54.         case postorder:  
  55.             printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);  
  56.             break;  
  57.         case endorder:  
  58.             break;  
  59.         case leaf:  
  60.             printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);  
  61.             break;  
  62.     }  
  63. }  
  64.   
  65. int main() {  
  66.     stu_t *ptr;  
  67.     srand(time(NULL));  
  68.     int i;  
  69.     for (i = 0; i < 12; i++) {  
  70.         ptr = (stu_t *)xmalloc(sizeof(stu_t));  
  71.         //ptr->stu_no = (int *)xmalloc(sizeof(int));  
  72.         ptr->stu_no = rand()&0xFF;  
  73.         ptr->name = (char *)xmalloc(20);  
  74.         sprintf(ptr->name, "shanghai_%d", ptr->stu_no);  
  75.         //tsearch = search + create(add)  
  76.         stu_t** rtp = (stu_t**)tsearch(ptr, &root, compare);  
  77.         if (rtp == NULL) {  
  78.             printf("malloc failure....\n");  
  79.             exit(1);  
  80.         }else{  
  81.             printf("tsearch: No. = %6d, Name = %s\n", (*rtp)->stu_no, (*rtp)->name);  
  82.         }  
  83.     }  
  84.     //add a node manually  
  85.     ptr = (stu_t *)xmalloc(sizeof(stu_t));  
  86.     ptr->stu_no = 88;  
  87.     ptr->name = (char *)xmalloc(20);  
  88.     sprintf(ptr->name, "shanghai_%d", ptr->stu_no);  
  89.     tsearch(ptr, &root, compare);  
  90.   
  91.     //tfind "88"  
  92.     stu_t** rt_find = tfind(ptr, &root, compare);  
  93.     if (rt_find == NULL) {  
  94.         printf("not found.\n");  
  95.     }else{  
  96.         printf("tfind: No. = %6d, Name = %s\n", (*rt_find)->stu_no, (*rt_find)->name);  
  97.     }  
  98.     //first twalk  
  99.     puts("first twalk, there is '88'");  
  100.     twalk(root, action);  
  101.     // tdelete "88"  
  102.     //returns a pointer to the parent of the item deleted, or NULL if the item was not found.  
  103.     stu_t ** rt_delete = (stu_t **)tdelete(ptr, &root, compare);  
  104.     if (rt_delete == NULL) {  
  105.         printf("'88' is not found to delete.\n");  
  106.     }else{  
  107.         printf("tdelete successful. its parent is: No. = %6d, Name = %s\n", (*rt_delete)->stu_no, (*rt_delete)->name);  
  108.     }  
  109.     //second twalk  
  110.     puts("second twalk, there is no '88'");  
  111.     twalk(root, action);  
  112.   
  113.     return 0;  

[plain] view plain copy
  1. #include <search.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>  
  4. #include <time.h>  
  5. /*  
  6.  * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry.  
  7.  *  
  8.  * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *));  
  9.  * tsearch() returns a pointer to a matching item in the tree, or to the newly added item,  
  10.  * or NULL if  there was  insufficient  memory to add the item.  
  11.  *  
  12.  * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *));  
  13.  * tfind() returns a pointer to the item, or NULL if no match is found.  
  14.  * If there are multiple elements that match the key, the element returned is unspecified.  
  15.  *  
  16.  *  
  17.  * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *));  
  18.  * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found.  
  19.  *  
  20.  * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth));  
  21.  */  
  22.   
  23. void *root = NULL;  
  24.   
  25. void *xmalloc(unsigned n) {  
  26.     void *p;  
  27.     p = malloc(n);  
  28.     if (p) return p;  
  29.     fprintf(stderr, "insufficient memory\n");  
  30.     exit(1);  
  31. }  
  32.   
  33. int compare(const void *pa, const void *pb) {  
  34.     if (*(int *)pa < *(int *)pb) return -1;  
  35.     if (*(int *)pa > *(int *)pb) return 1;  
  36.     return 0;  
  37. }  
  38.   
  39. void action(const void *nodep, const VISIT which, const int depth) {  
  40.     int *datap;  
  41.     switch(which) {  
  42.         case preorder:  
  43.             break;  
  44.         case postorder:  
  45.             datap = *(int **)nodep;  
  46.             printf("%6d\n", *datap);  
  47.             break;  
  48.         case endorder:  
  49.             break;  
  50.         case leaf:  
  51.             datap = *(int **)nodep;  
  52.             printf("%6d\n", *datap);  
  53.             break;  
  54.     }  
  55. }  
  56.   
  57. int main() {  
  58.     int i, *ptr;  
  59.   
  60.     srand(time(NULL));  
  61.     for (i = 0; i < 12; i++) {  
  62.         ptr = (int *)xmalloc(sizeof(int));  
  63.         *ptr = rand()&0xff;  
  64.         //returns a pointer to a matching item in the tree, or to the newly added item,  
  65.         //or NULL if  there was  insufficient  memory to add the item.  
  66.         int **val = (int *)tsearch((void *)ptr, &root, compare);  
  67.         if (val == NULL) {  
  68.             exit(1);  
  69.         }else{  
  70.             printf("value = %d\n", **val);  
  71.         }  
  72.     }  
  73.     twalk(root, action);  
  74.     return 0;  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值