码迷,mamicode.com
首页 > 其他好文 > 详细

03-树2 List Leaves

时间:2018-11-13 23:58:39      阅读:314      评论:0      收藏:0      [点我收藏+]

标签:span   number   oid   har   create   div   很多   print   def   

  这道题目需要按要求由上至下、从左到右 输出 叶节点(层序遍历) 。根据输入的数据构建好二叉树并返回根节点, 再利用循环队列层序遍历二叉树,同时将叶节点以链式结构进行存储, 最后以链表的形式输出。

 

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 
  4 #define null -1
  5 #define MaxTreeNode 10
  6 
  7 typedef int Tree;
  8 struct TreeNode {
  9     Tree Self;
 10     Tree Left;
 11     Tree Right;
 12 } T[MaxTreeNode];
 13 
 14 typedef int Position;
 15 struct QNode {
 16     struct TreeNode T1[MaxTreeNode];    // 存储元素 
 17     Position Front, Rear;                // 队列的 头 和 尾 
 18     int MaxSize;                        // 最大容量 
 19 };
 20 typedef struct QNode *Queue; 
 21 
 22 typedef struct node *Node;
 23 struct node {
 24     int value;
 25     Node next;
 26 };
 27 typedef Node List;
 28 
 29 Tree BulidTree(int N);
 30 Queue CreateQueue();
 31 int IsFull(Queue Q);
 32 int IsEmpty(Queue Q);
 33 void AddQ(Queue Q, Tree NodeIndex);
 34 Position DeleteQ(Queue Q);
 35 List FindLeaf(Tree Root, int N);
 36 void Print(List L);
37 int main() 38 { 39 int N; // the numbers of node 40 scanf("%d\n", &N); 41 42 Tree root; 43 root = BulidTree(N); 44 45 List L; 46 L = FindLeaf(root, N); 47 48 Print(L); 49 50 return 0; 51 } 52 53 Queue CreateQueue() 54 { 55 Queue Q = (Queue)malloc(sizeof(struct QNode)); 56 Q->Front = Q->Rear = 0; 57 Q->MaxSize = MaxTreeNode; 58 59 return Q; 60 } 61 62 int IsFull(Queue Q) 63 { 64 return (Q->Rear+1)%Q->MaxSize == Q->Front; 65 } 66 67 void AddQ(Queue Q, Tree NodeIndex) 68 { 69 if ( IsFull(Q) ) 70 return; // queue is fulled 71 else { 72 Q->Rear = (Q->Rear+1)%Q->MaxSize; 73 Q->T1[Q->Rear] = T[NodeIndex]; 74 } 75 } 76 77 int IsEmpty(Queue Q) 78 { 79 return (Q->Front == Q->Rear); 80 } 81 82 Position DeleteQ(Queue Q) 83 { 84 if ( IsEmpty(Q) ) 85 return -1; 86 else { 87 Q->Front = (Q->Front+1)%Q->MaxSize; 88 return Q->Front; 89 } 90 } 91 92 List FindLeaf(Tree Root, int N) 93 { 94 if ( Root == null || !N ) return NULL; 95 List r, p, t; 96 p = (List)malloc(sizeof(struct node)); p->next = NULL; 97 r = p; 98 99 Queue Q; 100 Q = CreateQueue(); // create empty queue 101 AddQ(Q, Root); 102 103 Position front; 104 while ( !IsEmpty(Q) ) { 105 front = DeleteQ(Q); 106 if ( Q->T1[front].Left == null && Q->T1[front].Right == null ) { 107 t = (List)malloc(sizeof(struct node)); t->next = NULL; 108 t->value = Q->T1[front].Self; 109 r->next = t; r = t; 110 } else { 111 if ( Q->T1[front].Left != null ) AddQ(Q, Q->T1[front].Left); 112 if ( Q->T1[front].Right != null ) AddQ(Q, Q->T1[front].Right); 113 } 114 } 115 t = p; p = p->next; free(t); 116 117 return p; 118 } 119 120 void Print(List L) 121 { 122 if ( !L ) return; 123 int flag = 1; 124 125 while ( L ) { 126 if ( flag ) 127 flag = 0; 128 else 129 printf(" "); 130 printf("%d", L->value); 131 132 L = L->next; 133 } 134 } 135 136 Tree BulidTree(int N) 137 { 138 Tree Root = null; 139 int check[N]; 140 char cl, cr; 141 int i; 142 if ( N ) { 143 for ( i = 0; i < N; i++ ) check[i] = 0; 144 for ( i = 0; i < N; i++ ) { 145 scanf("%c %c\n", &cl, &cr); 146 T[i].Self = i; 147 // left 148 if ( cl != - ) { 149 T[i].Left = cl - 0; 150 check[T[i].Left] = 1; 151 } else { 152 T[i].Left = null; 153 } 154 // right 155 if ( cr != - ) { 156 T[i].Right = cr - 0; 157 check[T[i].Right] = 1; 158 } else { 159 T[i].Right = null; 160 } 161 } 162 for ( i = 0; i < N; i++ ) 163 if ( !check[i] ) break; 164 Root = i; 165 } 166 167 return Root; 168 }

 

  整个过程犯了很多不该犯的错误,嗯,程序可以运行就是得不到正确的输出,感觉不出那里出错了,没办法了,只好去调试,结果都是 if 语句的条件搞反了, en,  引以为戒。

 

03-树2 List Leaves

标签:span   number   oid   har   create   div   很多   print   def   

原文地址:https://www.cnblogs.com/wgxi/p/9955528.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!