Example 6-7 Trees on the level, Uva122

Source: Internet
Author: User

Here are a few points to examine:

    1. Proficiency in Data entry
    2. Establishment of Binary tree
    3. Width-first traversal of binary tree

First of all, in particular, 1th, a considerable portion of the whole topic is time-consuming at the first Test point (although some unnecessary, because there should be a simpler approach). The input of this problem has the following options:

One-time input and direct access to the data to be obtained

Processing after input

For the first scenario, I'm using a scheme that combines a regular

scanf ("(%d%[,a-z])", &d,s))

It took a while to get this way. There are several difficulties, the most prominent is to consider the situation of data does not exist: such as (1,)

My solution is for (1,) read the back letter by passing ', ' to avoid the case of S being empty read chaos

For the decision of the () end tag, it is necessary to get the return value of scanf, which is 0, that is, the delegate reads ().

Also, it is important to know that this topic has multiple end tags, multiple sets of data,

while ((K=SCANF ("(%d%[,a-z)", &d,s)) >=0)

While executing the condition is >=0, note must include 0, and then in the loop to Judge K==0,k=0 is a set of data end of the tag, do not forget to add

scanf ("%*s");

It reads ")", as to why there is no front bracket that might have been the part of the parenthesis before%d was read.

On the second Test point, the establishment of a two-fork tree can be used to store nodes in a static array, or to dynamically establish nodes. Here can only be built with dynamic (data hit big!) )

The third one examines the width of the first traversal, using the queue to do the operation.

Enclosed is the AC code 1

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <iostream>
using namespace Std;
struct node{
Node *left;
Node *right;
int value;
Node (): Left (null), right (null) {}
};
Node * ROOT;
Node* NewNode () {
Node * t=new node ();
t->value=0;
return t;
}
int AddNode (int n, char *s) {
int len = strlen (s);
node* Node=root;
for (int i=0;i<len;i++) {
if (s[i] = = ' L ') {
if (Node->left = = NULL) {node->left = NewNode ();}
node = node->left;
}
else if (s[i] = = ' R ') {
if (node->right = = NULL) Node->right = NewNode ();
node = node->right;
}
}
if (!node->value) {Node->value=n;return 1;}
else return 0;
}

BOOL BFs (vector<int>& ans) {
Queue<node*> Q;
Ans.clear ();
Q.push (root);
while (!q.empty ()) {
node* u = Q.front (); Q.pop ();
if (!u->value) return false;
Ans.push_back (U->value);
if (u->left! = NULL) Q.push (u->left);
if (u->right! = NULL) Q.push (u->right);
}
return true;
}
void Remove_tree (node* u) {
if (U = = NULL) return;
Remove_tree (U->left);
Remove_tree (U->right);
Delete u;
}
int main () {
#ifdef DEBUG
Freopen ("6.7.in", "R", stdin);
Freopen ("6.7.out", "w", stdout);
#endif
Root=newnode ();
int d=-1;
int ok=1;
int k;
Char s[10]={0};
while ((K=SCANF ("(%d%[,a-z)", &d,s)) >=0) {
if (k==0) {
if (OK) {
Vector<int> ans;
if (BFS (ans)) {
int first=1;
for (Vector<int>::iterator it = Ans.begin (); It! = Ans.end (); ++it) {
if (!first) printf ("");
else first=0;
cout << *it;
}
printf ("\ n");

}
else ok=0;
}
if (!ok) printf ("Not complete\n");
ok=1;
Remove_tree (root);
Root=newnode ();
scanf ("%*s");
Continue
}
printf ("(%d%s) \n%d", d,s,k);
if (!ok) continue;
if (! AddNode (d,&s[1]) ok=0;
}


return 0;
}

Example 6-7 Trees on the level, Uva122

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.