Binary Tree stored in acmclub 1757 binary linked list-from lanshui_yang

Source: Internet
Author: User

Description:

In this article, we will provide a string that is traversed in the First Order. spaces represent empty subnodes, and uppercase letters represent node content. Create a binary tree using this string and output each non-empty node according to one of the first traversal and two middle Traversal Algorithms described in the topic.

The input format contains only one row, which contains a string S, used to create a binary tree. Ensure that S is a valid binary tree that traverses strings in sequence. The node content only has uppercase letters, and the length of S cannot exceed 100. The output consists of three lines. Each line contains a string of characters, indicating the node content obtained in the first, middle, and middle order respectively. Each letter is followed by a space. Note the line feed at the end of the line. Sample Input

ABC de G F

Sample output

A B C D E G F
C B e g d F
C B e g d F


Solution: there is a difficulty and a pitfall in this question! The difficulty is the problem of input and build. For specific solutions, see the program. There are two ways to solve the problem: direct recursion (relatively simple, not detailed); Type 2: manually call the stack (see the program if you need to pay attention to it !!)

The Program for direct recursive traversal is as follows:

# Include <cstdio> # include <string> # include <algorithm> # include <cstring> # include <cstdlib> # include <iostream> using namespace STD; const int maxn = 1111111; struct node {char data; int Xu; node * left; node * right; node (): Data ('\ 0'), Xu (0), left (null ), right (null) {};}; int flag; node * STAP [maxn]; int top; void creattree (node * & T) // note: input and build methods here {If (FLAG) return; char TMP; TMP = getchar (); If (TMP = '') {T = NULL ;} else if (TMP = '\ n') {flag = 1; return;} else {T = new node; t-> DATA = TMP; creattree (t-> left); creattree (t-> right) ;}} void initv (node * P) {If (P) {cout <p-> data <"; initv (p-> left); initv (p-> right) ;}} void midv (node * P) {If (p) {midv (p-> left); cout <p-> data <""; midv (p-> right) ;}} int main () {flag = 0; node * head; creattree (head); initv (head); cout <Endl; midv (head ); cout <Endl; return 0 ;}

Method 2: manually call the stack

# Include <cstdio> # include <string> # include <algorithm> # include <cstring> # include <cstdlib> # include <iostream> using namespace STD; const int maxn = 1111111; struct node {char data; int Xu; node * left; node * right; node (): Data ('\ 0'), Xu (0), left (null ), right (null) {};}; int flag; node * STAP [maxn]; int top; void creattree (node * & T) {If (FLAG) return; char TMP; TMP = getchar (); If (TMP = '') {T = NULL;} else if (TMP = '\ n') {flag = 1; return;} else {T = new node; t-> DATA = TMP; creattree (t-> left); creattree (t-> right) ;}} void initv (node * P) {If (P) {cout <p-> data <"; initv (p-> left); initv (p-> right) ;}} void mid2v (node * P) {Top =-1; if (p) STAP [++ top] = P; node * TMP; while (top> = 0) {TMP = STAP [Top]; while (TMP) {STAP [++ top] = TMP-> left; TMP = TMP -> Left;} top --; // unstack the NULL pointer. // Note: This is very important, the empty pointer to stack does not mean that the Left subtree of the node shown in the above program has been accessed. // It may also be the right subtree of the node shown below !! If (top> = 0) {TMP = STAP [top --]; cout <TMP-> data <""; STAP [++ top] = TMP-> right ;}} int main () {flag = 0; node * head; creattree (head); initv (head ); cout <Endl; mid2v (head); cout <Endl; mid2v (head); cout <Endl; return 0 ;}


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.