Uva122

Trees on the level UVA - 122

Trees are fundamental in many branches of computer science (Pun definitely intended). Current stateof-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 program that prints a level-order traversal of each tree. In this problemeachnodeofabinarytreecontainsapositiveinteger 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. Forexample,alevelordertraversalofthetreeontheright 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 specifiedby(2,LLR).Therootnodeisspecifiedby(5,)wheretheemptystringindicatesthepathfrom theroot to itself. A binary tree isconsidered to be completely specifiedifeverynode on allroot-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

 1 #include<bits/stdc++.h>
 2 const int maxn=1e5+5;
 3 #define LL long long
 4 using namespace std;
 5 //指针建树
 6 struct node
 7 {
 8  int haveValue;
 9  int v;
10  node *left,*right;
11  node():haveValue(false),left(NULL),right(NULL){}
12 };
13 node *root;
14 bool failed=false;
15 node* newnode()
16 {
17     return new node;
18 }
19 void addnode(int v,char *s)
20 {
21     node *u=root;
22     int len=strlen(s);
23     for(int i=0;i<len;i++)
24     {
25         if(s[i]=='L'){
26             if(u->left==NULL)
27             u->left=newnode();
28             u=u->left;
29         }
30         else if(s[i]=='R')
31         {
32             if(u->right==NULL)
33                 u->right=newnode();
34             u=u->right;
35         }
36     }
37         if(u->haveValue)
38             failed=true;
39     u->v=v;
40     u->haveValue=true;
41 }
42 vector<int>ans;
43 bool bfs(vector<int>&ans)
44 {
45     queue<node*>q;
46     ans.clear();
47     q.push(root);
48     while(!q.empty())
49     {
50         node* u=q.front();
51         q.pop();
52         if(u->haveValue==false){
53             failed=true;
54             return false;
55         }
56         ans.push_back(u->v);
57         if(u->left!=NULL)q.push(u->left);
58         if(u->right!=NULL)q.push(u->right);
59     }
60     return true;
61 }
62 
63 int main()
64 {
65     //freopen("zams.txt", "r", stdin);
66     //freopen("key.txt", "w", stdout);
67     while(1){
68         failed=false;
69         char s[maxn];
70         root=newnode();
71         while(1){
72         if(scanf("%s",s)!=1)return 0;
73         if(!strcmp(s,"()"))break;
74         int v;
75         sscanf(&s[1],"%d",&v);
76         addnode(v,strchr(s,',')+1);
77         }
78         bfs(ans);
79         if(failed)printf("not complete\n");
80         else
81         {
82             for(vector<int>::iterator it=ans.begin();it!=ans.end();it++)
83             {
84                 if(it!=ans.begin())printf(" ");
85                 printf("%d",*it);
86             }
87             printf("\n");
88         }
89     }
90     //fclose(stdin);
91     //fclose(stdout);
92     return 0;
93 }

思路:

指针建树,层次遍历

注意点:

sscanf(&s[1],"%d",&v);把s[1]及s[1]之后的字符串能转化为int的转化为整形。

strcmp(a,"as");比较两个字符串是否相等

strchr(a,',');

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5     //strchr(a,b)针对char数组,返回的是一个指针,指向包括b字符后的每个字符
 6     char s=',';
 7     char a[100]="as12,dsaf";
 8     char *b=strchr(a,s);
 9     cout<<b;//,dsaf
10     return 0;
11 }

 

转载于:https://www.cnblogs.com/zuiaimiusi/p/10991456.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值