treesearch.c
上传用户:bjtelijie
上传日期:2010-01-01
资源大小:87k
文件大小:3k
源码类别:

数学计算

开发平台:

Visual C++

  1. #include <stdio.h>
  2. #include <stdlib.h> 
  3. #include <string.h>
  4. #define NUM 4
  5. struct tree
  6. {
  7. char name[20];
  8. char city[20];
  9. char sex[10];
  10. char age[10];
  11. char job[10];
  12. struct tree *left;
  13. struct tree *right;
  14. };
  15. struct tree Datas[NUM]=
  16. {
  17. "Willing","Tianjing","Female","21","worker",NULL,NULL,
  18. "Tom","Beijing","Male","31","doctor",NULL,NULL,
  19. "Sun","Weifang","Male","24","student",NULL,NULL,
  20. "Marry","Shanghai","Female","19","techer",NULL,NULL
  21. };
  22. struct tree *construct(
  23. struct tree *root, 
  24. struct tree *r, 
  25. struct tree *Data)
  26. {
  27. if(!r)
  28. {
  29. r = (struct tree *)malloc(sizeof(struct tree));
  30. if(!r)
  31. {
  32. printf("内存分配失败!");
  33. exit(0);
  34. }
  35. r->left = NULL;
  36. r->right = NULL;
  37. strcpy(r->name,Data->name);
  38. strcpy(r->city,Data->city);
  39. strcpy(r->sex,Data->sex);
  40. strcpy(r->age,Data->age);
  41. strcpy(r->job,Data->job);
  42. if(!root)
  43. return r;
  44. if(strcmp(Data->name,root->name)<0)
  45. root->left = r;
  46. else 
  47. root->right = r;
  48. return r;
  49. }
  50. if(strcmp(Data->name,r->name)<0)
  51. construct(r,r->left,Data);
  52. else
  53. construct(r,r->right,Data);
  54. return root;
  55. }
  56. struct tree *Search(root,name)
  57. struct tree *root;
  58. char name[];
  59. {
  60. struct tree *p;
  61. if(root == NULL)
  62. printf("该树为空n");
  63. p = root;
  64. while(strcmp(p->name,name)!=0)
  65. {
  66. if(strcmp(p->name,name)>0)
  67. p = p->left;
  68. else
  69. p = p->right;
  70. if(p == NULL)
  71. break;
  72. }
  73. return(p);
  74. }
  75. void print(struct tree *r)
  76. {
  77. if(!r)
  78. return;
  79. print(r->left);
  80. printf("%sn",r->name);
  81. print(r->right);
  82. }
  83. void print_currentData(struct tree *point)
  84. {
  85. if(point == NULL)
  86. return;
  87. printf(" 姓名:%sn",point->name);
  88. printf(" 城市:%sn",point->city);
  89. printf(" 性别:%sn",point->sex);
  90. printf(" 年龄:%sn",point->age);
  91. printf(" 工作:%sn",point->job);
  92. }
  93. int main(void)
  94. {
  95. int i;
  96. char c[10];
  97. char swap[20];
  98. char name[20];
  99. struct tree *root,*p;
  100. struct tree *temp;
  101. p = NULL;
  102. temp = NULL;
  103. root = NULL;
  104. for(i = 0;i<NUM;i++)
  105. root =construct(root,root,&Datas[i]);
  106. printf("现有人员资料:n");
  107. print(root);
  108. printf("请输入要查找的人的名字n");
  109. scanf("%s",name);
  110. p = Search(root,name);
  111. if(p == NULL)
  112. {
  113. printf("没有该人资料n");
  114. printf("是否要插入该人资料[y/n]n");
  115. scanf("%s",c);
  116. if(strcmp(c,"y")==0)
  117. {
  118. temp = (struct tree *)malloc(sizeof(struct tree));
  119. if(!temp)
  120. {
  121. printf("内存分配失败!");
  122. exit(0);
  123. }
  124. printf("请输入该人姓名:n");
  125. scanf("%s",swap);
  126. strcpy(temp->name,swap);
  127. printf("请输入该人所在城市:n");
  128. scanf("%s",swap);
  129. strcpy(temp->city,swap);
  130. printf("请输入该人性别[Male/Female]:n");
  131. scanf("%s",swap);
  132. strcpy(temp->sex,swap);
  133. printf("请输入该人年龄:n");
  134. scanf("%s",swap);
  135. strcpy(temp->age,swap);
  136. printf("请输入该人工作:n");
  137. scanf("%s",swap);
  138. strcpy(temp->job,swap);
  139. temp->left = NULL;
  140. temp->right = NULL;
  141. root =construct(root,root,temp);
  142. print_currentData(temp);
  143. printf("现有人员资料:n");
  144. root = root;
  145. print(root);
  146. }
  147. else 
  148. return 0;
  149. }
  150. print_currentData(p);
  151. return 1;
  152. }