【数据结构】链式二叉树
二叉树头文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 #ifndef __BINTREE_H 7 #define __BINTREE_H 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <stdbool.h> 12 13 14 typedef char datatype_tree; 15 16 typedef struct tree 17 { 18 datatype_tree data_t; 19 struct tree *lchild,*rchild; 20 }bin_tree,*bin_ptree; 21 22 extern void bintree_create(bin_ptree *ptree);//create a binary tree 23 extern void pre_order(bin_ptree ptree);//iterate voer a binary tree,begin with root 24 extern void in_order(bin_ptree ptree);//iterate over a binary tree,begin with left child 25 extern void post_order(bin_ptree proot);//iterate over a binary tree,begin with right child 26 extern void level_order(bin_ptree proot); 27 extern void travel(char *str, void(*pfun)(bin_ptree),bin_ptree proot); 28 29 #endif
链表头文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 #ifndef __LINKQUEUE_H 7 #define __LINKQUEUE_H 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 #include <stdbool.h> 12 #include "bin_tree.h" 13 typedef bin_ptree datatype;//存储二叉树指针 14 15 typedef struct node 16 { 17 datatype data; 18 struct node *next; 19 }link_node,*link_pnode; 20 21 typedef struct queue 22 { 23 link_pnode front,rear; 24 }link_queue,*link_pqueue; 25 26 27 extern void linkqueue_init(link_pqueue *pqueue);//初始化链表队列 28 extern bool is_empty(link_pqueue pqueue); 29 extern bool in_linkqueue(link_pqueue pqueue, datatype data);//链表队列入队 30 extern bool out_linkqueue(link_pqueue pqueue, datatype *result);//链表队列出队 31 //extern void linkqueue_show(link_pqueue pqueue);//显示链表队列 32 33 #endif
二叉树文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 #include "bin_tree.h" 7 #include "linkqueue.h" 8 9 void bintree_create(bin_ptree *proot)//create a binary tree 10 { 11 datatype_tree ch; 12 13 scanf("%c",&ch); 14 if('#' != ch)//create 15 { 16 *proot = (bin_ptree)malloc(sizeof(bin_tree)); 17 if(NULL == *proot) 18 { 19 perror("malloc"); 20 exit(1); 21 } 22 (*proot)->data_t = ch; 23 bintree_create(&(*proot)->lchild); 24 bintree_create(&(*proot)->rchild); 25 } 26 else 27 *proot = NULL; 28 } 29 30 void pre_order(bin_ptree proot)//iterate over a binary tree,begin with root 31 { 32 if(NULL != proot) 33 { 34 printf("%c",proot->data_t); 35 pre_order(proot->lchild); 36 pre_order(proot->rchild); 37 } 38 } 39 40 void in_order(bin_ptree proot)//iterate over a binary tree,begin with left child 41 { 42 if(NULL != proot) 43 { 44 in_order(proot->lchild); 45 printf("%c",proot->data_t); 46 in_order(proot->rchild); 47 } 48 49 } 50 51 void post_order(bin_ptree proot)//iterate over a binary tree,begin with right child 52 { 53 if(NULL != proot) 54 { 55 post_order(proot->lchild); 56 post_order(proot->rchild); 57 printf("%c",proot->data_t); 58 } 59 60 } 61 62 63 void level_order(bin_ptree proot)//按层遍历 64 { 65 //需要借助队列 66 link_pqueue pqueue; 67 68 linkqueue_init(&pqueue);//初始化队列 69 70 //1.proot != NULL,打印proot->data_t,proot == NULL,出队,队列空结束 71 //2.proot->lchild != NULL,则入队 72 //3.proot->rchild != NULL,则入队 73 //4.队列不为空,出队,回到1,为空结束 74 75 while(NULL != proot)//proot不为空,则一值循环 76 { 77 printf("%c",proot->data_t); 78 79 if(NULL != proot->lchild) 80 in_linkqueue(pqueue,proot->lchild); 81 if(NULL != proot->rchild) 82 in_linkqueue(pqueue,proot->rchild); 83 if(is_empty(pqueue))//队列为空则结束 84 break; 85 else//不为空则出队 86 out_linkqueue(pqueue,&proot); 87 } 88 } 89 90 void travel(char *str, void(*pfun)(bin_ptree),bin_ptree proot)//将proot传给pfun函数处理 91 { 92 printf("%s\t",str); 93 pfun(proot);//将proot传给pfun函数处理 94 printf("\n"); 95 }
链表文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 //输入数字入栈 7 //输入字符出栈 8 9 #include "linkqueue.h" 10 11 void linkqueue_init(link_pqueue *pqueue)//初始化链表队列 12 { 13 link_pnode pnode; 14 15 *pqueue = (link_pqueue)malloc(sizeof(link_queue));//分配链表 16 if(NULL == *pqueue) 17 { 18 perror("malloc"); 19 exit(1); 20 } 21 22 pnode = (link_pnode)malloc(sizeof(link_node));//分配节点 23 if(NULL == pnode) 24 { 25 perror("malloc"); 26 exit(1); 27 } 28 29 pnode->next = NULL;//初始化pnode 30 //初始化pqueue 31 (*pqueue)->front = pnode; 32 (*pqueue)->rear = pnode; 33 } 34 35 bool is_empty(link_pqueue pqueue) 36 { 37 if(pqueue->front == pqueue->rear) 38 return true; 39 else 40 return false; 41 } 42 43 bool in_linkqueue(link_pqueue pqueue, datatype data)//链表队列入队 44 { 45 link_pnode new; 46 new = (link_pnode)malloc(sizeof(link_node));//分配结点 47 if(NULL == new) 48 return false; 49 else 50 { 51 new->data = data; 52 53 //将new结点插入到队列的末尾 54 new->next = pqueue->rear->next;//将尾结点的next赋值给new->next 55 pqueue->rear->next = new;//尾结点指向new,即new成为新的尾结点 56 pqueue->rear = new;//尾结点指向new 57 58 return true; 59 } 60 } 61 62 bool out_linkqueue(link_pqueue pqueue, datatype *result)//链表队列出队 63 { 64 link_pnode pnode;//临时指针,指向要出队的结点 65 66 if(is_empty(pqueue)) 67 return false; 68 else 69 { 70 pnode = pqueue->front;//头结点,将出队 71 pqueue->front = pnode->next;//头指针后移 72 //注意头指针指向的是头结点,第一个结点是pqueue->front,即出队结点 73 *result = pqueue->front->data; 74 free(pnode);//释放结点空间 75 76 return true; 77 } 78 } 79 80 #if 0 //链表中用不到 81 82 void linkqueue_show(link_pqueue pqueue)//显示链表队列 83 { 84 link_pnode pnode;//负责遍历过程中的节点移动 85 86 pnode = pqueue->front->next;//指向第一个结点 87 while(NULL != pnode) 88 { 89 printf("%d\t",pnode->data); 90 pnode = pnode->next; 91 } 92 printf("\n"); 93 } 94 #endif
主文件
1 //@ author 成鹏致远 2 //@ net http://infodown.tap.cn 3 //@ qq 552158509 4 //@ blog lcw.cnblogs.com 5 6 #include "bin_tree.h" 7 8 int main() 9 { 10 bin_ptree proot = NULL; 11 12 printf("Pls input data to create a binary tree \nsuch as ABC##DE#G##F###\n"); 13 bintree_create(&proot);//create binary tree 14 15 travel("Pre_order:",pre_order,proot); 16 travel("in_order:",in_order,proot); 17 travel("Post_order:",post_order,proot); 18 travel("level_order:",level_order,proot); 19 #if 0 20 pre_order(proot);//iterate,begin with root 21 printf("\n"); 22 23 in_order(proot);//iterate,begin with left child 24 printf("\n"); 25 26 post_order(proot);//iterate,begin with right child 27 printf("\n"); 28 29 level_order(proot); 30 printf("\n"); 31 #endif 32 return 0; 33 }
Makefile文件
1 binary_tree:main.c bin_tree.c linkqueue.c 2 gcc -o $@ $^ 3 clean: 4 $(RM) binary_tree .*.sw?