当前位置:   article > 正文

【数据结构】二叉树的链式存储结构(通过前序序列和中序序列构造二叉树_queue q;报错

queue q;报错

说明:需要分别输入要二叉树的前序序列和中序序列才能构建二叉树。如果构建失败,程序会报错。
在这里插入图片描述
比如我们给定一个二叉树,容易知道
前序序列为:GDAFEMHZ
中序序列为:ADEFGHMZ
程序运行结果:
在这里插入图片描述
源代码

  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<string>
  4. #include<math.h>
  5. #include<queue>
  6. #define TURE 1
  7. #define ERROR 0
  8. #define N 100
  9. using namespace std;
  10. typedef int Status;
  11. typedef char ElemType;
  12. /*定义二叉树的存储类型*/
  13. typedef struct BitNode
  14. {
  15. ElemType data; //结点元素
  16. struct BitNode* lchild; // 左孩子指针
  17. struct BitNode* rchild; //右孩子指针
  18. } BitNode,*BiTree;
  19. void InitBiTree(BiTree* T)
  20. {
  21. *T = NULL;
  22. }
  23. void ClearBiTree(BiTree* T)
  24. { //清空二叉树
  25. if(*T)
  26. {
  27. if((*T)->lchild)
  28. ClearBiTree(&((*T)->lchild));
  29. if((*T)->rchild)
  30. ClearBiTree(&((*T)->rchild));
  31. free(*T);
  32. *T=NULL;
  33. }
  34. }
  35. Status BiTreeEmpty(BiTree T)
  36. {
  37. return T==NULL?TURE:ERROR;
  38. }
  39. //由前序序列和中序序列建立二叉树的过程
  40. /* 算法
  41. 1、通过先序遍历找到根结点A,再通过A在中序遍历的位置找出左子树,右子树
  42. 2、在A的左子树中,找左子树的根结点(在先序中找),重新开始步骤1
  43. 3、在A的右子树中,找右子树的根结点(在先序中找),重新开始步骤1
  44. */
  45. //根据先序遍历和中序遍历创建二叉树
  46. BiTree createBiTree(char *pre, char *in, int n)//pre存放前序序列,in存放中序序列,n为序列的长度
  47. {
  48. int i=0;
  49. int n1=0,n2=0;
  50. int m1=0,m2=0;
  51. BiTree node = NULL;
  52. char lchild_pre[N],rchild_pre[N] ;//lchild_pre[N] 存放前序序列中的左子树;rchild_pre[N]存放前序序列中的右子树
  53. char lchild_in[N],rchild_in[N]; //lchild_in[N]存放中序序列中的左子树;rchild_in[N]存放中序序列中的右子树
  54. if(n==0)
  55. {
  56. return NULL;
  57. }
  58. node = (BiTree)malloc(sizeof(BitNode));
  59. if(node==NULL)
  60. {
  61. return NULL;
  62. }
  63. node->data = pre[0]; //前序序列的第一个元素一定是根节点
  64. for(i=0; i<n; i++)
  65. {
  66. //求前序序列中的左子树和右子树
  67. if((i<=n1)&&(in[i]!=pre[0]))
  68. {
  69. lchild_in[n1++]=in[i];
  70. }
  71. else if(in[i]!=pre[0])
  72. {
  73. rchild_in[n2++] = in[i];
  74. }
  75. }
  76. for(i=1; i<n; i++)
  77. {
  78. //求中序序列中的左子树和右子树
  79. if(i<(n1+1))
  80. {
  81. lchild_pre[m1++]=pre[i];
  82. }
  83. else
  84. {
  85. rchild_pre[m2++]=pre[i];
  86. }
  87. }
  88. //使用递归,分别插入左子树和右子树
  89. node->lchild =createBiTree(lchild_pre,lchild_in,n1);
  90. node->rchild =createBiTree(rchild_pre,rchild_in,n2);
  91. return node;
  92. }
  93. int HeightOfBiTree(BiTree root)
  94. { //求二叉树的高度
  95. int treeHeight = 0;
  96. if (root != NULL)
  97. {
  98. int leftHeight = HeightOfBiTree(root->lchild);
  99. int rightHeight = HeightOfBiTree(root->rchild);
  100. treeHeight = (leftHeight >= rightHeight)? (leftHeight + 1):(rightHeight + 1);
  101. }
  102. return treeHeight;
  103. }
  104. int WidthOfBiTree(BiTree root)
  105. { //求二叉树的宽度
  106. if(root==NULL)
  107. {
  108. return 0;
  109. }
  110. int width = 0;
  111. int maxWidth = 0;
  112. queue<BiTree > Q;
  113. BiTree p = NULL;
  114. Q.push(root);
  115. while(!Q.empty())
  116. {
  117. width = Q.size();
  118. if(maxWidth < width)
  119. {
  120. maxWidth = width;
  121. }
  122. for(int i=0; i<width; i++)
  123. {
  124. p = Q.front();
  125. Q.pop();
  126. if(p->lchild)
  127. {
  128. Q.push(p->lchild);
  129. }
  130. if(p->rchild)
  131. {
  132. Q.push(p->rchild);
  133. }
  134. }
  135. }
  136. return maxWidth;
  137. }
  138. int NodeCount(BiTree root)
  139. { //求二叉树的结点数量
  140. if(root==NULL)
  141. return 0;
  142. else
  143. return NodeCount(root->lchild)+NodeCount(root->rchild)+1;
  144. }
  145. int LeafNodeCount(BiTree root){
  146. //求二叉树的叶子结点个数
  147. if(root==NULL)
  148. return 0;
  149. if(root->lchild==NULL&&root->rchild==NULL)
  150. return 1;
  151. return LeafNodeCount(root->lchild)+LeafNodeCount(root->rchild);
  152. }
  153. void preOrder(BiTree root)
  154. { //二叉树的前序遍历
  155. if (root==NULL)
  156. {
  157. return;
  158. }
  159. printf("%c ",root->data);
  160. preOrder(root->lchild);
  161. preOrder(root->rchild);
  162. }
  163. void inOrder(BiTree root)
  164. { //二叉树的中序遍历
  165. if (root==NULL)
  166. {
  167. return;
  168. }
  169. inOrder(root->lchild);
  170. printf("%c ",root->data);
  171. inOrder(root->rchild);
  172. }
  173. void PostOrder(BiTree root)
  174. { //后序遍历
  175. if (root==NULL)
  176. {
  177. return;
  178. }
  179. PostOrder(root->lchild);
  180. PostOrder(root->rchild);
  181. printf("%c ",root->data);
  182. }
  183. int main()
  184. {
  185. char preNode[N];
  186. char inNode[N];
  187. int n = 0;
  188. char ch;
  189. BitNode* root=NULL;
  190. printf("请输入二叉树的先序序列\n");
  191. while((ch = getchar())&&ch!='\n')
  192. preNode[n++] = ch;
  193. printf("请输入二叉树的中序序列\n");
  194. n = 0;
  195. while((ch = getchar())&&ch!='\n')
  196. inNode[n++] = ch;
  197. root = createBiTree(preNode,inNode,n);
  198. printf("二叉树创建成功\n");
  199. printf("先序序列\n");
  200. preOrder(root);
  201. printf("\n中序序列\n");
  202. inOrder(root);
  203. printf("\n后序序列\n");
  204. PostOrder(root);
  205. printf("\n");
  206. int height =HeightOfBiTree(root);
  207. printf("\n二叉树的高度为:%d\n",height);
  208. int width =WidthOfBiTree(root);
  209. printf("\n二叉树的宽度为:%d\n",width);
  210. int Node =NodeCount(root);
  211. printf("\n二叉树的结点数量为:%d\n",Node);
  212. int leaf =LeafNodeCount(root);
  213. printf("\n二叉树的叶子结点为:%d\n",leaf);
  214. system("pause");
  215. return 0;
  216. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/754758
推荐阅读
相关标签
  

闽ICP备14008679号