不疯魔,不成活!——二叉树的创建、遍历(递归实现)等操作。


无论是大学计算机相关专业的考试,亦或是各种大小IT公司的笔试面试,

只要考C语言(或者C++),

有三点是永远的主题——指针,链表,二叉树。

今天写下这篇博客,初步研究一下二叉树。












1
/** 2 * 二叉树的创建,三种遍历(先、中、后序) 3 **/ 4 5 #include <stdio.h> 6 #include <stdlib.h> 7 8 typedef struct tree 9 { 10 char data; 11 struct tree *l_child; 12 struct tree *r_child; 13 }*Ptree, Dtree; 14 15 //先序创建二叉树 16 Ptree createTree() 17 { 18 char ch; 19 Ptree t; 20 ch = getchar(); 21 if(ch =='#') 22 t = NULL; 23 else{ 24 t = (Ptree)malloc(sizeof(Dtree)); 25 t->data = ch; 26 t->l_child = creatTree();//按照二叉树的递归定义,构建左、右子树 27 t->r_child = creatTree(); 28 } 29 return t; 30 } 31 32 //定义队列 33 typedef struct queue_tree 34 { 35 Ptree data[100]; 36 int front,; 37 int rear; 38 }*Pqueue, Dqueue; 39 40 //初始化队列 41 Pqueue initQueue() 42 { 43 Pqueue p; 44 p = (Pqueue)malloc(sizeof(Dqueue)); 45 if(p){ 46 p->front = 0; 47 p->rear = 0; 48 } 49 return p; 50 } 51 52 //判断队列是否为空 53 int emptyQueue(Pqueue p) 54 { 55 if(p->front == p->rear) 56 return 1; 57 else 58 return 0; 59 } 60 61 //入队列 62 void inQueue(Pqueue p, Ptree t) 63 { 64 p->rear = (p->rear+1)%100; 65 p->data[p->rear] = t; 66 } 67 68 //出队列 69 void outQueue(Pqueue, Ptree *t) 70 { 71 p->front = (p->front+1)%100; 72 *t = p->data[p->front]; 73 } 74 75 //层次遍历 76 void levelOrder(Ptree t) 77 { 78 Ptree p = t; 79 Pqueue Q; 80 if(p){ 81 Q = initQueue(); 82 printf("%c", p->data); 83 inQueu(Q, &p); 84 while(!emptyQueue(Q)){ 85 outQueue(Q, &p); 86 if(p->l_child){ 87 printf("%c", p->l_child->data); 88 inQueue(Q, p->l_child); 89 } 90 if(p->r_child){ 91 printf("%c", p->r_child->data); 92 inQueue(Q, p->r_child); 93 } 94 } 95 } 96 } 97 98 //先序遍历二叉树,递归实现 99 void preOrder(Ptree t) 100 { 101 if(t){ 102 printf("%c", t->data); 103 preOrder(t->l_child); 104 preOrder(t->r_child); 105 } 106 } 107 108 //中序遍历 109 void midOrder(Ptree t) 110 { 111 if(t){ 112 midOrder(t->l_child); 113 printf("%c", t->data); 114 midOrder(t->r_child); 115 } 116 } 117 118 //后序遍历 119 void posOrder(Ptree t) 120 { 121 if(t){ 122 posOrder(t->l_child); 123 posOrder(t->r_child); 124 printf("%c", t->data); 125 } 126 } 127 128 //求二叉树的叶子数 129 int leafCount(Ptree t) 130 { 131 int count1,count2; 132 if(t == NULL) 133 return 0; 134 else{ 135 if(t->l_child==NULL && t->r_child==NULL) 136 return 1; 137 else{ 138 count1 = leafCount(t->l_child); 139 count2 = leafCount(t->r_child); 140 return count1+count2; 141 } 142 } 143 } 144 145 //求二叉树每层节点的个数,保存到数组num中 146 void levelCount(Ptree t, int n, int num[]) 147 { 148 if(t){ 149 num[n]++; 150 levelCount(t->l_child, n+1, num); 151 levleCount(t->r_child, n+1, num); 152 } 153 } 154 155 //求二叉树的高度 156 int heightTree(Ptree t) 157 { 158 int h1, h2; 159 if(t == NULL) 160 return 0; 161 else{ 162 h1 = heightTree(t->l_child); 163 h2 = heightTree(t->r_child); 164 h1>h2 ? (h1+1) : (h2+1); 165 } 166 } 167 168 //主函数 169 int main(int argc, char *argv[]) 170 { 171 int num[10] = {0}; 172 int height; 173 int i; 174 Ptree t; 175 176 t = creatTree(); 177 178 printf("先序遍历:\n"); 179 preOrder(t); 180 printf("\n"); 181 182 printf("中序遍历:\n"); 183 midOrder(t); 184 printf("\n"); 185 186 printf("后序遍历:\n"); 187 posOrder(t); 188 printf("\n"); 189 190 height = heightTree(t); 191 printf("二叉树的深度:%d\n", height); 192 193 levelCount(t, 1, num); 194 for(i=1; i<=height; i++) 195 printf("第%d层节点个数:%d\n", i, num[i]); 196 197 printf("二叉树的叶子总数:%d\n", leafCount(t)); 198 199 return 0; 200 }

 

posted @ 2012-06-21 19:00  HandsomeDragon  阅读(252)  评论(0)    收藏  举报