zoukankan      html  css  js  c++  java
  • 各种基本算法实现小结(三)—— 树与二叉树

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

    (均已测试通过)

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

    二叉树——先序

    测试环境:VS 2010

     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     }  

    运行结果:

        

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

    二叉树——各种操作

    测试环境:VS 2010

      1 #include "stdafx.h"
      2 #include <malloc.h>
      3 
      4 struct _node{
      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 
     13 pnode create_tree(){
     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 
     30 void print_pretree(pnode ps){
     31     if(ps!=NULL){
     32         printf("%3c",ps->data);
     33         print_pretree(ps->lchild);
     34         print_pretree(ps->rchild);
     35     }
     36 }
     37 
     38 void print_midtree(pnode ps){
     39     if(ps!=NULL){
     40         print_midtree(ps->lchild);
     41         printf("%3c",ps->data);
     42         print_midtree(ps->rchild);
     43     }
     44 }
     45 
     46 void print_posttree(pnode ps){
     47     if(ps!=NULL){
     48         print_posttree(ps->lchild);
     49         print_posttree(ps->rchild);
     50         printf("%3c",ps->data);
     51     }
     52 }
     53 
     54 int count_leaf(pnode ps){
     55     if(ps!=NULL){
     56         if(ps->lchild==NULL && ps->rchild==NULL){
     57             ++count_l;
     58         }
     59         count_leaf(ps->lchild);
     60         count_leaf(ps->rchild);
     61     }
     62     return count_l;
     63 }
     64 
     65 int count_node(pnode ps){
     66     if(ps!=NULL){
     67         count_n++;
     68         count_node(ps->lchild);
     69         count_node(ps->rchild);
     70     }
     71     return count_n;
     72 }
     73 
     74 int count_depth(pnode ps){
     75     int ldep,rdep;
     76     if(ps==NULL)
     77         return 0;
     78     else
     79     {
     80         ldep=count_depth(ps->lchild);
     81         rdep=count_depth(ps->rchild);
     82         return ldep>rdep ? (ldep+1):(rdep+1);
     83     }
     84 }
     85 
     86 int _tmain(int argc, _TCHAR* argv[])
     87 {
     88     pnode ps;
     89     ps=create_tree();
     90     printf("pre order...
    ");
     91     print_pretree(ps);
     92     printf("
    ");
     93     printf("mid order...
    ");
     94     print_midtree(ps);
     95     printf("
    ");
     96     printf("post order...
    ");
     97     print_posttree(ps);
     98     printf("
    ");
     99     printf("number of leaf is : %d
    ",count_leaf(ps));
    100     printf("number of node is : %d
    ",count_node(ps));
    101     printf("max of depth is: %d
    ",count_depth(ps));
    102 
    103     return 0;
    104 }

    运行结果:

         

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

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

    测试环境:VS 2010

      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...
    ");  
    257         pt=create_tree();  
    258         /************  recursion ************/  
    259         printf("n
    recursion...");  
    260         printf("npre tree...
    ");  
    261         print_pretree(pt);  
    262         printf("nmid tree...
    ");  
    263         print_midtree(pt);  
    264         printf("npost tree...
    ");  
    265         print_posttree(pt);  
    266         /************  stack ************/  
    267         printf("n
    stack, non recursion...");  
    268         printf("npre tree...
    ");  
    269         print_pretree2(pt);  
    270         printf("nmid tree...
    ");  
    271         print_midtree2(pt);  
    272         printf("npost tree...
    ");  
    273         print_posttree2(pt);  
    274         printf("n");  
    275         return 0;  
    276     }  

    运行结果:

          

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

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

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

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

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

    运行结果:

          

     本文转自:~~~

  • 相关阅读:
    restart xinetd
    Hello World——Linux汇编
    C++箴言:理解inline化的介入和排除
    网上找来的不错的Debian sources.list
    C/C++中static变量和static函数的用法
    Inline内联函数的总结贴
    C++箴言:理解inline化的介入和排除
    Hello World——Linux汇编
    网上找来的不错的Debian sources.list
    C/C++中static变量和static函数的用法
  • 原文地址:https://www.cnblogs.com/labi/p/3587677.html
Copyright © 2011-2022 走看看