UVa Problem 122 - Trees on the level

来源:互联网 发布:淘宝女装商城 编辑:程序博客网 时间:2024/05/12 05:29
// UVa Problem 122 - Trees on the level// Verdict: Accepted// Submission Date: 2011-12-25// UVa Run Time: 0.008s//// 版权所有(C)2011,邱秋。metaphysis # yeah dot net//// [解题方法]// 本题可以归结为数据结构问题。步骤是建立树结构,检查是否完整,若完整则按深度输出节点。#include <iostream>#include <sstream>#include <cstring>using namespace std;#define TAG 0#define MAXN 256struct node{struct node *parent;struct node *childLeft, *childRight;int value;};bool noComplete, printWhitespace;int cache[MAXN][MAXN];int count[MAXN];void checkComplete(node * current){if (current->value == TAG)noComplete = true;if (current->childLeft != NULL)checkComplete(current->childLeft);if (current->childRight != NULL)checkComplete(current->childRight);}// 遍历树,检查叶子节点保存的路径和是否为目标值。void travelTree(node * current, int depth){cache[depth][count[depth]++] = current->value;if (current->childLeft != NULL)travelTree(current->childLeft, depth + 1);if (current->childRight != NULL)travelTree(current->childRight, depth + 1);}void clearTree(node * current){if (current->childLeft != NULL)clearTree(current->childLeft);if (current->childRight != NULL)clearTree(current->childRight);delete current;}int main(int argc, char const *argv[]){char c;node *root = new node;root->childLeft = NULL;root->childRight = NULL;root->value = TAG;while (cin >> c){if (c == ' ')continue;if (c == '('){string pairs;while (cin >> c, c != ')')pairs += c;if (pairs.length()){istringstream iss(pairs);int number;string positions;iss >> number >> c >> positions;node *current = root;for (int i = 0; i < positions.length(); i++){if (positions[i] == 'L'){if (current->childLeft == NULL){node *temp = new node;temp->value = TAG;temp->childLeft = NULL;temp->childRight = NULL;current->childLeft = temp;}current = current->childLeft;}else{if (current->childRight == NULL){node *temp = new node;temp->value = TAG;temp->childLeft = NULL;temp->childRight = NULL;current->childRight = temp;}current = current->childRight;}}if (current->value > 0)current->value = TAG;elsecurrent->value = number;}else{noComplete = false;checkComplete(root);if (noComplete)cout << "not complete\n";else{memset(count, 0, sizeof(count));travelTree(root, 0);printWhitespace = false;for (int i = 0; i < MAXN; i++)for (int j = 0; j < count[i]; j++){if (printWhitespace)cout << " ";elseprintWhitespace = true;cout << cache[i][j];}cout << endl;}clearTree(root);root = new node;root->childLeft = NULL;root->childRight = NULL;root->value = TAG;}}}return 0;}