Establish a tree based on the first sequence and the middle sequence and obtain the latter sequence

Source: Internet
Author: User

// Create a tree based on the first sequence and the middle sequence and obtain the latter sequence

# Include <stdio. h>
# Include <malloc. h>
# Include <string. h>

Typedef struct tree
{
Char data;
TREE * lchild, * rchild;
} Tree, * stree;

// A --- first, B --- middle
Void create (stree & T, char * a, char * B, int num)
{
Int K;
Char * P = NULL;
If (num = 0)
T = NULL;
Else
{
P = B;
While (* P! = * A) P ++;
K = p-B;
T = (stree) malloc (sizeof (tree ));
T-> DATA = *;
// Start point of the first and middle orders, plus the number
Create (t-> lchild, A + 1, B, k );
Create (t-> rchild, A + k + 1, p + 1, num-k-1 );
}
}

Void postorder (stree T)
{
If (t)
{
Postorder (t-> lchild );
Postorder (t-> rchild );
Printf ("% C", T-> data );
}
}

Void main ()
{
Int Len;
Char A [20], B [20];
Stree T;
Printf ("Enter your first sequence:/N ");
Scanf ("% s", );
Printf ("Enter your ordinal sequence:/N ");
Scanf ("% s", B );
Len = strlen ();
Create (t, a, B, Len );
Printf ("post-order traversal:/N ");
Postorder (t );
Printf ("/N ");
}
/*
Enter your first sequence:
Abcdefg
Enter your ordinal sequence:
Cdbagf
The following is the post-order traversal:
Dcbgfea
Press any key to continue
*/

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.