当前位置:   article > 正文

二叉树--兄弟孩子表示法_二叉树实现孩子兄弟表示法

二叉树实现孩子兄弟表示法

  1. //BTree.h
  2. #ifndef _BTREE_H_
  3. #define _BTREE_H_
  4. #define BT_LEFT 0
  5. #define BT_RIGHT 1
  6. typedef void BTree;
  7. typedef unsigned long long BTPos;
  8. typedef struct _tag_BTreeNode BTreeNode;
  9. struct _tag_BTreeNode
  10. {
  11. BTreeNode* left;
  12. BTreeNode* right;
  13. };
  14. typedef void (BTree_Printf)(BTreeNode*);
  15. BTree* BTree_Create();
  16. void BTree_Destroy(BTree* tree);
  17. void BTree_Clear(BTree* tree);
  18. int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag);
  19. BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count);
  20. BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count);
  21. BTreeNode* BTree_Root(BTree* tree);
  22. int BTree_Height(BTree* tree);
  23. int BTree_Count(BTree* tree);
  24. int BTree_Degree(BTree* tree);
  25. void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div);
  26. #endif

  1. //BTree.c
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include "BTree.h"
  5. typedef struct _tag_BTree TBTree;
  6. struct _tag_BTree
  7. {
  8. int count;
  9. BTreeNode* root;
  10. };
  11. static void recursive_display(BTreeNode* node, BTree_Printf* pFunc, int format, int gap, char div) // O(n)
  12. {
  13. int i = 0;
  14. if( (node != NULL) && (pFunc != NULL) )
  15. {
  16. for(i=0; i<format; i++)
  17. {
  18. printf("%c", div);
  19. }
  20. pFunc(node);
  21. printf("\n");
  22. if( (node->left != NULL) || (node->right != NULL) )
  23. {
  24. recursive_display(node->left, pFunc, format + gap, gap, div);
  25. recursive_display(node->right, pFunc, format + gap, gap, div);
  26. }
  27. }
  28. else
  29. {
  30. for(i=0; i<format; i++)
  31. {
  32. printf("%c", div);
  33. }
  34. printf("\n");
  35. }
  36. }
  37. static int recursive_count(BTreeNode* root) // O(n)
  38. {
  39. int ret = 0;
  40. if( root != NULL )
  41. {
  42. ret = recursive_count(root->left) + 1 + recursive_count(root->right);
  43. }
  44. return ret;
  45. }
  46. static int recursive_height(BTreeNode* root) // O(n)
  47. {
  48. int ret = 0;
  49. if( root != NULL )
  50. {
  51. int lh = recursive_height(root->left);
  52. int rh = recursive_height(root->right);
  53. ret = ((lh > rh) ? lh : rh) + 1;
  54. }
  55. return ret;
  56. }
  57. static int recursive_degree(BTreeNode* root) // O(n)
  58. {
  59. int ret = 0;
  60. if( root != NULL )
  61. {
  62. if( root->left != NULL )
  63. {
  64. ret++;
  65. }
  66. if( root->right != NULL )
  67. {
  68. ret++;
  69. }
  70. if( ret == 1 )
  71. {
  72. int ld = recursive_degree(root->left);
  73. int rd = recursive_degree(root->right);
  74. if( ret < ld )
  75. {
  76. ret = ld;
  77. }
  78. if( ret < rd )
  79. {
  80. ret = rd;
  81. }
  82. }
  83. }
  84. return ret;
  85. }
  86. BTree* BTree_Create() // O(1)
  87. {
  88. TBTree* ret = (TBTree*)malloc(sizeof(TBTree));
  89. if( ret != NULL )
  90. {
  91. ret->count = 0;
  92. ret->root = NULL;
  93. }
  94. return ret;
  95. }
  96. void BTree_Destroy(BTree* tree) // O(1)
  97. {
  98. free(tree);
  99. }
  100. void BTree_Clear(BTree* tree) // O(1)
  101. {
  102. TBTree* btree = (TBTree*)tree;
  103. if( btree != NULL )
  104. {
  105. btree->count = 0;
  106. btree->root = NULL;
  107. }
  108. }
  109. int BTree_Insert(BTree* tree, BTreeNode* node, BTPos pos, int count, int flag) // O(n)
  110. {
  111. TBTree* btree = (TBTree*)tree;
  112. int ret = (btree != NULL) && (node != NULL) && ((flag == BT_LEFT) || (flag == BT_RIGHT));
  113. int bit = 0;
  114. if( ret )
  115. {
  116. BTreeNode* parent = NULL;
  117. BTreeNode* current = btree->root;
  118. node->left = NULL;
  119. node->right = NULL;
  120. while( (count > 0) && (current != NULL) )
  121. {
  122. bit = pos & 1;
  123. pos = pos >> 1;
  124. parent = current;
  125. if( bit == BT_LEFT )
  126. {
  127. current = current->left;
  128. }
  129. else if( bit == BT_RIGHT )
  130. {
  131. current = current->right;
  132. }
  133. count--;
  134. }
  135. if( flag == BT_LEFT )
  136. {
  137. node->left = current;
  138. }
  139. else if( flag == BT_RIGHT )
  140. {
  141. node->right = current;
  142. }
  143. if( parent != NULL )
  144. {
  145. if( bit == BT_LEFT )
  146. {
  147. parent->left = node;
  148. }
  149. else if( bit == BT_RIGHT )
  150. {
  151. parent->right = node;
  152. }
  153. }
  154. else
  155. {
  156. btree->root = node;
  157. }
  158. btree->count++;
  159. }
  160. return ret;
  161. }
  162. BTreeNode* BTree_Delete(BTree* tree, BTPos pos, int count) // O(n)
  163. {
  164. TBTree* btree = (TBTree*)tree;
  165. BTreeNode* ret = NULL;
  166. int bit = 0;
  167. if( btree != NULL )
  168. {
  169. BTreeNode* parent = NULL;
  170. BTreeNode* current = btree->root;
  171. while( (count > 0) && (current != NULL) )
  172. {
  173. bit = pos & 1;
  174. pos = pos >> 1;
  175. parent = current;
  176. if( bit == BT_LEFT )
  177. {
  178. current = current->left;
  179. }
  180. else if( bit == BT_RIGHT )
  181. {
  182. current = current->right;
  183. }
  184. count--;
  185. }
  186. if( parent != NULL )
  187. {
  188. if( bit == BT_LEFT )
  189. {
  190. parent->left = NULL;
  191. }
  192. else if( bit == BT_RIGHT )
  193. {
  194. parent->right = NULL;
  195. }
  196. }
  197. else
  198. {
  199. btree->root = NULL;
  200. }
  201. ret = current;
  202. btree->count = btree->count - recursive_count(ret);
  203. }
  204. return ret;
  205. }
  206. BTreeNode* BTree_Get(BTree* tree, BTPos pos, int count) // O(n)
  207. {
  208. TBTree* btree = (TBTree*)tree;
  209. BTreeNode* ret = NULL;
  210. int bit = 0;
  211. if( btree != NULL )
  212. {
  213. BTreeNode* current = btree->root;
  214. while( (count > 0) && (current != NULL) )
  215. {
  216. bit = pos & 1;
  217. pos = pos >> 1;
  218. if( bit == BT_LEFT )
  219. {
  220. current = current->left;
  221. }
  222. else if( bit == BT_RIGHT )
  223. {
  224. current = current->right;
  225. }
  226. count--;
  227. }
  228. ret = current;
  229. }
  230. return ret;
  231. }
  232. BTreeNode* BTree_Root(BTree* tree) // O(1)
  233. {
  234. TBTree* btree = (TBTree*)tree;
  235. BTreeNode* ret = NULL;
  236. if( btree != NULL )
  237. {
  238. ret = btree->root;
  239. }
  240. return ret;
  241. }
  242. int BTree_Height(BTree* tree) // O(n)
  243. {
  244. TBTree* btree = (TBTree*)tree;
  245. int ret = 0;
  246. if( btree != NULL )
  247. {
  248. ret = recursive_height(btree->root);
  249. }
  250. return ret;
  251. }
  252. int BTree_Count(BTree* tree) // O(1)
  253. {
  254. TBTree* btree = (TBTree*)tree;
  255. int ret = 0;
  256. if( btree != NULL )
  257. {
  258. ret = btree->count;
  259. }
  260. return ret;
  261. }
  262. int BTree_Degree(BTree* tree) // O(n)
  263. {
  264. TBTree* btree = (TBTree*)tree;
  265. int ret = 0;
  266. if( btree != NULL )
  267. {
  268. ret = recursive_degree(btree->root);
  269. }
  270. return ret;
  271. }
  272. void BTree_Display(BTree* tree, BTree_Printf* pFunc, int gap, char div) // O(n)
  273. {
  274. TBTree* btree = (TBTree*)tree;
  275. if( btree != NULL )
  276. {
  277. recursive_display(btree->root, pFunc, 0, gap, div);
  278. }
  279. }

  1. //main.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "BTree.h"
  5. /* run this program using the console pauser or add your own getch, system("pause") or input loop */
  6. struct Node
  7. {
  8. BTreeNode header;
  9. char v;
  10. };
  11. void printf_data(BTreeNode* node)
  12. {
  13. if( node != NULL )
  14. {
  15. printf("%c", ((struct Node*)node)->v);
  16. }
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. BTree* tree = BTree_Create();
  21. struct Node n1 = {{NULL, NULL}, 'A'};
  22. struct Node n2 = {{NULL, NULL}, 'B'};
  23. struct Node n3 = {{NULL, NULL}, 'C'};
  24. struct Node n4 = {{NULL, NULL}, 'D'};
  25. struct Node n5 = {{NULL, NULL}, 'E'};
  26. struct Node n6 = {{NULL, NULL}, 'F'};
  27. BTree_Insert(tree, (BTreeNode*)&n1, 0, 0, 0);
  28. BTree_Insert(tree, (BTreeNode*)&n2, 0x00, 1, 0);
  29. BTree_Insert(tree, (BTreeNode*)&n3, 0x01, 1, 0);
  30. BTree_Insert(tree, (BTreeNode*)&n4, 0x00, 2, 0);
  31. BTree_Insert(tree, (BTreeNode*)&n5, 0x02, 2, 0);
  32. BTree_Insert(tree, (BTreeNode*)&n6, 0x02, 3, 0);
  33. printf("Height: %d\n", BTree_Height(tree));
  34. printf("Degree: %d\n", BTree_Degree(tree));
  35. printf("Count: %d\n", BTree_Count(tree));
  36. printf("Position At (0x02, 2): %c\n", ((struct Node*)BTree_Get(tree, 0x02, 2))->v);
  37. printf("Full Tree: \n");
  38. BTree_Display(tree, printf_data, 4, '-');
  39. BTree_Delete(tree, 0x00, 1);
  40. printf("After Delete B: \n");
  41. printf("Height: %d\n", BTree_Height(tree));
  42. printf("Degree: %d\n", BTree_Degree(tree));
  43. printf("Count: %d\n", BTree_Count(tree));
  44. printf("Full Tree: \n");
  45. BTree_Display(tree, printf_data, 4, '-');
  46. BTree_Clear(tree);
  47. printf("After Clear: \n");
  48. printf("Height: %d\n", BTree_Height(tree));
  49. printf("Degree: %d\n", BTree_Degree(tree));
  50. printf("Count: %d\n", BTree_Count(tree));
  51. BTree_Display(tree, printf_data, 4, '-');
  52. BTree_Destroy(tree);
  53. return 0;
  54. }

  1. //my design --BTree.c
  2. #include "BTree.h"
  3. #include <malloc.h>
  4. #include <stdio.h>
  5. typedef struct _tag_TBTree TBTree;
  6. struct _tag_TBTree //define the node of header
  7. {
  8. int count;
  9. BTreeNode* root;
  10. };
  11. static int recursive_degree(BTreeNode* root)
  12. {
  13. int ret = 0;
  14. if(root!=NULL)
  15. {
  16. if(root->left!=NULL)
  17. {
  18. ret++;
  19. }
  20. if(root->right!=NULL)
  21. {
  22. ret++;
  23. }
  24. if(ret == 1)
  25. {
  26. int ld = recursive_degree(root->left);
  27. int rd = recursive_degree(root->right);
  28. if(ret < ld)
  29. {
  30. ret = ld;
  31. }
  32. if(ret < rd)
  33. {
  34. ret = rd;
  35. }
  36. }
  37. }
  38. return ret;
  39. }
  40. static int recursive_height(BTreeNode* root)
  41. {
  42. int ret = 0;
  43. if(root!=NULL)
  44. {
  45. int lh = recursive_height(root->left);
  46. int rh = recursive_height(root->right);
  47. ret = ((lh>rh)?lh:rh) + 1;
  48. }
  49. return ret;
  50. }
  51. static int recursive_count(BTreeNode* node)
  52. {
  53. int ret = 0;
  54. if(node!=NULL)
  55. {
  56. ret = recursive_count(node->left) + 1 + recursive_count(node->right);
  57. }
  58. return ret;
  59. }
  60. static void recursive_display(BTreeNode* node,BTree_Printf* pFunc,int format,int gap,char div)
  61. {
  62. int i = 0;
  63. if((node!=NULL)&&(pFunc!=NULL))
  64. {
  65. for(i=0;i<format;i++)
  66. {
  67. printf("%c",div);
  68. }
  69. pFunc(node);
  70. printf("\n");
  71. if((node->left!=NULL)||(node->right!=NULL))
  72. {
  73. recursive_display(node->left,pFunc,format+gap,gap,div);
  74. recursive_display(node->right,pFunc,format+gap,gap,div);
  75. }
  76. }
  77. else //when there has only-left child node or only-right child node,print the char to fill the place
  78. {
  79. for(i=0;i<format;i++)
  80. {
  81. printf("%c",div);
  82. }
  83. printf("\n");
  84. }
  85. }
  86. BTree* BTree_Create()
  87. {
  88. TBTree* ret = (TBTree*)malloc(sizeof(TBTree));
  89. if(ret != NULL)
  90. {
  91. ret->count = 0;
  92. ret->root = NULL;
  93. }
  94. return ret;
  95. }
  96. void BTree_Destroy(BTree* tree)
  97. {
  98. free(tree);
  99. }
  100. void BTree_Clear(BTree* tree)
  101. {
  102. TBTree* btree = (TBTree*)tree;
  103. if(btree != NULL)
  104. {
  105. btree->count = 0;
  106. btree->root = NULL;
  107. }
  108. }
  109. int BTree_Insert(BTree* tree,BTreeNode* node,BTPos pos,int count,int flag) // pos :position; count :moving times;
  110. {
  111. int ret = 0;
  112. int bit = 0;
  113. TBTree* btree = (TBTree*)tree;
  114. ret = (btree!=NULL)&&(node!=NULL)&&((flag==BT_LEFT)||(flag==BT_RIGHT));
  115. if(ret)
  116. {
  117. BTreeNode* parent = NULL;
  118. BTreeNode* current = btree->root;
  119. node->left = NULL;
  120. node->right = NULL;
  121. while((count>0)&&(current!=NULL))
  122. {
  123. bit = pos & 1;
  124. pos = pos >> 1;
  125. parent = current;
  126. if(bit==BT_LEFT)
  127. {
  128. current = current->left;
  129. }
  130. else if(bit==BT_RIGHT)
  131. {
  132. current = current->right;
  133. }
  134. count--;
  135. }
  136. if(flag==BT_LEFT) //when the current node has some children node,the current node should be inserted after the node
  137. {
  138. node->left = current;
  139. }
  140. else if(flag == BT_RIGHT)
  141. {
  142. node->right = current;
  143. }
  144. if(parent!=NULL)
  145. {
  146. if(bit == BT_LEFT)
  147. {
  148. parent->left = node;
  149. }
  150. else if(bit == BT_RIGHT)
  151. {
  152. parent->right = node;
  153. }
  154. }
  155. else //insert the first node
  156. {
  157. btree->root = node;
  158. }
  159. btree->count++;
  160. }
  161. return ret;
  162. }
  163. void BTree_Display(BTree* tree,BTree_Printf* pFunc,int gap,char div)
  164. {
  165. TBTree* btree = (TBTree*)tree;
  166. if((btree!=NULL)&&(pFunc!=NULL))
  167. {
  168. recursive_display(btree->root,pFunc,0,gap,div);
  169. }
  170. }
  171. BTreeNode* BTree_Delete(BTree* tree,BTPos pos,int count)
  172. {
  173. BTreeNode* ret = NULL;
  174. TBTree* btree = (TBTree*)tree; //×öÒ»¸ö¿Ç
  175. int bit = 0;
  176. if(btree!=NULL)
  177. {
  178. BTreeNode* parent = NULL;
  179. BTreeNode* current = btree->root;
  180. while((count>0)&&(current!=NULL))
  181. {
  182. bit = pos & 1;
  183. pos = pos >> 1;
  184. parent = current;
  185. if(bit==BT_LEFT)
  186. {
  187. current = current->left;
  188. }
  189. else if(bit==BT_RIGHT)
  190. {
  191. current = current->right;
  192. }
  193. count--;
  194. }
  195. if(parent!=NULL)
  196. {
  197. if(bit==BT_LEFT)
  198. {
  199. parent->left = NULL;
  200. }
  201. else if(bit == BT_RIGHT)
  202. {
  203. parent->right = NULL;
  204. }
  205. }
  206. else //the only one node
  207. {
  208. btree->root = NULL;
  209. }
  210. ret = current; //important
  211. btree->count = btree->count - recursive_count(ret);
  212. }
  213. return ret;
  214. }
  215. BTreeNode* BTree_Get(BTree* tree,BTPos pos,int count)
  216. {
  217. BTreeNode* ret = NULL;
  218. TBTree* btree = (TBTree*)tree;
  219. int bit = 0;
  220. if(btree!=NULL)
  221. {
  222. BTreeNode* parent = NULL;
  223. BTreeNode* current = btree->root;
  224. while((count>0)&&(current!=NULL))
  225. {
  226. bit = pos & 1;
  227. pos = pos >> 1;
  228. parent = current; //record the current node
  229. if(bit == BT_LEFT)
  230. {
  231. current = current->left;
  232. }
  233. else if(bit == BT_RIGHT)
  234. {
  235. current = current->right;
  236. }
  237. count--;
  238. }
  239. ret = current; //important
  240. }
  241. return ret;
  242. }
  243. BTreeNode* BTree_Root(BTree* tree)
  244. {
  245. BTreeNode* ret = NULL;
  246. TBTree* btree = (TBTree*)tree;
  247. if(btree != NULL)
  248. {
  249. ret = btree->root;
  250. }
  251. return ret;
  252. }
  253. int BTree_Height(BTree* tree)
  254. {
  255. int ret = 0;
  256. TBTree* btree = (TBTree*)tree;
  257. if(btree!=NULL)
  258. {
  259. ret = recursive_height(btree->root);
  260. }
  261. return ret;
  262. }
  263. int BTree_Count(BTree* tree)
  264. {
  265. int ret = 0;
  266. TBTree* btree = (TBTree*)tree;
  267. if(btree!=NULL)
  268. {
  269. ret = btree->count;
  270. }
  271. return ret;
  272. }
  273. int BTree_Degree(BTree* tree)
  274. {
  275. int ret = 0;
  276. TBTree* btree = (TBTree*)tree;
  277. if(btree!=NULL)
  278. {
  279. ret = recursive_degree(btree->root);
  280. }
  281. return ret;
  282. }




声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/神奇cpp/article/detail/1014338
推荐阅读
相关标签
  

闽ICP备14008679号