二叉树

  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <malloc.h>
  4 
  5 typedef struct BitTreeNode{
  6     int data;
  7     struct BitTreeNode* Lchild;
  8     struct BitTreeNode* Rchild;
  9 }BTNode;
 10 
 11 BTNode* CreateBitTree(int tdata[], int num);
 12 void PreOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode));
 13 void MidOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode));
 14 void PostOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode));
 15 void PrintData(BTNode* tnode);
 16 
 17 int main(){
 18     BTNode* troot;
 19     int tdata[12] = {123456789101112};
 20     troot = CreateBitTree(tdata, 3);
 21     if(troot == NULL){
 22         printf("Create binary tree failed.\n");
 23         exit(0);
 24     }
 25     printf("PreOrderTraverse:\n");
 26     PreOrderTraverse(troot, PrintData);
 27 
 28     printf("MidOrderTraverse:\n");
 29     MidOrderTraverse(troot, PrintData);
 30 
 31     printf("PostOrderTraverse:\n");
 32     PostOrderTraverse(troot, PrintData);
 33     return 0;
 34 }
 35 
 36 /*
 37 *@brief 二叉树的创建
 38 *
 39 *@param [in] tdata    树中结点的个数
 40 *       [in] num      数据个数
 41 *
 42 *@return 创建好的树的根结点指针
 43 */
 44 
 45 BTNode* CreateBitTree(int tdata[], int num){
 46     BTNode* tnode;
 47     if(num != 0){
 48         tnode = (BTNode*)malloc(sizeof(BTNode));
 49         if(tnode == NULL){
 50             return NULL;
 51         }
 52         tnode->data = tdata[num - 1];
 53         tnode->Lchild = CreateBitTree(tdata, num - 1);
 54         tnode->Rchild = CreateBitTree(tdata, num - 1);
 55         return tnode;
 56     }
 57     else{
 58         return NULL;
 59     }
 60 }
 61 
 62 /*
 63 *@brief 二叉树的前序遍历
 64 *
 65 *@param [in] tnode    以该节点为根节点进行遍历
 66 *       [in] Visit    对节点调用的指针函数
 67 *
 68 *@return 空
 69 */
 70 void PreOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode)){
 71     if(tnode != NULL){
 72         Visit(tnode);
 73         PreOrderTraverse(tnode->Lchild, Visit);
 74         PreOrderTraverse(tnode->Rchild, Visit);
 75     }
 76 }
 77 
 78 /*
 79 *@brief 二叉树的中遍历
 80 *
 81 *@param [in] tnode    以该节点为根节点进行遍历
 82 *       [in] Visit    对节点调用的指针函数
 83 *
 84 *@return 空
 85 */
 86 void MidOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode)){
 87     if(tnode != NULL){
 88         MidOrderTraverse(tnode->Lchild, Visit);
 89         Visit(tnode);
 90         MidOrderTraverse(tnode->Rchild, Visit);
 91     }
 92 }
 93 
 94 /*
 95 *@brief 二叉树的后遍历
 96 *
 97 *@param [in] tnode    以该节点为根节点进行遍历
 98 *       [in] Visit    对节点调用的指针函数
 99 *
100 *@return 空
101 */
102 void PostOrderTraverse(BTNode* tnode, void(*Visit)(BTNode* tnode)){
103     if(tnode != NULL){
104         PostOrderTraverse(tnode->Lchild, Visit);
105         PostOrderTraverse(tnode->Rchild, Visit);
106         Visit(tnode);
107     }
108 }
109 
110 /*
111 *@brief 二叉树遍历
112 *
113 *@param [in] tnode    对该节点节点调用本函数
114 *
115 *@return 空
116 */
117 void PrintData(BTNode* tnode){
118     printf("%d\n", tnode->data);
119 }

posted on 2014-10-21 18:30  angle_qqs  阅读(440)  评论(0编辑  收藏  举报

导航