二叉樹遍曆(中序,先序->後序;中序,後序->先序)

來源:互聯網
上載者:User

自己寫的兩個C語言程式,功能如題,如果有什麼問題,請大家留言!!!

開發平台:linux;vim;gcc

 

 

已知先序,中序,求後序:

#include<stdio.h>
#include<string.h>

#define MAX 20
char str[MAX]={'/0'};

 

int main()
{

     void postorder(char *str1,char *str2);
     char str1[MAX]={'/0'};
     char str2[MAX]={'/0'};
     int i;

 

     printf("please input the preorder:/n");
     scanf("%s%*c",str1); 
     printf("please input the inorder:/n");
     scanf("%s%*c",str2);

 

     postorder(str1,str2);

 

     printf("the postorder is:/n");
     for(i=0;str[i]=='/0';i++);
     puts(str+i);
}

void postorder(char *str1,char *str2)
{
     static int num= MAX-1;
     int i,j;
     char str3[MAX]={'/0'};
     char str4[MAX]={'/0'};
     char str5[MAX]={'/0'};
     char str6[MAX]={'/0'}; 

 

     if(str1[0]=='/0'||str2[0]=='/0')
         return;

 

     str[num--]=str1[0];

 

     i=0;
     j=0;
     while(str2[i]!=str1[0])
     str3[j++]=str2[i++];
     i++;
     j=0;
     while(str2[i]!='/0')
         str4[j++]=str2[i++];

     for(i=0;i<strlen(str3);i++)
         str5[i]=str1[i+1];
     for(j=0;j<strlen(str4);j++)
         str6[j]=str1[++i];

 

     postorder(str6,str4);
     postorder(str5,str3);

}

 

 

已知後序,中序,求先序:

#include<stdio.h>
#include<string.h>

 

#define MAX 20
char str[MAX]={'/0'};

 

int main()
{
     void postorder(char *str1,char *str2);
     char str1[MAX]={'/0'};
     char str2[MAX]={'/0'};
     int i;

 

     printf("please input the inorder:/n");
     scanf("%s%*c",str1); 
     printf("please input the postorder:/n");
     scanf("%s%*c",str2);

 

     postorder(str1,str2);

 

     printf("the preorder is:/n");
     puts(str);
}

void postorder(char *str1,char *str2)
{
      static num=0;
      int i,j,position;
      char str3[MAX]={'/0'};
      char str4[MAX]={'/0'};
      char str5[MAX]={'/0'};
      char str6[MAX]={'/0'}; 

 

     if(str1[0]=='/0'||str2[0]=='/0')
         return;

 

     position=strlen(str2)-1;
     str[num++]=str2[position];

 

     i=0;
     j=0;
     while(str1[i]!=str2[position])
         str3[j++]=str1[i++];
     i++;
     j=0;
     while(str1[i]!='/0')
         str4[j++]=str1[i++];

     for(i=0;i<strlen(str3);i++)
         str5[i]=str2[i];
     for(j=0;j<strlen(str4);j++)
         str6[j]=str2[i++];

 

     postorder(str3,str5);
     postorder(str4,str6);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.