Chinaunix首页 | 论坛 | 博客
  • 博客访问: 209301
  • 博文数量: 136
  • 博客积分: 2919
  • 博客等级: 少校
  • 技术积分: 1299
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-11 09:08
文章分类

全部博文(136)

文章存档

2013年(1)

2011年(135)

我的朋友

分类: C/C++

2011-07-09 09:26:51

2.8 Trees

A tree is a hierarchical data structure that stores a set of items in which each item has a value, may point to zero or more others, and is pointed to by exactly one other. The root of the tree is the sole exception; no item points to it.

Binary search trees have tow links at each node. They're the easiest to implement, and demonstrate the essential properties of trees. A node in a binary search tree has a value and two pointers, left and right, that point to its children. The child pointers may be two null if the node has fewer than two children. In a binary search tree, the values at the nodes define the tree: all children to the left of a particular node have lower values, and all children to the right have higher values.

Because of this property, we can use a variant of binary search to search the tree quickly for a specific value or determine that it is not present.

  1. /**
  2.   tpop(2.8)Algorithm and Data Structure: Trees
  3.   created on Jul 9, 2011
  4.   */


  5.  #include <stdio.h>

  6. typedef struct Nameval Nameval;
  7. struct Nameval {
  8.     char *name;
  9.     int value;
  10.     Nameval *left; /* lesser */
  11.     Nameval *right; /* greater */
  12. };


  13. /* newitem: */
  14. Nameval *newitem(char *name, int value) {
  15.     Nameval *newp;
  16.     newp = (Nameval*)malloc(sizeof(Nameval));
  17.     newp->name = name;
  18.     newp->value = value;
  19.     newp->left = NULL;
  20.     newp->right = NULL;
  21.     return newp;
  22. }

  23. /* insert: insert newp in treep, return treep */
  24. Nameval *insert(Nameval *treep, Nameval *newp) {
  25.     int cmp;

  26.     if(treep == NULL)
  27.       return newp;
  28.     cmp = strcmp(newp->name, treep->name);
  29.     if (cmp == 0)
  30.       printf("insert: duplicate entry %s ignored", newp->name);
  31.     else if (cmp < 0)
  32.       treep->left = insert(treep->left, newp);
  33.     else
  34.       treep->right = insert(treep->right, newp);
  35.     return treep;
  36. }

  37. /* lookup: look up name in tree treep */
  38. Nameval *lookup(Nameval *treep, char *name)
  39. {
  40.     int cmp;

  41.     if (treep == NULL)
  42.       return NULL;
  43.     cmp = strcmp(name, treep->name);
  44.     if (cmp == 0)
  45.       return treep;
  46.     else if (cmp < 0)
  47.       return lookup(treep->left, name);
  48.     else
  49.       return lookup(treep->right, name);
  50. }

  51. /* nrlookup: non-recursively look up name in tree treep */
  52. Nameval *nrlookup(Nameval *treep, char *name)
  53. {
  54.     int cmp;
  55.     while (treep != NULL) {
  56.     cmp = strcmp(name, treep->name);
  57.     if (cmp == 0)
  58.      return treep;
  59.     else if (cmp < 0)
  60.      treep = treep->left;
  61.     else
  62.      treep = treep->right;
  63.     }
  64.     return NULL;
  65. }

  66. /* applyinorder: inorder application of fn to treep
  67.  typical use: applyinorder(treep, printnv, "%s: %x\n");*/
  68. void applyinorder(Nameval *treep,
  69.      void (*fn)(Nameval*, void*), void *arg){
  70.     if (treep == NULL)
  71.       return;
  72.     applyinorder(treep->left, fn, arg);
  73.     (*fn)(treep, arg);
  74.     applyinorder(treep->right, fn, arg);
  75. }

  76. int main(int argc, char* argv[]) {
  77.     Nameval *nt = newitem("smiley", 0x263A);
  78.     Nameval *rc = newitem("zeta", 0x03b6);
  79.     Nameval *lc = newitem("Aacute", 0x00c1);
  80.     nt = insert(nt, lc);
  81.     nt = insert(nt, rc);
  82.     Nameval *look = lookup(nt, "zeta");
  83.      printf("The look result is %s\n", look->name);
  84.     if (nt->left == NULL)
  85.        printf("The right child is %s\n", nt->right->name);
  86.     else
  87.       printf("The left child is %s\n", nt->left->name);
  88. }



阅读(379) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~