Boring Homework
时间限制: 1 Sec 内存限制: 128 MB
题目描述
Professor Z. always gives his students lots of boring homework. Last week, after explaining binary search trees (BSTs), he asked his students to draw a picture of BST according to the list of numbers inserted into the tree sequentially. Maryanna spent so much time playing the game “Starcraft II” that she can’t finish her homework in time. She needs your help.
A binary search tree, which may sometimes also be called ordered or sorted binary tree, is a node-based binary tree data structure which has the following properties:
The left subtree of a node contains only nodes with keys less than the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
–from Wikipedia
To draw a picture of BST, you may follow the rules listed below:
1.The picture of a 1-node BST, whose size is 1*1, is a single ‘o’ (15th small Latin letter).
2.If a BST has a non-empty subtree, draw a single ‘|’ just above the subtree’s root, and a single ‘+’ just above previous drawn ‘|’. Finally, in the row of ‘+’, use the least number (including 0) of '-'s to connect ‘+’ (denoting the left subtree and right subtree) and ‘o’ (denoting the parent node of the subtree)
3.The left subtree (if exists) must be drawn on the left side of its parent. Similarly, the right subtree (if exists) must be drawn on the right side of its parent.
4.The column of the BST’s root must not contain any character from left subtree or right subtree.
5.Any column containing any characters from BST’s left subtree must not contain any characters from BST’s right subtree, and vice versa. That is, for a node of the BST, the picture of its left subtree and the picture of its right subtree do not share common columns in the picture of the whole tree.
The sample output may give a clear clarification about the format of the picture.
输入
The first line contains T (T <= 2500), the number of test cases. T lines follow. Each line contains a positive integer N (N < 80), followed by N integers - a permutation of 1 to N. The permutation indicates the insert order for the BST.
输出
For each test case:
Output the case number counting from 1 in the first line. The next lines should be the image described above without any trailing spaces. See the sample for more format details.
Notice that no trailing whitespaces after the last visible characters of each line are allowed.
样例输入
4
3 3 1 2
6 4 5 6 1 3 2
5 3 4 5 2 1
7 4 6 5 7 2 1 3
样例输出
Case #1:
+-*
|
*+
|
*
Case #2:
+--*+
| |
*-+ *+
| |
+* *
|
*
Case #3:
+*+
| |
+* *+
| |
* *
Case #4:
+-*-+
| |
+*+ +*+
| | | |
* * * *
#include<iostream>
#include <cstdio>
#include <set>
#include <queue>
#include <cstring>
typedef long long ll;
using namespace std;
typedef pair<int,int> pii;
set<int>vvv;
struct Node
{
int value,lson,rson,x,width;
Node()
{
value=lson=rson=x=width=0;
}
}tree[300];
queue<Node> que[2];
int cnt;
void insert(int root,int value) {
if (tree[root].lson == 0 && tree[root].rson == 0) {
tree[cnt].value = value;
tree[cnt].width = 1;
cnt++;
}
if (tree[root].value < value) {
if (tree[root].rson == 0) {
tree[cnt].value = value;
tree[cnt].width = 1;
tree[root].rson = cnt;
cnt++;
} else insert(tree[root].rson, value);
}
if (tree[root].value > value) {
if (tree[root].lson == 0) {
tree[cnt].value = value;
tree[cnt].width = 1;
tree[root].lson = cnt;
cnt++;
} else insert(tree[root].lson, value);
}
tree[root].width = tree[tree[root].lson].width + tree[tree[root].rson].width + 1;
}
int drewTree() {
while (!que[0].empty())
que[0].pop();
while (!que[1].empty())
que[1].pop();
int t = 0;
tree[1].x = tree[tree[1].lson].width + 1;
que[t].push(tree[1]);
int dep = 0;
int flag = 0;
while (!que[t].empty()) {
int tt = 1;
if (flag) {
auto a = vvv.end();
a--;
for (int i = 1; i <= *a; i++)
if (vvv.find(i) != vvv.end())
printf("|");
else printf(" ");
printf("\n");
} else flag = 1;
vvv.clear();
while (!que[t].empty()) {
Node fir = que[t].front();
que[t].pop();
if (fir.lson != 0) {
tree[fir.lson].x = fir.x - tree[tree[fir.lson].rson].width - 1;
while (tt < tree[fir.lson].x) {
printf(" ");
tt++;
}
printf("+");
tt++;
while (tt < fir.x) {
printf("-");
tt++;
}
vvv.insert(tree[fir.lson].x);
que[1 - t].push(tree[fir.lson]);
}
while (tt < fir.x) {
printf(" ");
tt++;
}
printf("*");
tt++;
if (fir.rson != 0) {
tree[fir.rson].x = fir.x + tree[tree[fir.rson].lson].width + 1;
while (tt < tree[fir.rson].x) {
printf("-");
tt++;
}
printf("+");
tt++;
vvv.insert(tree[fir.rson].x);
que[1 - t].push(tree[fir.rson]);
}
}
printf("\n");
dep += 2;
t = 1 - t;
}
return dep + 1;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
int t, n, cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%d", &n);
cnt = 1;
memset(tree, 0, sizeof tree);
for (int i = 0; i < 00; i++) {
tree[i].value = 0;
tree[i].lson = 0;
tree[i].rson = 0;
}
int tmp;
for (int i = 0; i < n; ++i) {
scanf("%d", &tmp);
insert(1, tmp);
}
printf("Case #%d:\n", cas++);
drewTree();
}
return 0;
}