Text Two-fork tree
View Submit Statistics Questions
Total time limit: 1000ms memory limit: 65536kB
Describe
As shown above, a two-fork tree in which each node is a letter, with different letters, can be represented by several lines of text:
A
-B
--*
--c
-D
--e
---*
---F
In several lines of text:
1 each letter represents a node. The letter is the first line in the text, which is called the line number of the node. Root in line 1th
2 The number of '-' characters to the left of each letter represents the level of the node in the tree (the root is on the No. 0 floor)
3 If a non-root node in the first layer in the text is located in the nth row, then its parent node is necessarily the node in the I-1 layer, the line number is less than n, and the line number and n is the smallest difference between the
4 If a text in the nth row of nodes (hierarchy is I) has two child nodes, then the N+1 row is its left child node, the right child node is n+1 row below the first level of i+1 node
5 If a node in layer I is in the text in the nth row, and it does not have the left child node and the right child node, then its next line is i+1 '-' character plus a ' * '
The text representation of a tree is given, which requires the output of the first order, the sequential sequence, and the middle order traversal result of the number.
Input
The first row is the number of trees n
Next is n trees, each tree ending with ' 0 '. ' 0 ' is not part of the tree
No more than 100 nodes per tree
Output
For each tree, the output of the first order, the sequential sequence and the middle order traversal result is three lines.
Separated by a blank line between the two trees
Sample input
2
A
-B
--*
--c
-D
--e
---*
---F
0
A
-B
-C
0
Sample output
ABCDEF
Cbfeda
Bcaefd
Abc
Bca
Bac
Source
Guo Wei
#include <iostream> using namespace std;
#include <stdio.h> #include <malloc.h> #include <string.h> #include <stdlib.h> #define Floor 10
typedef char ELEMTYPE;
typedef struct NODES {elemtype data; int D; Used to record about, 0 left 1 right int deepth;
Record level struct Nodes *lchild;
struct Nodes *rchild;
}btreenode;
int A (Btreenode *t) {printf ("%c", t->data);
if (t->lchild!= NULL) A (t->lchild);
if (t->rchild!= NULL) A (t->rchild);
return 0;
int B (Btreenode *t) {if (T->lchild!= NULL) b (t->lchild);
printf ("%c", t->data);
if (t->rchild!= NULL) B (t->rchild);
return 0;
int C (Btreenode *t) {if (T->lchild!= NULL) C (t->lchild);
if (t->rchild!= NULL) C (t->rchild);
printf ("%c", t->data);
return 0;
BOOL Textcreatetree (Btreenode *&h) {int top =-1;
Char Temp[floor]; Btreenode *staCK[101];
Btreenode *P1,*P2;
scanf ("%s", temp);
H = P1 = (Btreenode *) malloc (sizeof (Btreenode));
P1->data = Temp[strlen (temp)-1];
P1->d = 0;
P1->deepth = strlen (temp)-1;
P1->lchild = P1->rchild = NULL;
Stack[++top] = p1;
while (~SCANF ("%s", temp)) {if (Temp[strlen (temp)-1] = = ' 0 ') {break;
} P2 = (Btreenode *) malloc (sizeof (Btreenode));
P2->data = Temp[strlen (temp)-1];
P2->d = 0;
P2->deepth = strlen (temp)-1;
P2->lchild = P2->rchild = NULL;
P1 = Stack[top];
while (P2->deepth-p1->deepth!= 1) {top--;
P1 = Stack[top];
} if (P2->data = = ' * ') {p1->d++;
Continue
} if (p1->d = = 0) {P1->lchild = p2;
P1->d++;
else if (P1->d = = 1) {P1->rchild = p2;
P1->d++;
} if (P1->d = = 2) {top--;
} Stack[++top] = p2;
return 0; int main () {INT N;
scanf ("%d", &n);
GetChar ();
while (n--) {Btreenode *h;
Textcreatetree (h);
A (h);
printf ("\ n");
C (h);
printf ("\ n");
B (h);
printf ("\ n \ nthe");
return 0; }