Binary tree traversal (middle order, first order-> back order; Middle Order, later order-> first order)

Source: Internet
Author: User

I wrote two C language programs with functions such as questions. If you have any questions, please leave a message !!!

Development Platform: Linux; VIM; gcc

 

 

We know the first order, the middle order, and the back order:

# 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 );

}

 

 

After the known order, the middle order, the first order:

# 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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.