当前位置:   article > 正文

数据结构--二叉树--节点的修改(链式结构--队列)_二叉树结点data数据的修改

二叉树结点data数据的修改

建一个队列,将二叉树的每个 节点入队列并判断处理 

 

  1. #define CHAR /* 字符型 */
  2. #include<stdio.h> /* EOF(=^Z或F6),NULL */
  3. #include<math.h> /* floor(),ceil(),abs() */
  4. #include <stdlib.h>
  5. #define TRUE 1
  6. #define FALSE 0
  7. #define OK 1
  8. #define ERROR 0
  9. typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
  10. #ifdef CHAR
  11. typedef char TElemType;
  12. TElemType Nil=' '; /* 字符型以空格符为空 */
  13. #endif
  14. #ifdef INT
  15. typedef int TElemType;
  16. TElemType Nil=0; /* 整型以0为空 */
  17. #endif
  18. typedef struct BiTNode
  19. {
  20. TElemType data;
  21. struct BiTNode *lchild,*rchild; /* 左右孩子指针 */
  22. }BiTNode,*BiTree;
  23. typedef BiTree QElemType; /* 设队列元素为二叉树的指针类型 */
  24. typedef struct QNode //队列节点
  25. {
  26. QElemType data;
  27. struct QNode *next;
  28. }QNode,*QueuePtr;
  29. typedef struct
  30. {
  31. QueuePtr front,rear; /* 队头、队尾指针 */
  32. }LinkQueue;
  33. Status InitQueue(LinkQueue &Q);
  34. Status QueueEmpty(LinkQueue &Q);
  35. Status EnQueue(LinkQueue &Q,QElemType e);
  36. Status DeQueue(LinkQueue &Q,QElemType &e);
  37. Status InitBiTree(BiTree &T)
  38. { /* 操作结果: 构造空二叉树T */
  39. T=NULL;
  40. return OK;
  41. }
  42. void CreateBiTree(BiTree &T)
  43. {//按先序次序输入二叉树中结点的值(一个字符),空格字符表示空树,构造二叉链表表示的二叉树T。
  44. TElemType ch;
  45. #ifdef CHAR
  46. scanf("%c",&ch);
  47. #endif
  48. #ifdef INT
  49. scanf("%d",&ch);
  50. #endif
  51. if(ch==Nil) /* 空 */
  52. T=NULL;
  53. else
  54. {
  55. T=(BiTree)malloc(sizeof(BiTNode));
  56. if(!T)
  57. exit(OVERFLOW);
  58. T->data=ch; /* 生成根结点 */
  59. CreateBiTree(T->lchild); /* 构造左子树 */
  60. CreateBiTree(T->rchild); /* 构造右子树 */
  61. }
  62. }
  63. TElemType Value(BiTree p)
  64. { /* 初始条件: 二叉树T存在,p指向T中某个结点 */
  65. /* 操作结果: 返回p所指结点的值 */
  66. return p->data;
  67. }
  68. void Assign(BiTree &p,TElemType value)
  69. { /* 给p所指结点赋值为value */
  70. p->data=value;
  71. }
  72. BiTree Point(BiTree T,TElemType s)
  73. { /* 返回二叉树T中指向元素值为s的结点的指针。另加 */
  74. LinkQueue q;
  75. QElemType a;
  76. if(T) /* 非空树 */
  77. {
  78. InitQueue(q); /* 初始化队列 */
  79. EnQueue(q,T); /* 根结点入队 */
  80. while(!QueueEmpty(q)) /* 队不空 */
  81. {
  82. DeQueue(q,a); /* 出队,队列元素赋给a */
  83. if(a->data==s)
  84. return a;
  85. if(a->lchild) /* 有左孩子 */
  86. EnQueue(q,a->lchild); /* 入队左孩子 */
  87. if(a->rchild) /* 有右孩子 */
  88. EnQueue(q,a->rchild); /* 入队右孩子 */
  89. }
  90. }
  91. return NULL;
  92. }
  93. Status InitQueue(LinkQueue &Q)
  94. { /* 构造一个空队列Q */
  95. Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
  96. if(!Q.front)
  97. exit(OVERFLOW);
  98. Q.front->next=NULL;
  99. return OK;
  100. }
  101. Status QueueEmpty(LinkQueue &Q)
  102. { /* 若Q为空队列,则返回TRUE,否则返回FALSE */
  103. if(Q.front==Q.rear)
  104. return TRUE;
  105. else
  106. return FALSE;
  107. }
  108. Status EnQueue(LinkQueue &Q,QElemType e)
  109. { /* 插入元素e为Q的新的队尾元素 */
  110. QueuePtr p=(QueuePtr)malloc(sizeof(QNode));
  111. if(!p) /* 存储分配失败 */
  112. exit(OVERFLOW);
  113. p->data=e;
  114. p->next=NULL;
  115. Q.rear->next=p;
  116. Q.rear=p;
  117. return OK;
  118. }
  119. Status DeQueue(LinkQueue &Q,QElemType &e)
  120. { /* 若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR */
  121. QueuePtr p;
  122. if(Q.front==Q.rear)
  123. return ERROR;
  124. p=Q.front->next;
  125. e=p->data;
  126. Q.front->next=p->next;
  127. if(Q.rear==p)//说明队列为空
  128. Q.rear=Q.front;
  129. free(p);
  130. return OK;
  131. }
  132. void LevelOrderTraverse(BiTree T,Status(*Visit)(TElemType))
  133. { /* 初始条件:二叉树T存在,Visit是对结点操作的应用函数 */
  134. /* 操作结果:层序递归遍历T(利用队列),对每个结点调用函数Visit一次且仅一次 */
  135. LinkQueue q;
  136. QElemType a;
  137. if(T)
  138. {
  139. InitQueue(q);
  140. EnQueue(q,T);
  141. while(!QueueEmpty(q))
  142. {
  143. DeQueue(q,a);
  144. Visit(a->data);
  145. if(a->lchild!=NULL)
  146. EnQueue(q,a->lchild);
  147. if(a->rchild!=NULL)
  148. EnQueue(q,a->rchild);
  149. }
  150. printf("\n");
  151. }
  152. }
  153. Status visitT(TElemType e)
  154. {
  155. #ifdef CHAR
  156. printf("%c ",e);
  157. #endif
  158. #ifdef INT
  159. printf("%d ",e);
  160. #endif
  161. return OK;
  162. }
  163. void main()
  164. {
  165. BiTree T,p;
  166. TElemType e1,e2;
  167. InitBiTree(T);
  168. #ifdef CHAR
  169. printf("请先序输入二叉树(如:ab三个空格表示a为根结点,b为左子树的二叉树)\n");
  170. #endif
  171. #ifdef INT
  172. printf("请先序输入二叉树(如:1 2 0 0 0表示1为根结点,2为左子树的二叉树)\n");
  173. #endif
  174. CreateBiTree(T);
  175. printf("请输入一个结点的值: ");
  176. #ifdef CHAR
  177. scanf("%*c%c",&e1);
  178. #endif
  179. #ifdef INT
  180. scanf("%d",&e1);
  181. #endif
  182. p=Point(T,e1); /* p为e1的指针 */
  183. #ifdef CHAR
  184. printf("结点的值为%c\n",Value(p));
  185. #endif
  186. #ifdef INT
  187. printf("结点的值为%d\n",Value(p));
  188. #endif
  189. printf("欲改变此结点的值,请输入新值: ");
  190. #ifdef CHAR
  191. scanf("%*c%c%*c",&e2);
  192. #endif
  193. #ifdef INT
  194. scanf("%d",&e2);
  195. #endif
  196. Assign(p,e2);
  197. printf("层次遍历二叉树:\n");
  198. LevelOrderTraverse(T,visitT);
  199. }



 

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

闽ICP备14008679号