Description
As the diagramshown above, every node is represented by a letter in the binary tree and theletters are different from each other. It can be represented below.
A
-B
–*
–C
-D
–E
—*
—F
In the text:
1) Every letterrepresented a node. The line number of the node is the same as the line numberof the letter. The root is at the first line.
2) The number ofcharacter ‘-‘ at the left side of the letter represents the level of the node.(The level of the root is 0)
3) If the linenumber of anode, which is not root and the level of which is i, is n, the levelof its father node is i-1, the line number of its father is less than n and itsfather is the one with the smallest gap between its line number and n.
4) If a node,whose level is i and line number is n, has two child nodes, its left child nodeis the one in line n+1 and its right child node is the first one at level i+1in those with line number more than n+1.
5) If a node,whose level is i, is in line n has left child node and doesn’t have right childnode, the next line of which is i+1 character ‘-‘ and a character ‘*’.
Givena tree in text, please output the preorder, post order and infix ordertraversal.
Input
The fist line is the number of trees n,
followed by n trees and the end of each one is ‘0’. The ‘0’ is not a part of the tree.
Every tree has no more than 100 nodes.
Output
Output three lines for every tree: the preorder, post order and infix order results.
There is an empty line between trees.
Sample Input
2
A
-B
–*
–C
-D
–E
—*
—F
0
A
-B
-C
0
Sample Output
ABCDEF
CBFEDA
BCAEFDABC
BCA
BAC
分析:
重点的是子节点的左右分配判断;
另外,根节点最好单独输入;
代码:
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
struct Node{
Node *l,*r,*f;
char content;
int len;
int lr;//0代表左子树未赋值,1代表已经赋值
Node():l(NULL),r(NULL),f(NULL),len(0),lr(0){}
};
void input_tree(Node * last_node)
{
char line[20] ;
cin >> line;
int len = strlen(line);
if(line[len-1] == '0') return; //返回条件
while(last_node->len != len-1) last_node = last_node->f;
if(last_node->lr == 0){ //左子树未赋值
last_node->l = new Node();
last_node->l->f = last_node;
last_node->l->len = len;
last_node->l->content = line[len-1];
last_node->lr = 1;
input_tree(last_node->l);
}else if(last_node->lr == 1){ //左子树已赋值
last_node->r = new Node();
last_node->r->f = last_node;
last_node->r->len = len;
last_node->r->content = line[len-1];
last_node->lr = 3;//这句不写也行
input_tree(last_node->r);
}
}
void pre_order(Node *root)
{
if(root == NULL || root->content == '*') return;
cout << root->content;
pre_order(root->l);
pre_order(root->r);
}
void in_order(Node *root)
{
if(root == NULL || root->content == '*') return;
in_order(root->l);
cout << root->content;
in_order(root->r);
}
void post_order(Node *root)
{
if(root == NULL || root->content == '*') return;
post_order(root->l);
post_order(root->r);
cout << root->content;
}
void del_tree(Node *root)
{
if(root == NULL) return;
del_tree(root->l);
del_tree(root->r);
delete root;
}
void output_tree(Node *root)
{
pre_order(root);printf("\n");
post_order(root);printf("\n");
in_order(root);printf("\n");
}
int main()
{
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
int tree_num;
cin >> tree_num;
for (int i = 0; i < tree_num; i += 1){
Node *root = new Node();
cin >> root->content;
root->len = 1;
input_tree(root);
output_tree(root);
del_tree(root);
if(i != tree_num-1) printf("\n");
}
return 0;
}