050.二叉搜索树操作

举报
C语言与CPP编程 发表于 2022/04/30 00:17:49 2022/04/30
【摘要】 #include <stdio.h>#include <stdlib.h>struct node { int value; struct node* left; struct node* right;};typedef struct node NODE;typedef struct node* PNO...

  
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct node {
  4. int value;
  5. struct node* left;
  6. struct node* right;
  7. };
  8. typedef struct node NODE;
  9. typedef struct node* PNODE;
  10. void new_node (PNODE* n, int value) {
  11. *n = (PNODE)malloc (sizeof(NODE));
  12. if (*n != NULL) {
  13. (*n)->value = value;
  14. (*n)->left = NULL;
  15. (*n)->right = NULL;
  16. }
  17. }
  18. void free_node (PNODE* n) {
  19. if ((*n) != NULL) {
  20. free (*n);
  21. *n = NULL;
  22. }
  23. }
  24. void free_tree (PNODE* n) {
  25. if (*n == NULL) return;
  26. if ((*n)->left != NULL) {
  27. free_tree (&((*n)->left));
  28. }
  29. if ((*n)->right != NULL) {
  30. free_tree (&((*n)->right));
  31. }
  32. free_node (n);
  33. }
  34. //查找结点
  35. PNODE find_node (PNODE n, int value) {
  36. if (n == NULL) {
  37. return NULL;
  38. } else if (n->value == value) {
  39. return n;
  40. } else if (value <= n->value) {
  41. return find_node (n->left, value);
  42. } else {
  43. return find_node (n->right, value);
  44. }
  45. }
  46. //插入结点
  47. void insert_node (PNODE* n, int value) {
  48. if (*n == NULL) {
  49. new_node (n, value);
  50. } else if (value == (*n)->value) {
  51. return;
  52. } else if (value < (*n)->value) {
  53. insert_node (&((*n)->left), value);
  54. } else {
  55. insert_node (&((*n)->right), value);
  56. }
  57. }
  58. //最长路径
  59. int get_max_depth (PNODE n) {
  60. int left = 0;
  61. int right = 0;
  62. if (n == NULL) {
  63. return 0;
  64. }
  65. if (n->left != NULL) {
  66. left = 1 + get_max_depth (n->left);
  67. }
  68. if (n->right != NULL) {
  69. right = 1 + get_max_depth (n->right);
  70. }
  71. return (left > right ? left : right );
  72. }
  73. //最短路径
  74. int get_min_depth (PNODE n) {
  75. int left = 0;
  76. int right = 0;
  77. if (n == NULL) {
  78. return 0;
  79. }
  80. if (n->left != NULL) {
  81. left = 1 + get_min_depth (n->left);
  82. }
  83. if (n->right != NULL) {
  84. right = 1 + get_min_depth (n->right);
  85. }
  86. return (left < right ? left : right );
  87. }
  88. int get_num_nodes (PNODE n) {
  89. int left = 0;
  90. int right = 0;
  91. if (n == NULL) {
  92. return 0;
  93. }
  94. if (n->left != NULL) {
  95. left = get_num_nodes (n->left);
  96. }
  97. if (n->right != NULL) {
  98. right = get_num_nodes (n->right);
  99. }
  100. return (left + 1 + right);
  101. }
  102. //最短路径长度
  103. int get_min_value (PNODE n) {
  104. if (n == NULL) return 0;
  105. if (n->left == NULL) {
  106. return n->value;
  107. } else {
  108. return get_min_value(n->left);
  109. }
  110. }
  111. //最长路径长度
  112. int get_max_value (PNODE n) {
  113. if (n == NULL) return 0;
  114. if (n->right == NULL) {
  115. return n->value;
  116. } else {
  117. return get_max_value(n->right);
  118. }
  119. }
  120. //删除结点
  121. void deletenode (PNODE *n) {
  122. PNODE tmp = NULL;
  123. if (n == NULL) return;
  124. if ((*n)->right == NULL) {
  125. tmp = *n;
  126. *n = (*n)->left;
  127. free_node (n);
  128. } else if ((*n)->left == NULL) {
  129. tmp = *n;
  130. *n = (*n)->right;
  131. free_node (n);
  132. } else {
  133. for (tmp = (*n)->right; tmp->left != NULL; tmp = tmp->left);
  134. tmp->left = (*n)->left;
  135. tmp = (*n);
  136. *n = (*n)->right;
  137. free_node (&tmp);
  138. }
  139. }
  140. void delete_node (PNODE *n, int value) {
  141. PNODE node;
  142. if (n == NULL) return;
  143. node = find_node (*n, value);
  144. if ((*n)->value == value) {
  145. deletenode (n);
  146. } else if (value < (*n)->value) {
  147. delete_node (&((*n)->left), value);
  148. } else {
  149. delete_node(&((*n)->right), value);
  150. }
  151. }
  152. void pre_order_traversal(PNODE n)
  153. {
  154. if (n != NULL) {
  155. printf ("%i ", n->value);
  156. pre_order_traversal (n->left);
  157. pre_order_traversal( n->right);
  158. }
  159. }
  160. void in_order_traversal (PNODE n)
  161. {
  162. if (n != NULL) {
  163. in_order_traversal (n->left);
  164. printf ("%i ", n->value);
  165. in_order_traversal ( n->right);
  166. }
  167. }
  168. void post_order_traversal (PNODE n)
  169. {
  170. if (n != NULL) {
  171. post_order_traversal (n->left);
  172. post_order_traversal (n->right);
  173. printf ("%i ", n->value);
  174. }
  175. }
  176. int main() {
  177. char buf[50];
  178. int option;
  179. PNODE tree = NULL;
  180. PNODE node = NULL;
  181. while (1) {
  182. printf ("--------------------------\n");
  183. printf ("Options are:\n\n");
  184. printf (" 0 Exit\n");
  185. printf (" 1 Insert node\n");
  186. printf (" 2 Delete node\n");
  187. printf (" 3 Find node\n");
  188. printf (" 4 Pre order traversal\n");
  189. printf (" 5 In order traversal\n");
  190. printf (" 6 Post order traversal\n");
  191. printf (" 7 Max depth\n");
  192. printf (" 8 Min depth\n");
  193. printf (" 9 Max value\n");
  194. printf (" 10 Min value\n");
  195. printf (" 11 Node Count\n\n");
  196. printf ("--------------------------\n");
  197. printf ("Select an option: ");
  198. fgets (buf, sizeof(buf), stdin);
  199. sscanf (buf, "%i", &option);
  200. printf ("--------------------------\n");
  201. if (option < 0 || option > 11) {
  202. fprintf (stderr, "Invalid option");
  203. continue;
  204. }
  205. switch (option) {
  206. case 0:
  207. exit (0);
  208. case 1:
  209. printf ("Enter number to insert: ");
  210. fgets (buf, sizeof(buf), stdin);
  211. sscanf (buf, "%i", &option);
  212. printf ("\n\n");
  213. insert_node (&tree, option);
  214. break;
  215. case 2:
  216. printf ("Enter number to delete: ");
  217. fgets (buf, sizeof(buf), stdin);
  218. sscanf (buf, "%i", &option);
  219. printf ("\n\n");
  220. delete_node (&tree, option);
  221. break;
  222. case 3:
  223. printf ("Enter number to find: ");
  224. fgets (buf, sizeof(buf), stdin);
  225. sscanf (buf, "%i", &option);
  226. printf ("\n\n");
  227. node = find_node (tree, option);
  228. if (node != NULL) {
  229. printf ("Found node\n\n");
  230. } else {
  231. printf ("Couldn't find node\n\n");
  232. }
  233. break;
  234. case 4:
  235. printf ("Pre order traversal: ");
  236. pre_order_traversal (tree);
  237. printf ("\n\n");
  238. break;
  239. case 5:
  240. printf ("In order traversal: ");
  241. in_order_traversal (tree);
  242. printf ("\n\n");
  243. break;
  244. case 6:
  245. printf ("Post order traversal: ");
  246. post_order_traversal (tree);
  247. printf ("\n\n");
  248. break;
  249. case 7:
  250. printf ("Max depth is %i\n\n", get_max_depth (tree));
  251. break;
  252. case 8:
  253. printf ("Min depth is %i\n\n", get_min_depth (tree));
  254. break;
  255. case 9:
  256. printf ("Max value is %i\n\n", get_max_value (tree));
  257. break;
  258. case 10:
  259. printf ("Min value is %i\n\n", get_min_value (tree));
  260. break;
  261. case 11:
  262. printf ("Node Count is %i\n\n", get_num_nodes (tree));
  263. break;
  264. }
  265. }
  266. return 0;
  267. }

文章来源: blog.csdn.net,作者:程序员编程指南,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/weixin_41055260/article/details/124491572

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。