zoukankan      html  css  js  c++  java
  • 二叉树的遍历

     #include<stdio.h>
    #include<string.h>
    #include<windows.h>
    #include<stdlib.h>
    #include<stack>
    #include<iostream>
    #define max 500
    using namespace std;
    typedef struct node
    {
       char data;
       struct node *left ,*right;
    }binode,*bitree;//树结构
    typedef struct
    {
     bitree num[max];
     int top;
    }sqstack;//栈结构
    typedef struct kk
    {
     bitree t;
     struct kk *next;
    }sqqueue;//
    typedef struct
    {
     sqqueue *front,*rear;
    }queue;//层次遍历时用到的队列结构
    typedef struct post_y
    {
     binode *prt;
     int flag;
    }post_type;
    typedef struct LP
    {
     post_type num[max];
     int top1;
    }Sqstack;
    int initqueue(queue *Q)
    {
     sqqueue *p;
     p=(sqqueue*)malloc(sizeof(sqqueue));
     Q->front=p;
     Q->rear=p;
     (Q->front)->next=NULL;
     return 0;
    }//初始化队列
    int queueempty(queue *Q)
    {
     if(Q->rear==Q->front)
     {
      return 1;
     }
     else
     return 0;
    }//队列的判空运算
    int queuepush(queue *Q,bitree T)
    {
      sqqueue *p;
      p=(sqqueue*)malloc(sizeof(sqqueue));
    
      p->t=T;
      p->next=NULL;
    
      (Q->rear)->next=p;
    
      Q->rear=p;
    
      return 0;
    }//元素入队
    bitree GET_top(queue *Q)
    {
        sqqueue *p;
        p=(sqqueue*)malloc(sizeof(sqqueue));
        bitree q;
        q=(bitree)malloc(sizeof(bitree));
        q->data='1';
        q->left=NULL;
        q->right=NULL;
        if(queueempty(Q))
        {
         return q;
        }
        else
        {
         p=(Q->front)->next;
         return p->t;
        }
    }//获取队首元素
    int queuepop(queue *Q)
    {
         if(queueempty(Q))
         {
          return -1;
         }
         else
         {
          sqqueue *p;
         p=(Q->front)->next;
         (Q->front)->next=p->next;
         if(p->next==NULL)
         Q->rear=Q->front;
         free(p);
         return 0;
     }
     }//元素出队
    void creattree(bitree &T)
    {
     char s;
     scanf("%c",&s);
     if(s=='#')
     {
      T=NULL;
     }
     else
     {
      T=(bitree)malloc(sizeof(binode));
    
      T->data=s;
    
      creattree(T->left);
    
      creattree(T->right);
     }
    }//建树
    void visit(bitree T)访问跟操作
    {
     if(T!=NULL)
     {
      printf("%c ",T->data);
     }
    }
    void firstvisit(bitree T)//递归实现先序遍历
    {
       if(NULL!=T)
       {
        visit(T);
    
        firstvisit(T->left);
    
        firstvisit(T->right);
       }
    }//递归实现先序遍历
    void midvisit(bitree T)//递归实现中序遍历
    {
     if(T!=NULL)
     {
      midvisit(T->left);
    
      visit(T);
    
      midvisit(T->right);
     }
    }//递归实现中序遍历
    void lastvisit(bitree T)//递归实现后序遍历
    {
     if(T!=NULL)
     {
      lastvisit(T->left);
    
      lastvisit(T->right);
    
      visit(T);
     }
    }//递归实现后序遍历
    void post_visit(bitree T)
    {
     Sqstack S;
     bitree p=NULL;
     post_type elem;
     S.top1=-1;
     p=T;
     while(NULL!=p||S.top1>-1)
     {
      while(NULL!=p)
      {
       elem.prt=p;
       elem.flag=1;
       S.num[++S.top1]=elem;
       p=p->left;
      }
      elem=S.num[S.top1--];
      if(1==elem.flag)
      {
       elem.flag=2;
       S.num[++S.top1]=elem;
       p=elem.prt->right;
      }
      else if(elem.flag==2)
      {
       p=elem.prt;
       printf("%c ",p->data);
       p=NULL;
      }
     }
    }//非递归实现后序遍历
    void previsit(bitree T)
    {
    
     binode *p;
     p=NULL;
        sqstack S;
        S.top=-1;
        if(NULL!=T)
        {
      S.num[++S.top]=T;
      while(S.top>-1)
      {
       p=S.num[S.top--];
       printf("%c ",p->data);
       if(NULL!=p->right)
       {
        S.num[++S.top]=p->right;
       }
       if(NULL!=p->left)
       {
        S.num[++S.top]=p->left;
       }
      }
     }
    }
    void inorder(bitree T)
    {
     sqstack S;
         S.top=-1;
         binode *p=T;
         while(p||S.top>-1)
         {
          while(NULL!=p)
          {
           S.num[++S.top]=p;
           p=p->left;
          }
          p=S.num[S.top--];
          printf("%c ",p->data);
          p=p->right;
         }
      }
    void levelvisit(bitree T)
    {
          queue *Q;
         bitree p=T;
      initqueue(Q);
      queuepush(Q,T);
      while(!queueempty(Q))
      {
       p=GET_top(Q);
       queuepop(Q);
       printf("%c ",p->data);
       if(p->left!=NULL) queuepush(Q,p->left);
       if(p->right!=NULL) queuepush(Q,p->right);
      }
     }//层次遍历
    void menu()
    {
     printf("		选择遍历方式
    ");
     printf("		1:先序遍历
    ");
     printf("		2:中序遍历
    ");
     printf("		3:后序遍历
    ");
     printf("		4:层次遍历
    ");
    }
    int main()
    {
     int n;
     printf("			手动输入测试数据
    ");
     printf("输入节点,其中#表示空子树
    ");
    
     bitree T;
     creattree(T);
    
     menu();
     while(scanf("%d",&n)!=EOF)
     {
      switch(n)
      {
       case 1:
        system("cls");
        printf("非递归先序遍历得到的序列
    ");
    
                 previsit(T);
    
                 printf("
    ");
    
                 printf("递归先序遍历得到的序列
    ");
    
                 firstvisit(T) ;
    
                  printf("
    ");
    
                Sleep(10000);
    
                  system("cls");
                  menu();
    
               break;
    
           case 2:
           system("cls");
        printf("非递归中序遍历得到的结果
    ");
    
                inorder(T);
    
                printf("
    ");
    
                printf("递归中序遍历得到的序列
    ");
    
                midvisit(T);
    
                printf("
    ");
    
                Sleep(100000);
    
                system("cls");
    
                menu();
    
                 break;
            case 3:
             system("cls");
    
           printf("递归后序遍历得到的序列
    ");
    
            lastvisit(T);
    
            printf("
    ");
    
            printf("非递归后序遍历的到的序列
    ");
    
            post_visit(T);
    
            printf("
    ");
    
            Sleep(10000);
    
            system("cls");
    
            menu();
    
            break;
            case 4:
             system("cls");
    
             printf("层次遍历得到的序列
    ");
    
                levelvisit(T);
    
                printf("
    ");
    
                Sleep(10000);
    
                system("cls");
    
                menu();
             break;
         }
      }
      return 0;
     }
    
    本次测试使用的数据
    ABC##DE#G##F###
    树的形态
    
    
    
    
    
  • 相关阅读:
    紫外传感器波长
    常见设备功耗
    点型感温火灾探测器研发思路
    C#使用Linq to XML进行XPath查询
    题解 最大获利
    题解 走迷宫
    2020-11-16 考试题解
    题解 最小生成树
    题解 「BZOJ4919 Lydsy1706月赛」大根堆
    题解 2020.10.24 考试 T4 模板
  • 原文地址:https://www.cnblogs.com/NaCl/p/9580210.html
Copyright © 2011-2022 走看看