• 二叉数与广义表互相转换


    1. //二叉树广义表互转
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <time.h>
    5. #include <string.h>
    6. #define MAX 10
    7. typedef struct Node {
    8. int key;
    9. struct Node* lchild, * rchild;
    10. }Node;
    11. Node* getNewNode(int key) {
    12. Node* p = (Node*)malloc(sizeof(Node));
    13. p->key = key;
    14. p->lchild = p->rchild = NULL;
    15. return p;
    16. }
    17. Node* insert(Node* root, int key) {
    18. if (root == NULL) return getNewNode(key);
    19. if (rand() % 2) root->lchild = insert(root->lchild, key);
    20. else root->rchild = insert(root->rchild, key);
    21. return root;
    22. }
    23. Node* getRandomBinaryTree(int n) {
    24. Node* root = NULL;
    25. for (int i = 0; i < n; i++) root = insert(root, rand() & 100);
    26. return root;
    27. }
    28. void clear(Node* p) {
    29. if (p == NULL) return;
    30. clear(p->lchild);
    31. clear(p->rchild);
    32. free(p);
    33. return;
    34. }
    35. //二叉树转广义表
    36. char str[1000]; //广义表信息存到str中
    37. int len = 0;
    38. void __serialize(Node* root) {
    39. if (root == NULL) return;
    40. len += sprintf(str + len, "%d", root->key);
    41. if (!root->lchild && !root->rchild) return;
    42. len += sprintf(str + len, "(");
    43. __serialize(root->lchild);
    44. if (root->rchild) len += sprintf(str + len, ",");
    45. __serialize(root->rchild);
    46. len += sprintf(str + len, ")");
    47. return;
    48. }
    49. void serialize(Node* root) {
    50. memset(str, 0, sizeof(str));
    51. len = 0;
    52. __serialize(root);
    53. return;
    54. }
    55. void output(Node* root) {
    56. if (root == NULL) return;
    57. printf("%d(%d, %d)\n",
    58. root->key,
    59. root->lchild ? root->lchild->key : -1,
    60. root->rchild ? root->rchild->key : -1);
    61. output(root->lchild);
    62. output(root->rchild);
    63. return;
    64. }
    65. //广义表转二叉树
    66. Node* deserialize(char* str, int n) {
    67. Node** sta = (Node**)malloc(sizeof(Node*) * MAX);
    68. int top = -1, flag = 0, scode = 0;
    69. //flag表示当前节点该连在栈顶结点的左边还是右边
    70. //scode为状态码,0:任务分发 遇到关键字赋为1 遇到左括号赋为2 遇到逗号赋为3 遇到右括号赋为4
    71. Node* p = NULL, *root = NULL;
    72. //p指针指向当前要处理的结点
    73. //root永远指向最后出栈的元素
    74. for (int i = 0; str[i]; i++) {
    75. switch (scode){
    76. case 0: {
    77. //做任务分发
    78. if (str[i] >= '0' && str[i] <= '9') scode = 1;
    79. else if (str[i] == '(') scode = 2;
    80. else if (str[i] == ',') scode = 3;
    81. else scode = 4;
    82. //分发了任务,没有处理当前字符,为了抵消for循环中的i++
    83. i -= 1;
    84. }break;
    85. case 1: {
    86. //当前位置指向关键字,不考虑负数
    87. int key = 0;
    88. while (str[i] <= '9' && str[i] >= '0') {
    89. key = key * 10 + (str[i] - '0');
    90. i++;
    91. }
    92. p = getNewNode(key);
    93. //flag为1,连在栈顶结点的左边;flag为2,连在栈顶结点的右边
    94. if (top >= 0 && flag == 0) sta[top]->lchild = p;
    95. if (top >= 0 && flag == 1) sta[top]->rchild = p;
    96. //读入关键字后i指向关键字下一位,for循环后i会加1,那么关键字下一位就没有被处理
    97. //所以i减一
    98. scode = 0, i--;
    99. }break;
    100. case 2: {
    101. //左括号,说明p结点有子结点,则p入栈
    102. sta[++top] = p;
    103. flag = 0;
    104. scode = 0;
    105. } break;
    106. case 3: {
    107. //逗号,说明下一节点为栈顶结点的右结点
    108. flag = 1;
    109. scode = 0;
    110. } break;
    111. case 4: {
    112. //右括号,说明栈顶结点没有子节点了,出栈
    113. root = sta[top--];
    114. scode = 0;
    115. } break;
    116. }
    117. }
    118. return root;
    119. }
    120. int main() {
    121. srand(time(0));
    122. //建立二叉树并输出
    123. Node* root = getRandomBinaryTree(MAX);
    124. output(root); printf("\n");
    125. //序列化并输出
    126. serialize(root);
    127. printf("str : %s\n\n", str);
    128. //反序列化并输出
    129. Node* new_root = deserialize(str, len);
    130. output(new_root);
    131. return 0;
    132. }

  • 相关阅读:
    【python进阶】装饰器
    什么是MDM
    恒生电子:护航金融行业自主创新
    Jenkinsfile+Dockerfile前端vue自动化部署
    在spring中使用RequestBodyAdvice 和 ResponseBodyAdvice增强器
    Logo设计教程:从入门到精通的全程指导
    [架构之路-240]:目标系统 - 纵向分层 - 应用层 - 应用层协议与业务应用程序的多样化,与大自然生物的丰富多彩,异曲同工
    nrf52832 开发板入手笔记:J-Flash 蓝牙协议栈烧写
    java-php-python-ssm抑抑心理交流平台计算机毕业设计
    广州蓝景分享—开发人员都知道的JavaScript技巧
  • 原文地址:https://blog.csdn.net/Mz_yuner/article/details/133389575