C语言实现二叉树的基本操作

http://blog.csdn.net/chenyufeng1991/article/details/50858926

C语言实现二叉树的基本操作

我在前面的博客中讲解了链表、栈和队列,这些数据结构其实都是线性表,并且给出了详细的实现。从今天开始,我们将要来学习树,树作为一种数据结构我们经常会用到,作为起步和基础,我们先来实现二叉树,也就是每个节点有不超过2个子节点的树。对于树的操作,最基本的创建、遍历、求树高、节点数等。代码上传至 https://github.com/chenyufeng1991/BinaryTree 。

(1)节点的定义

 

[cpp] view plain copy
 
 print?
  1. typedef struct BTNode{  
  2.     int data;  
  3.     struct BTNode *lChild;  
  4.     struct BTNode *rChild;  
  5. }BiTNode;  



 

(2)二叉树的创建

 

 

[cpp] view plain copy
 
 print?
  1. //先序创建二叉树  
  2. int CreateBiTree(BiTNode **T)  
  3. {  
  4.     int ch;  
  5.     scanf("%d",&ch);  
  6.     if (ch == -1)  
  7.     {  
  8.         *T = NULL;  
  9.         return 0;  
  10.     }  
  11.     else  
  12.     {  
  13.         *T = (BiTNode *)malloc(sizeof(BiTNode));  
  14.         if (T == NULL)  
  15.         {  
  16.             printf("failed\n");  
  17.             return 0;  
  18.         }  
  19.         else  
  20.         {  
  21.             (*T)->data = ch;  
  22.             printf("输入%d的左子节点:",ch);  
  23.             CreateBiTree(&((*T)->lChild));  
  24.             printf("输入%d的右子节点:",ch);  
  25.             CreateBiTree((&(*T)->rChild));  
  26.         }  
  27.     }  
  28.   
  29.     return 1;  
  30. }  



 


(3)先序遍历二叉树

 

 

[cpp] view plain copy
 
 print?
  1. //先序遍历二叉树  
  2. void PreOrderBiTree(BiTNode *T)  
  3. {  
  4.     if (T == NULL)  
  5.     {  
  6.         return;  
  7.     }  
  8.     else  
  9.     {  
  10.         printf("%d ",T->data);  
  11.         PreOrderBiTree(T->lChild);  
  12.         PreOrderBiTree(T->rChild);  
  13.     }  
  14. }  



 


(4)中序遍历二叉树

 

 

[cpp] view plain copy
 
 print?
  1. //中序遍历二叉树  
  2. void MiddleOrderBiTree(BiTNode *T)  
  3. {  
  4.     if (T == NULL)  
  5.     {  
  6.         return;  
  7.     }  
  8.     else  
  9.     {  
  10.         MiddleOrderBiTree(T->lChild);  
  11.         printf("%d ",T->data);  
  12.         MiddleOrderBiTree(T->rChild);  
  13.     }  
  14. }  



 


(5)后续遍历二叉树

 

 

[cpp] view plain copy
 
 print?
  1. //后续遍历二叉树  
  2. void PostOrderBiTree(BiTNode *T)  
  3. {  
  4.     if (T == NULL)  
  5.     {  
  6.         return;  
  7.     }  
  8.     else  
  9.     {  
  10.         PostOrderBiTree(T->lChild);  
  11.         PostOrderBiTree(T->rChild);  
  12.         printf("%d ",T->data);  
  13.     }  
  14. }  



 


(6)二叉树的深度

 

 

[cpp] view plain copy
 
 print?
  1. //二叉树的深度  
  2. int TreeDeep(BiTNode *T)  
  3. {  
  4.     int deep = 0;  
  5.     if (T != NULL)  
  6.     {  
  7.         int leftdeep = TreeDeep(T->lChild);  
  8.         int rightdeep = TreeDeep(T->rChild);  
  9.         deep = leftdeep >= rightdeep?leftdeep+1:rightdeep+1;  
  10.     }  
  11.   
  12.     return deep;  
  13. }  



 


(7)叶子节点个数

 

 

[cpp] view plain copy
 
 print?
  1. //叶子节点个数  
  2. int LeafCount(BiTNode *T)  
  3. {  
  4.     static int count;  
  5.     if (T != NULL)  
  6.     {  
  7.         if (T->lChild == NULL && T->rChild == NULL)  
  8.         {  
  9.             count++;  
  10.         }  
  11.   
  12.         LeafCount(T->lChild);  
  13.         LeafCount(T->rChild);  
  14.     }  
  15.   
  16.     return count;  
  17. }  



 

 

 

(8)测试函数

 

[cpp] view plain copy
 
 print?
    1. //主函数  
    2. int main(int argc,const char *argv[])  
    3. {  
    4.     BiTNode *T;  
    5.     int depth,leafCount = 0;  
    6.     printf("请输入第一个节点的值,-1表示没有叶节点:\n");  
    7.     CreateBiTree(&T);  
    8.   
    9.     printf("先序遍历二叉树:");  
    10.     PreOrderBiTree(T);  
    11.     printf("\n");  
    12.   
    13.     printf("中序遍历二叉树:");  
    14.     MiddleOrderBiTree(T);  
    15.     printf("\n");  
    16.   
    17.     printf("后续遍历二叉树:");  
    18.     PostOrderBiTree(T);  
    19.     printf("\n");  
    20.   
    21.     depth = TreeDeep(T);  
    22.     printf("树的深度为:%d\n",depth);  
    23.       
    24.     leafCount = LeafCount(T);  
    25.     printf("叶子节点个数:%d\n",leafCount);  
    26.   
    27.     return 0;  
    28. }  

posted on 2017-10-22 19:57  小西红柿  阅读(361)  评论(0)    收藏  举报

导航