POJ 1577

Falling Leaves

Problem Description

Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tree of letters, and go right to The problem.
A binary tree of letters may be one of two things:

  1. It may be empty.
  2. It may have a root node. A node has a letter as data and refers to a left and a right subtree. The left and right subtrees are also binary trees of letters. In the graphical representation of a binary tree of letters:
  3. Empty trees are omitted completely.
  4. Each node is indicated by
  • Its letter data,
  • A line segment down to the left to the left subtree, if the left subtree is nonempty,
  • A line segment down to the right to the right subtree, if the right subtree is nonempty.

A leaf in a binary tree is a node whose subtrees are both empty. In the example in Figure 1, this would be the five nodes with data B, D, H, P, and Y.
The preorder traversal of a tree of letters satisfies the defining properties:

  1. If the tree is empty, then the preorder traversal is empty.
  2. If the tree is not empty, then the preorder traversal consists of the following, in order
  • The data from the root node,
  • The preorder traversal of the root’s left subtree,
  • The preorder traversal of the root’s right subtree.

The preorder traversal of the tree in Figure 1 is KGCBDHQMPY.
A tree like the one in Figure 1 is also a binary search tree of letters. A binary search tree of letters is a binary tree of letters in which each node satisfies:
The root’s data comes later in the alphabet than all the data in the nodes in the left subtree.
The root’s data comes earlier in the alphabet than all the data in the nodes in the right subtree.
The problem:
Consider the following sequence of operations on a binary search tree of letters
Remove the leaves and list the data removed
Repeat this procedure until the tree is empty
Starting from the tree below on the left, we produce the sequence of trees shown, and then the empty tree by removing the leaves with data
BDHPY
CM
GQ
K
Your problem is to start with such a sequence of lines of leaves from a binary search tree of letters and output the preorder traversal of the tree.

Input

The input will contain one or more data sets. Each data set is a sequence of one or more lines of capital letters.
The lines contain the leaves removed from a binary search tree in the stages described above. The letters on a line will be listed in increasing alphabetical order. Data sets are separated by a line containing only an asterisk (‘*’).
The last data set is followed by a line containing only a dollar sign (‘$’). There are no blanks or empty lines in the input.

Output

For each input data set, there is a unique binary search tree that would produce the sequence of leaves. The output is a line containing only the preorder traversal of that tree, with no blanks.

Sample Input

BDHPY
CM
GQ
K
*
AC
B
$

Sample Output

KGCBDHQMPY
BAC

Source

Mid-Central USA 2000

源代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*就是逆着建BST,之后先序遍历就好了*/
/*读数据时先扔进栈里*/
/*然后边出栈,边建树*/
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int sum;
struct node
{
char s;
node *left;
node *right;
};
node *findmin(node *head)
{
if(head!=NULL)
{
while(head->left!=NULL)
{
head=head->left;
}
}
return head;
}
node *findmax(node *head)
{
if(head!=NULL)
{
while(head->right!=NULL)
{
head=head->right;
}
}
return head;
}
node *delet(node *head,char s)
{
node *temp;
if(head==NULL)
{
return 0;
}
if(s<head->s)
{
head->left=delet(head,s);
}
else
{
if(s>head->s)
{
head->right=delet(head,s);
}
else
{
if(head->left==NULL&&head->right==NULL)
{
delete head;
head=NULL;
}
else if(head->left)
{
for(temp=head->left; temp->right; temp=temp->right);
head->s=temp->s;
head->left=delet(head->left,head->s);
}
else
{
for(temp=head->right; temp->left; temp=temp->left);
head->s=temp->s;
head->right=delet(head->right,head->s);
}
}
}
return head;
}
node *inser(node *head,char s)
{
if(head==NULL)
{
head=new node;
head->s=s;
head->left=NULL;
head->right=NULL;
}
else
{
if(s<head->s)
{
head->left=inser(head->left,s);
}
else if(s>head->s)
{
head->right=inser(head->right,s);
}
else
{
return head;
}
}
return head;
}
void printff(node* head)
{

if(head==NULL)
{
return;
}
if(head->s>='A'&&head->s<='Z')printf("%c",head->s);
printff(head->left);
printff(head->right);
}
node *empt(node* head)
{
if(head!=NULL)
{
empt(head->left);
empt(head->right);
delete head;
}
return NULL;
}
int main()
{

char s[50],sb;
char ss[50];
int num;
num=0;
memset(ss,0,sizeof(ss));
int i=0;
while(scanf("%c",&s[i])!=EOF)
{
if(s[i]!='*'&&s[i]!='$'&&(s[i]>'Z'||s[i]<'A'))
continue;
if(s[i]=='*')
{
node *head;
head=new node;
head->right=NULL;
head->left=NULL;
i--;
for(; i>=0; i--)
{
inser(head,s[i]);
}
i=-1;
printff(head);
printf("\n");
}
else if(s[i]=='$')
{
i--;
node *head;
head=new node;
head->right=NULL;
head->left=NULL;
for(; i>=0; i--)
{
inser(head,s[i]);
}
i=-1;
printff(head);
printf("\n");
return 0;
}
i++;
}
return 0;
}