• <二叉树及模拟实现>——《Data Structure in C Train》


    目录

    详细实现链接:

    <二叉树>《数据结构(C语言版)》

    <二叉树(链式)>《数据结构(C语言版)》

     1.分析实现功能,感受二叉树的结构:

    2.完整源码:

    BinaryTree:

    复用的Queue.h:

    复用的Queue.c:


    详细实现链接:

    <二叉树>《数据结构(C语言版)》

    <二叉树(链式)>《数据结构(C语言版)》

     1.分析实现功能,感受二叉树的结构:

    结构设计注意问题:

    功能函数:

    层序遍历复用的查找问题:

     

     

    2.完整源码:

    BinaryTree:

    1. #include
    2. #include
    3. #include"Queue.h"
    4. typedef char BTDataType;
    5. typedef struct BinaryTreeNode
    6. {
    7. struct BinaryTreeNode* left;
    8. struct BinaryTreeNode* right;
    9. BTDataType data;
    10. }BTNode;
    11. //前序遍历
    12. void PrevOrder(BTNode* root)
    13. {
    14. if (root == NULL)
    15. {
    16. printf("NULL ");
    17. return;
    18. }
    19. printf("%c ", root->data);
    20. PrevOrder(root->left);
    21. PrevOrder(root->right);
    22. }
    23. //中序遍历
    24. void InOrder(BTNode* root)
    25. {
    26. if (root == NULL)
    27. {
    28. printf("NULL ");
    29. return;
    30. }
    31. InOrder(root->left);
    32. printf("%c ", root->data);
    33. InOrder(root->right);
    34. }
    35. //后序遍历
    36. void PostOrder(BTNode* root)
    37. {
    38. if (root == NULL)
    39. {
    40. printf("NULL ");
    41. return;
    42. }
    43. PostOrder(root->left);
    44. PostOrder(root->right);
    45. printf("%c ", root->data);
    46. }
    47. //int size = 0;
    48. //void TreeSize(BTNode* root)
    49. //{
    50. // if (root == NULL)
    51. // {
    52. // return;
    53. // }
    54. // else
    55. // {
    56. // ++size;
    57. // }
    58. //
    59. // TreeSize(root->left);
    60. // TreeSize(root->right);
    61. //}
    62. //void TreeSize(BTNode* root, int* psize)
    63. //{
    64. // if (root == NULL)
    65. // {
    66. // return;
    67. // }
    68. // else
    69. // {
    70. // ++(*psize);
    71. // }
    72. //
    73. // TreeSize(root->left, psize);
    74. // TreeSize(root->right, psize);
    75. //}
    76. //采用分治思想
    77. int TreeSize(BTNode* root)
    78. {
    79. return root == NULL ? 0 : TreeSize(root->left) + TreeSize(root->right) + 1;
    80. }
    81. // 叶子节点的个数
    82. int TreeLeafSize(BTNode* root)
    83. {
    84. //1.0个
    85. //2.1个
    86. //3.一个以上
    87. if (root == NULL)
    88. return 0;
    89. if (root->left == NULL && root->right == NULL)
    90. return 1;
    91. return TreeLeafSize(root->left)
    92. + TreeLeafSize(root->right);
    93. }
    94. //层序遍历
    95. void LevelOrder(BTNode* root)
    96. {
    97. // 核心思路:上一层出的时候带下一层节点进
    98. Queue q;
    99. QueueInit(&q);
    100. if (root)
    101. QueuePush(&q, root);
    102. while (!QueueEmpty(&q))
    103. {
    104. BTNode* front = QueueFront(&q);
    105. QueuePop(&q);
    106. printf("%c ", front->data);
    107. if (front->left)
    108. {
    109. QueuePush(&q, front->left);
    110. }
    111. if (front->right)
    112. {
    113. QueuePush(&q, front->right);
    114. }
    115. }
    116. printf("\n");
    117. QueueDestory(&q);
    118. }
    119. int main()
    120. {
    121. BTNode* A = (BTNode*)malloc(sizeof(BTNode));
    122. A->data = 'A';
    123. A->left = NULL;
    124. A->right = NULL;
    125. BTNode* B = (BTNode*)malloc(sizeof(BTNode));
    126. B->data = 'B';
    127. B->left = NULL;
    128. B->right = NULL;
    129. BTNode* C = (BTNode*)malloc(sizeof(BTNode));
    130. C->data = 'C';
    131. C->left = NULL;
    132. C->right = NULL;
    133. BTNode* D = (BTNode*)malloc(sizeof(BTNode));
    134. D->data = 'D';
    135. D->left = NULL;
    136. D->right = NULL;
    137. BTNode* E = (BTNode*)malloc(sizeof(BTNode));
    138. E->data = 'E';
    139. E->left = NULL;
    140. E->right = NULL;
    141. A->left = B;
    142. A->right = C;
    143. B->left = D;
    144. B->right = E;
    145. PrevOrder(A);
    146. printf("\n");
    147. InOrder(A);
    148. printf("\n");
    149. PostOrder(A);
    150. printf("\n");
    151. /*TreeSize(A);
    152. printf("TreeSize:%d\n", size);
    153. size = 0;
    154. TreeSize(B);
    155. printf("TreeSize:%d\n", size);*/
    156. /*int Asize = 0;
    157. TreeSize(A, &Asize);
    158. printf("TreeSize:%d\n", Asize);
    159. int Bsize = 0;
    160. TreeSize(B, &Bsize);
    161. printf("TreeSize:%d\n", Bsize);*/
    162. printf("TreeSize:%d\n", TreeSize(A));
    163. printf("TreeSize:%d\n", TreeSize(B));
    164. LevelOrder(A);
    165. return 0;
    166. }

    复用的Queue.h:

    1. #pragma once
    2. #include
    3. #include
    4. #include
    5. #include
    6. //前置声明
    7. struct BinaryTreeNode; //在Queue.c展开时,可以在这里指定,防止找不到
    8. //typedef int QDataType;
    9. typedef struct BinaryTreeNode* QDataType; //头文件展开时向上找
    10. typedef struct QueueNode
    11. {
    12. struct QueueNode* next;
    13. QDataType data;
    14. }QNode;
    15. typedef struct Queue
    16. {
    17. QNode* head;
    18. QNode* tail;
    19. }Queue;
    20. //初始化
    21. void QueueInit(Queue* pq);
    22. //销毁
    23. void QueueDestory(Queue* pq);
    24. // 队尾入
    25. void QueuePush(Queue* pq, QDataType x);
    26. // 队头出
    27. void QueuePop(Queue* pq);
    28. //取队列头元素
    29. QDataType QueueFront(Queue* pq);
    30. //取队列尾元素
    31. QDataType QueueBack(Queue* pq);
    32. //队列大小
    33. int QueueSize(Queue* pq);
    34. //队列判空
    35. bool QueueEmpty(Queue* pq);

    复用的Queue.c:

    1. #include"Queue.h"
    2. //初始化
    3. void QueueInit(Queue* pq)
    4. {
    5. assert(pq);
    6. pq->head = pq->tail = NULL;
    7. }
    8. //销毁
    9. void QueueDestory(Queue* pq)
    10. {
    11. assert(pq);
    12. QNode* cur = pq->head;
    13. while (cur)
    14. {
    15. QNode* next = cur->next;
    16. free(cur);
    17. cur = next;
    18. }
    19. pq->head = pq->tail = NULL;
    20. }
    21. // 队尾入
    22. void QueuePush(Queue* pq, QDataType x)
    23. {
    24. assert(pq);
    25. QNode* newnode = (QNode*)malloc(sizeof(QNode));
    26. if (newnode == NULL)
    27. {
    28. printf("malloc fail!\n");
    29. exit(-1);
    30. }
    31. newnode->data = x;
    32. newnode->next = NULL;
    33. if (pq->tail == NULL)
    34. {
    35. pq->head = pq->tail = newnode;
    36. }
    37. else
    38. {
    39. pq->tail->next = newnode;
    40. pq->tail = newnode;
    41. }
    42. }
    43. // 队头出
    44. void QueuePop(Queue* pq)
    45. {
    46. assert(pq);
    47. assert(pq->head);
    48. // 1、一个
    49. // 2、多个
    50. if (pq->head->next == NULL)
    51. {
    52. free(pq->head);
    53. pq->head = pq->tail = NULL;
    54. }
    55. else
    56. {
    57. QNode* next = pq->head->next;
    58. free(pq->head);
    59. pq->head = next;
    60. }
    61. }
    62. //取队头元素
    63. QDataType QueueFront(Queue* pq)
    64. {
    65. assert(pq);
    66. assert(pq->head);
    67. return pq->head->data;
    68. }
    69. //取队尾元素
    70. QDataType QueueBack(Queue* pq)
    71. {
    72. assert(pq);
    73. assert(pq->head);
    74. return pq->tail->data;
    75. }
    76. //队列大小
    77. int QueueSize(Queue* pq)
    78. {
    79. assert(pq);
    80. int size = 0;
    81. QNode* cur = pq->head;
    82. while (cur)
    83. {
    84. ++size;
    85. cur = cur->next;
    86. }
    87. return size;
    88. }
    89. //队列判空
    90. bool QueueEmpty(Queue* pq)
    91. {
    92. assert(pq);
    93. return pq->head == NULL;
    94. }

  • 相关阅读:
    Android使用setXfermode例子
    TDengine3.0 基础操作
    docker安装nextcloud+onlyoffice+https
    使用adobe font style 工具绘制的艺术字,请鉴赏。
    【漏洞复现】CRMEB开源商城v5.2.2——ProductController.php——SQL注入
    英语语法汇总(10.直接引语和间接引语)
    java中常见的设计模式
    3.rsync备份案例
    Linux中for循环
    多线程会遇到的问题与解决方法
  • 原文地址:https://blog.csdn.net/m0_57859086/article/details/126616045