C/C++:各种基本算法实现小结(三)—— 树与二叉树

各种基本算法实现小结(三)—— 树与二叉树

(均已测试通过)

===================================================================

二叉树——先序

测试环境:VC 6.0 (C)

[cpp]  view plain  copy
 print ?
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. #include <stdlib.h>  
  4. struct _node  
  5. {  
  6.     char data;  
  7.     struct _node *lchild;  
  8.     struct _node *rchild;  
  9. };  
  10. typedef struct _node node, *pnode;  
  11. pnode create_tree()  
  12. {  
  13.     pnode pt;  
  14.     char data;  
  15.     scanf("%c", &data);  
  16.     getchar();  
  17.     if(data==' ')  
  18.         pt=NULL;  
  19.     else  
  20.     {  
  21.         pt=(pnode)malloc(sizeof(node));  
  22.         pt->data=data;  
  23.         pt->lchild=create_tree();  
  24.         pt->rchild=create_tree();  
  25.     }  
  26.     return(pt);  
  27. }  
  28. void print_pretree(pnode ps)  
  29. {  
  30.     if(ps != NULL)  
  31.     {  
  32.         printf("%3c", ps->data);  
  33.         print_pretree(ps->lchild);  
  34.         print_pretree(ps->rchild);  
  35.     }     
  36. }  
  37. void main()  
  38. {  
  39.     pnode ps;  
  40.     ps=create_tree();  
  41.     print_pretree(ps);  
  42.     printf("/n");  
  43. }  

运行结果:

     

===========================================================

二叉树——各种操作

测试环境:VC 6.0 (C)

[cpp]  view plain  copy
 print ?
  1. #include <stdio.h>  
  2. #include <malloc.h>  
  3. struct _node  
  4. {  
  5.     char data;  
  6.     struct _node *lchild;  
  7.     struct _node *rchild;  
  8. };  
  9. typedef struct _node node, *pnode;  
  10. int count_l=0;  /* count leaf */  
  11. int count_n=0;  /* count node */  
  12. pnode create_tree()  
  13. {  
  14.     pnode pt;  
  15.     char data;  
  16.     scanf("%c", &data);  
  17.     getchar();  
  18.     if(data==' ')  
  19.         pt=NULL;  
  20.     else  
  21.     {  
  22.         pt=(pnode)malloc(sizeof(node));  
  23.         pt->data=data;  
  24.         pt->lchild=create_tree();  
  25.         pt->rchild=create_tree();  
  26.     }  
  27.     return(pt);  
  28. }  
  29. void print_pretree(pnode ps)  
  30. {  
  31.     if(ps != NULL)  
  32.     {  
  33.         printf("%3c", ps->data);  
  34.         print_pretree(ps->lchild);  
  35.         print_pretree(ps->rchild);  
  36.     }     
  37. }  
  38. void print_midtree(pnode ps)  
  39. {  
  40.     if(ps != NULL)  
  41.     {  
  42.         print_midtree(ps->lchild);  
  43.       
  44.         printf("%3c", ps->data);  
  45.         print_midtree(ps->rchild);     
  46.     }  
  47. }  
  48. void print_posttree(pnode ps)  
  49. {  
  50.     if(ps != NULL)  
  51.     {  
  52.         print_posttree(ps->lchild);  
  53.         print_posttree(ps->rchild);  
  54.         printf("%3c", ps->data);  
  55.     }  
  56. }  
  57. int count_leaf(pnode ps)  
  58. {     
  59.     if(ps != NULL)  
  60.     {  
  61.         if(ps->lchild == NULL && ps->rchild == NULL)  
  62.         count_l++;    
  63.         count_leaf(ps->lchild);  
  64.         count_leaf(ps->rchild);  
  65.     }  
  66.     return count_l;  
  67. }  
  68. int count_node(pnode ps)  
  69. {  
  70.     if(ps != NULL)  
  71.     {  
  72.         count_n++;  
  73.         count_node(ps->lchild);  
  74.         count_node(ps->rchild);  
  75.     }  
  76.     return count_n;  
  77. }  
  78. int count_depth(pnode ps)  
  79. {  
  80.     int ldep, rdep;  
  81.     if(ps == NULL)  
  82.         return 0;  
  83.     else  
  84.     {  
  85.         ldep=count_depth(ps->lchild);  
  86.         rdep=count_depth(ps->rchild);  
  87.           
  88.         return ldep>rdep ? (ldep+1) : (rdep+1);  
  89.     }  
  90. }  
  91. void main()  
  92. {  
  93.     pnode ps;  
  94.     ps=create_tree();  
  95.       
  96.     printf("pre order.../n");  
  97.     print_pretree(ps);  
  98.     printf("/n");  
  99.     printf("mid order.../n");  
  100.     print_midtree(ps);  
  101.     printf("/n");  
  102.     printf("post order.../n");  
  103.     print_posttree(ps);  
  104.     printf("/n");  
  105.     printf("number of leaf is: %d/n", count_leaf(ps));  
  106.     printf("number of node is: %d/n", count_node(ps));  
  107.     printf("max  of  depth is: %d/n", count_depth(ps));  
  108. }  

运行结果:

      

===========================================================

二叉树——先序、中序、后序的递归与非递归实现

测试环境:VS2008 (C)

[cpp]  view plain  copy
 print ?
  1. #include "stdafx.h"  
  2. #include <stdlib.h>  
  3. #include <malloc.h>  
  4. #define DataType char  
  5. /**************************************/  
  6. /********     树的结构定义     ********/  
  7. /**************************************/  
  8. struct _tree  
  9. {  
  10.     DataType data;  
  11.     struct _tree *lchild;  
  12.     struct _tree *rchild;  
  13. };  
  14. typedef struct _tree tree, *ptree;  
  15. /**************************************/  
  16. /********     栈的结构定义     ********/  
  17. /**************************************/  
  18. struct _node  
  19. {  
  20.     ptree pt;  
  21.     struct _node *next;  
  22. };  
  23. typedef struct _node node, *pnode;  
  24. struct _stack  
  25. {  
  26.     int size;  
  27.     pnode ptop;  
  28. };  
  29. typedef struct _stack stack, *pstack;  
  30. /**************************************/  
  31. /********     堆的结构定义     ********/  
  32. /**************************************/  
  33. struct _queue  
  34. {  
  35.     pnode front;  
  36.     pnode rear;  
  37. };  
  38. typedef struct _queue queue, *pqueue;  
  39. /**************************************/  
  40. /********     栈的数据操作     ********/  
  41. /**************************************/  
  42. pstack init_stack()  
  43. {  
  44.     pnode pn=NULL;  
  45.     pstack ps=NULL;  
  46.     pn=(pnode)malloc(sizeof(node));  
  47.     ps=(pstack)malloc(sizeof(stack));  
  48.     pn->pt=NULL;  
  49.     pn->next=NULL;  
  50.     ps->ptop=pn;  
  51.     return ps;  
  52. }  
  53. int empty_stack(pstack ps)  
  54. {  
  55.     if(ps->ptop->next==NULL)  
  56.         return 1;  
  57.     else  
  58.         return 0;  
  59. }  
  60. void push_stack(pstack ps, ptree pt) /* flag for post tree: 0 for lchild; 1 for rchild */  
  61. {  
  62.     pnode pn=NULL;  
  63.     pn=(pnode)malloc(sizeof(node));  
  64.     pn->pt=pt;  
  65.     pn->next=ps->ptop;  
  66.     ps->ptop=pn;  
  67. }  
  68. ptree pop_stack(pstack ps)  
  69. {  
  70.     ptree pt=NULL;  
  71.     pnode pn=NULL;  
  72.     if(!empty_stack(ps))  
  73.     {  
  74.         pn=ps->ptop;  
  75.         ps->ptop=ps->ptop->next;  
  76.         pt=pn->pt;  
  77.         free(pn);  
  78.     }  
  79.     return pt;  
  80. }  
  81. ptree gettop_stack(pstack ps)  
  82. {  
  83.     if(!empty_stack(ps))  
  84.         return ps->ptop->pt;  
  85. }  
  86. /**************************************/  
  87. /********     堆的数据操作     ********/  
  88. /**************************************/  
  89. queue init_queue()  
  90. {  
  91.     pnode pn=NULL;  
  92.     queue qu;  
  93.     pn=(pnode)malloc(sizeof(node));  
  94.     pn->pt=NULL;  
  95.     pn->next=NULL;  
  96.     qu.front=qu.rear=pn;  
  97.     return qu;  
  98. }  
  99. int empty_queue(queue qu)  
  100. {  
  101.     if(qu.front==qu.rear)  
  102.         return 1;  
  103.     else  
  104.         return 0;  
  105. }  
  106. void en_queue(queue qu, ptree pt)  
  107. {  
  108.     pnode pn=NULL;  
  109.     pn=(pnode)malloc(sizeof(node));  
  110.     pn->pt;  
  111.     pn->next=qu.rear->next;  
  112.     qu.rear=pn;  
  113. }  
  114. ptree de_queue(queue qu)  
  115. {  
  116.     ptree pt=NULL;  
  117.     pnode pn=NULL;  
  118.     if(!empty_queue(qu))  
  119.     {  
  120.         pn=qu.front;  
  121.         qu.front=qu.front->next;  
  122.         pt=pn->pt;  
  123.         free(pn);  
  124.     }  
  125.     return pt;  
  126. }  
  127. /**************************************/  
  128. /********     堆的数据操作     ********/  
  129. /**************************************/  
  130. ptree init_tree()  
  131. {  
  132.     ptree pt=NULL;  
  133.     pt=(ptree)malloc(sizeof(tree));  
  134.     pt->data='0';  
  135.     pt->lchild=NULL;  
  136.     pt->rchild=NULL;  
  137.     return pt;  
  138. }  
  139. ptree create_tree()  
  140. {  
  141.     char ch;  
  142.     ptree pt=NULL;  
  143.       
  144.     scanf("%c", &ch);  
  145.     getchar();    
  146.     if(ch==' ')  
  147.         return NULL;  
  148.     else  
  149.     {  
  150.         pt=(ptree)malloc(sizeof(tree));  
  151.         pt->data=ch;  
  152.         pt->lchild=create_tree();  
  153.         pt->rchild=create_tree();  
  154.     }  
  155.     return pt;  
  156. }  
  157. void print_pretree(ptree pt)  
  158. {  
  159.     if(pt!=NULL)  
  160.     {  
  161.         printf("%3c", pt->data);  
  162.         print_pretree(pt->lchild);  
  163.         print_pretree(pt->rchild);  
  164.     }  
  165. }  
  166. void print_pretree2(ptree pt)  
  167. {  
  168.     pstack ps=NULL;  
  169.     ptree p=NULL;  
  170.     ps=init_stack();  
  171.     p=pt;  
  172.     while(p!=NULL || !empty_stack(ps))  
  173.     {  
  174.         while(p!=NULL)  
  175.         {  
  176.             printf("%3c", p->data);  
  177.             push_stack(ps, p);  
  178.             p=p->lchild;  
  179.         }  
  180.         if(!empty_stack(ps))  
  181.         {  
  182.             p=pop_stack(ps);  
  183.             p=p->rchild;  
  184.         }  
  185.     }  
  186. }  
  187. void print_midtree(ptree pt)  
  188. {  
  189.     if(pt!=NULL)  
  190.     {  
  191.         print_midtree(pt->lchild);  
  192.         printf("%3c", pt->data);  
  193.         print_midtree(pt->rchild);  
  194.     }  
  195. }  
  196. void print_midtree2(ptree pt)  
  197. {  
  198.     pstack ps=NULL;  
  199.     ptree p=NULL;  
  200.     ps=init_stack();  
  201.     p=pt;  
  202.     while (p!=NULL || !empty_stack(ps))  
  203.     {  
  204.         while(p!=NULL)  
  205.         {  
  206.             push_stack(ps, p);  
  207.             p=p->lchild;       
  208.         }  
  209.         if(!empty_stack(ps))  
  210.         {             
  211.             p=pop_stack(ps);  
  212.             printf("%3c", p->data);  
  213.             p=p->rchild;  
  214.         }  
  215.     }  
  216. }  
  217. void print_posttree(ptree pt)  
  218. {  
  219.     if(pt!=NULL)  
  220.     {  
  221.         print_posttree(pt->lchild);        
  222.         print_posttree(pt->rchild);    
  223.         printf("%3c", pt->data);  
  224.     }  
  225. }  
  226. void print_posttree2(ptree pt)  
  227. {  
  228.     pstack ps=NULL;  
  229.     ptree p=NULL;  
  230.     ptree p2=NULL;  
  231.     ptree lastvisit=NULL;  
  232.     ps=init_stack();  
  233.     p=pt;  
  234.     while (p!=NULL || !empty_stack(ps))  
  235.     {  
  236.         while(p!=NULL)  
  237.         {  
  238.             push_stack(ps, p);  
  239.             p=p->lchild;                   
  240.         }  
  241.         p2=gettop_stack(ps); /* top: rchild==null or sub_root */  
  242.         if(p2->rchild==NULL || p2->rchild==lastvisit)  
  243.         {  
  244.             printf("%3c", p2->data);  
  245.             lastvisit=pop_stack(ps); /* pop */  
  246.         }  
  247.         else  
  248.             p=p2->rchild;  
  249.     }     
  250. }  
  251. int _tmain(int argc, _TCHAR* argv[])  
  252. {  
  253.     ptree pt=NULL;  
  254.     /*pt=init_tree();*/  
  255.       
  256.     printf("Create recursion tree.../n");  
  257.     pt=create_tree();  
  258.     /************  recursion ************/  
  259.     printf("/n/nrecursion...");  
  260.     printf("/npre tree.../n");  
  261.     print_pretree(pt);  
  262.     printf("/nmid tree.../n");  
  263.     print_midtree(pt);  
  264.     printf("/npost tree.../n");  
  265.     print_posttree(pt);  
  266.     /************  stack ************/  
  267.     printf("/n/nstack, non recursion...");  
  268.     printf("/npre tree.../n");  
  269.     print_pretree2(pt);  
  270.     printf("/nmid tree.../n");  
  271.     print_midtree2(pt);  
  272.     printf("/npost tree.../n");  
  273.     print_posttree2(pt);  
  274.     printf("/n");  
  275.     return 0;  
  276. }  

运行结果:

       

===========================================================

二叉树——学习交流与修正改进

在网上看到了好多人转载这段代码,我也复制、粘贴下来学习

但在VC6.0编译器上运行并未通过,于是调试修正了几个小bug

测试运行通过后的代码粘贴如下,希望对大家学习有所帮助,谢谢!

本算法源码引用网址:http://www.ccrun.com/article.asp?i=292&d=y6y12h (二叉树实现源代码)


测试环境:VC 6.0 (C)

[cpp]  view plain  copy
 print ?
  1. #include <conio.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #define OK 1  
  5. #define ERROR 0  
  6. #define TRUE 1  
  7. #define FALSE 0  
  8. #define OVERFLOW -2  
  9. typedef int status;  
  10. typedef struct BiNode  
  11. {  
  12.     char Data;  
  13.     struct BiNode* lChild;  
  14.     struct BiNode* rChild;  
  15. }BiNode,*pBiNode;  
  16. status CreateTree(BiNode** pTree);  
  17. status PreOrderTraval(BiNode* pTree);  
  18. status InOrderTraval(BiNode* pTree);  
  19. status PostOrderTraval(BiNode* pTree);  
  20. status Visit(char Data);  
  21. status ShowLeaves(BiNode* pTree);  
  22. status DelTree(BiNode* pTree);  
  23. status Display(BiNode* pTree,int Level);  
  24. status Clear(BiNode* pTree);  
  25. BiNode *pRoot=NULL;  
  26. void main()  
  27. {  
  28.     CreateTree(&pRoot);  
  29.     printf("/nPreOrder:");  
  30.     PreOrderTraval(pRoot);  
  31.     printf("/n");  
  32.     printf("/nInOrder:");  
  33.     InOrderTraval(pRoot);  
  34.     printf("/n");  
  35.     printf("/nPostOrder:");  
  36.     PostOrderTraval(pRoot);  
  37.     printf("/n");  
  38.     printf("/nShowLeaves:");  
  39.     ShowLeaves(pRoot);  
  40.     printf("/n-----------------------/n");  
  41.     printf("/n");  
  42.     Display(pRoot,0);  
  43.     printf("/n");  
  44.     printf("/nDeleting Tree:/n");  
  45.     DelTree(pRoot);  
  46.     printf("BiTree Deleted.");  
  47. }  
  48. status CreateTree(BiNode** pTree)   
  49. {  
  50.     char ch;  
  51.     scanf("%c",&ch);  
  52.     getchar();  
  53.       
  54.     if(ch==' ')  /* NOTE: enter space, example: [ab  cd  e  ] */  
  55.     {  
  56.         (*pTree)=NULL;  
  57.     }  
  58.     else  
  59.     {  
  60.         if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))  
  61.         {  
  62.             exit(OVERFLOW);  
  63.         }  
  64.         (*pTree)->Data=ch;  
  65.         CreateTree(&((*pTree)->lChild));  
  66.         CreateTree(&((*pTree)->rChild));  
  67.     }  
  68. return OK;  
  69. }  
  70. status PreOrderTraval(BiNode* pTree)  
  71. {  
  72.     if(pTree)  
  73.     {  
  74.         if(Visit(pTree->Data))  
  75.         {  
  76.             if(PreOrderTraval(pTree->lChild))  
  77.             {  
  78.                 if(PreOrderTraval(pTree->rChild))  
  79.                 {  
  80.                     return OK;  
  81.                 }  
  82.             }  
  83.         }  
  84.         return ERROR;  
  85.     }  
  86.     else  
  87.     {  
  88.         return OK;  
  89.     }  
  90. }  
  91. status InOrderTraval(BiNode* pTree)  
  92. {  
  93.     if(pTree)  
  94.     {  
  95.         if(InOrderTraval(pTree->lChild))  
  96.         {  
  97.             if(Visit(pTree->Data))  
  98.             {  
  99.                 if(InOrderTraval(pTree->rChild))  
  100.                 {  
  101.                     return OK;  
  102.                 }  
  103.             }  
  104.             return ERROR;  
  105.         }  
  106.         return ERROR;  
  107.     }  
  108.     else  
  109.         return OK;  
  110. }  
  111. status PostOrderTraval(BiNode* pTree)  
  112. {  
  113.     if(pTree)  
  114.     {  
  115.         if(PostOrderTraval(pTree->lChild))       
  116.         {  
  117.             if(PostOrderTraval(pTree->rChild))   
  118.             {  
  119.                 if(Visit(pTree->Data))  
  120.                 {  
  121.                     return OK;  
  122.                 }  
  123.                 return ERROR;  
  124.             }  
  125.         }  
  126.         return ERROR;  
  127.     }  
  128.     else  
  129.     {  
  130.         return OK;  
  131.     }  
  132. }  
  133. status Visit(char Data)  
  134. {  
  135.     printf("%c",Data);  
  136.     return OK;  
  137. }  
  138. status Display(BiNode* pTree,int Level)  
  139. {  
  140.     int i;  
  141.     if(pTree==NULL)   
  142.         return FALSE;  
  143.     Display(pTree->lChild,Level+1);  
  144.     for(i=0;i<Level-1;i++)  
  145.     {  
  146.         printf(" ");  
  147.     }  
  148.     if(Level>=1)  
  149.     {  
  150.         printf("--");  
  151.     }  
  152.     printf("%c/n",pTree->Data);  
  153.     Display(pTree->rChild,Level+1);  
  154.     return TRUE;  
  155. }  
  156. status ShowLeaves(BiNode* pTree)  
  157. {  
  158.     if(pTree)  
  159.     {  
  160.         if(ShowLeaves(pTree->lChild))  
  161.         {  
  162.             if(ShowLeaves(pTree->rChild))  
  163.             {  
  164.                 if((pTree->lChild==NULL)&&(pTree->rChild==NULL))  
  165.                 {  
  166.                     if(!Visit(pTree->Data))  
  167.                     {  
  168.                         return ERROR;  
  169.                     }  
  170.                 }   
  171.                 return OK;  
  172.             }  
  173.         }  
  174.         return ERROR;  
  175.     }  
  176.     else  
  177.     {  
  178.         return OK;  
  179.     }  
  180. }  
  181. status DelTree(BiNode* pTree)  
  182. {  
  183.     if(pTree)  
  184.     {  
  185.         if(DelTree(pTree->lChild))  
  186.         {  
  187.             if(DelTree(pTree->rChild))  
  188.             {  
  189.                 printf("Deleting %c/n",pTree->Data);  
  190.                 free((void*)pTree);  
  191.                 return OK;  
  192.             }  
  193.         }  
  194.         return ERROR;  
  195.     }  
  196.     else  
  197.         return OK;  
  198. }   

运行结果:

     

 

===========================================================

上述代码改进后,逻辑更清晰,并添加了计算二叉树层次的函数 ShowDepth(BiNode* pTree)

具体代码如下:

[cpp]  view plain  copy
 print ?
  1. #include <conio.h>  
  2. #include <stdio.h>  
  3. #include <stdlib.h>  
  4. #define OK 1  
  5. #define ERROR 0  
  6. #define TRUE 1  
  7. #define FALSE 0  
  8. #define OVERFLOW -2  
  9. typedef int status;  
  10. typedef struct BiNode  
  11. {  
  12.     char Data;  
  13.     struct BiNode* lChild;  
  14.     struct BiNode* rChild;  
  15. }BiNode,*pBiNode;  
  16. status CreateTree(BiNode** pTree);  
  17. status PreOrderTraval(BiNode* pTree);  
  18. status InOrderTraval(BiNode* pTree);  
  19. status PostOrderTraval(BiNode* pTree);  
  20. status Visit(char Data);  
  21. status ShowLeaves(BiNode* pTree);  
  22. status ShowDepth(BiNode* pTree);  
  23. status DelTree(BiNode* pTree);  
  24. status Display(BiNode* pTree,int Level);  
  25. status Clear(BiNode* pTree);  
  26. BiNode *pRoot=NULL;  
  27. void main()  
  28. {  
  29.     CreateTree(&pRoot);  
  30.     printf("/nPreOrder:");  
  31.     PreOrderTraval(pRoot);  
  32.     printf("/n");  
  33.     printf("/nInOrder:");  
  34.     InOrderTraval(pRoot);  
  35.     printf("/n");  
  36.     printf("/nPostOrder:");  
  37.     PostOrderTraval(pRoot);  
  38.     printf("/n");  
  39.     printf("/nShowLeaves:");  
  40.     ShowLeaves(pRoot);  
  41.     printf("/nShowDepth:%d/n", ShowDepth(pRoot));  
  42.     printf("/n------------------/n");  
  43.     printf("/n");  
  44.     Display(pRoot,0);  
  45.     printf("/n");  
  46.     printf("/nDeleting Tree:/n");  
  47.     DelTree(pRoot);  
  48.     printf("BiTree Deleted.");  
  49. }  
  50. status CreateTree(BiNode** pTree)   
  51. {  
  52.     char ch;  
  53.     scanf("%c",&ch);  
  54.     getchar();  
  55.       
  56.     if(ch==' ')  /* NOTE: enter space, example: [ab  cd  e  ] */  
  57.         (*pTree)=NULL;  
  58.     else  
  59.     {  
  60.         if(!((*pTree)=(BiNode*)malloc(sizeof(BiNode))))  
  61.             exit(OVERFLOW);  
  62.         (*pTree)->Data=ch;  
  63.         CreateTree(&((*pTree)->lChild));  
  64.         CreateTree(&((*pTree)->rChild));  
  65.     }  
  66.     return OK;  
  67. }  
  68. status PreOrderTraval(BiNode* pTree)  
  69. {  
  70.     if(pTree)  
  71.     {  
  72.         Visit(pTree->Data);  
  73.         PreOrderTraval(pTree->lChild);  
  74.         PreOrderTraval(pTree->rChild);  
  75.     }  
  76.     return OK;  
  77. }  
  78. status InOrderTraval(BiNode* pTree)  
  79. {  
  80.     if(pTree)  
  81.     {  
  82.         InOrderTraval(pTree->lChild);  
  83.         Visit(pTree->Data);  
  84.         InOrderTraval(pTree->rChild);  
  85.     }  
  86.       
  87.     return OK;  
  88. }  
  89. status PostOrderTraval(BiNode* pTree)  
  90. {  
  91.     if(pTree)  
  92.     {  
  93.         PostOrderTraval(pTree->lChild);      
  94.         PostOrderTraval(pTree->rChild);     
  95.         Visit(pTree->Data);  
  96.     }  
  97.       
  98.     return OK;  
  99. }  
  100. status Visit(char Data)  
  101. {  
  102.     printf("%c",Data);  
  103.     return OK;  
  104. }  
  105. status Display(BiNode* pTree,int Level)  
  106. {  
  107.     int i;  
  108.     if(pTree==NULL)   
  109.         return FALSE;  
  110.     Display(pTree->lChild,Level+1);  
  111.     for(i=0;i<Level-1;i++)  
  112.     {  
  113.         printf(" ");  
  114.     }  
  115.     if(Level>=1)  
  116.     {  
  117.         printf("--");  
  118.     }  
  119.     printf("%c/n",pTree->Data);  
  120.     Display(pTree->rChild,Level+1);  
  121.     return TRUE;  
  122. }  
  123. status ShowLeaves(BiNode* pTree)  
  124. {  
  125.     if(pTree)  
  126.     {  
  127.         ShowLeaves(pTree->lChild);  
  128.         ShowLeaves(pTree->rChild);  
  129.         if((pTree->lChild==NULL)&&(pTree->rChild==NULL))  
  130.             Visit(pTree->Data);  
  131.     }  
  132.       
  133.     return OK;  
  134. }  
  135. status ShowDepth(BiNode* pTree)  
  136. {  
  137.     int ldep=0, rdep=0;  
  138.       
  139.     if(!pTree)  
  140.         return 0;  
  141.     else  
  142.     {  
  143.         ldep=ShowDepth(pTree->lChild);  
  144.         rdep=ShowDepth(pTree->rChild);  
  145.         return ldep>rdep ? (ldep+1) : (rdep+1);  
  146.     }  
  147. }  
  148. status DelTree(BiNode* pTree)  
  149. {  
  150.     if(pTree)  
  151.     {  
  152.         DelTree(pTree->lChild);  
  153.         DelTree(pTree->rChild);  
  154.         printf("Deleting %c/n",pTree->Data);  
  155.         free((void*)pTree);  
  156.     }  
  157.     return OK;  
  158. }   

运行结果:

       

===========================================================

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值