当前位置:网站首页>A - trees on the level
A - trees on the level
2022-07-19 15:07:00 【zjsru_ Beginner】
author :myh
Title Description :
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics.
This problem involves building and traversing binary trees.
Given a sequence of binary trees, you are to write a pro-gram that prints a level-order traversal of each tree. In this problem each node of a binary tree contains a positive integer and all binary trees have have fewer than 256 nodes.
In a level-order traversal of a tree, the data in all nodes at a given level are printed in left-to-right order and all nodes at level k are printed before all nodes at level k + 1.
For example, a level order traversal of the tree on the right is: 5, 4, 8, 11, 13, 4, 7, 2, 1.
In this problem a binary tree is specified by a sequence of pairs ‘(n,s)’ where n is the value at the node whose path from the root is given by the string s. A path is given be a sequence of ‘L’s and ‘R’s where ‘L’ indicates a left branch and ‘R’ indicates a right branch. In the tree diagrammed above, the node containing 13 is specified by (13,RL), and the node containing 2 is specified by (2,LLR). The root node is specified by (5,) where the empty string indicates the path from the root to itself. A binary tree is considered to be completely specified if every node on all root-to-node paths in the tree is given a value exactly once.
Input
The input is a sequence of binary trees specified as described above. Each tree in a sequence consists of several pairs ‘(n,s)’ as described above separated by whitespace. The last entry in each tree is ‘()’. No whitespace appears between left and right parentheses. All nodes contain a positive integer. Every tree in the input will consist of at least one node and no more than 256 nodes. Input is terminated by end-of-file.
Output
For each completely specified binary tree in the input file, the level order traversal of that tree should be printed. If a tree is not completely specified, i.e., some node in the tree is NOT given a value or a node is given a value more than once, then the string ‘not complete’ should be printed.
Sample Input
(11,LL) (7,LLL) (8,R)
(5,) (4,L) (13,RL) (2,LLR) (1,RRR) (4,RR) ()
(3,L) (4,R) ()
Sample Output
5 4 8 11 13 4 7 2 1
not complete
Ideas :
Mainly used c++ STL Medium set. Sort the input by sorting , According to the length of characters and the dictionary order of the same length, from small to large , It just corresponds to the tree from top to bottom .
Be careful :
1. Some nodes cannot be reached from the root node
2. You need to clear the path when you start processing data
AC Code
#include<iostream>
#include<string>
#include<set>
#include<algorithm>
using namespace std;
struct Node
{
string v;
string path;
};
Node nodes[300];// most 256 Nodes
set<string>p_set; // Mainly used set Find quick features
int get_(string s)// Find the subscript of the comma , No return found -1
{
int i=0;
for (i=0; i<s.length(); i++)
{
if (s[i]==',')
{
return i;
}
}
return -1;
}
bool cmp(const Node& a, const Node& b)
{
if (a.path.length()!=b.path.length())
{
return a.path.length()<b.path.length();// The smaller one is in front
}
return a.path<b.path;// The dictionary order with the same length is smaller in the front
}
int main()
{
int flag=1;
int i=0,j;
string str;
p_set.clear();// Empty
while (cin >> str)
{
if (str!="()")
{
j=get_(str);
if (1)
{
nodes[i].v=str.substr(1,j-1);// utilize substr Function takes out numbers and paths
nodes[i].path=str.substr(j+1,str.length()-j-2);
if (p_set.find(nodes[i].path)!=p_set.end())
{
flag=0;// A node is given more than once
}
else
{
p_set.insert(nodes[i].path);
}
i++;
}
}
else// Start processing a set of data
{
if (flag==0)
{
cout << "not complete" <<endl;// A node is given more than once
}
else
{
sort(nodes, nodes+i, cmp);// Sort
p_set.clear();// Clear the path
if (nodes[0].path.length()==0)
{
p_set.insert(nodes[0].path);
for (j=1; j<i; j++)
{
if(p_set.find(nodes[j].path.substr(0,nodes[j].path.length()-1))==p_set.end())
{
flag=0;// Some nodes on the path from the root to a leaf node are not given
}
else
{
p_set.insert(nodes[j].path);
}
}
if (flag==0)
{
cout << "not complete" <<endl;// Some nodes on the path from the root to a leaf node are not given
}
else
{
for (j=0; j<i-1;j++)
{
cout <<nodes[j].v << " ";
}
cout <<nodes[j].v << endl;
}
}
else
{
cout << "not complete" <<endl;// No root node
}
}
i=0;
p_set.clear();// Clear the previous set of data
flag=1;
}
}
return 0;
}
边栏推荐
猜你喜欢
![[microservice] microservice learning note 3: use feign to replace resttemplate to complete remote call](/img/e6/b2f328a8e5ec3becdb9f934d041182.png)
[microservice] microservice learning note 3: use feign to replace resttemplate to complete remote call

1. Basic concepts of DBMS

08_服务熔断Hystrix

数据填报、报表展示哪家强?亿信ABI给你答案

6U VPX high speed signal processing board based on ku115+mpsoc (xcku115 + zu9eg +dsp)

Top domestic experts gathered in Guangzhou to discuss the safety application of health care data

Preview of authtalk phase I | comprehensive dismantling of multi tenant solutions

ORA-08103

One article, teach you to achieve single sign on

Natural language processing model of bigscience open source bloom
随机推荐
PKI: TLS handshake
009 execution sequence of SQL statement of interview questions
2. MySQL introduction
ICML2022 | 幾何多模態對比錶示學習
MySQL view
ORA-08103
[flask introduction series] request hook and context
MySQL index (I)
Code runner for vs code, with more than 40million downloads! Support more than 50 languages
Single channel 6Gsps 12 bit AD acquisition and single channel 6Gsps 16 bit Da (ad9176) output sub card based on vita57.1 standard
分布式事务总结
Leetcode 1296. Divide the array into a set of consecutive numbers (solved)
最大堆与堆排序和优先队列
见鬼,U盘空间怎么少了,原来是EFI分区搞的鬼,删除它
5-21 拦截器 Interceptor
Module 1 job
抽象類與派生類
Practice of tDesign in vitest
长安链学习研究-存储分析wal机制
dba