Uva 122 Trees on the level

Source: Internet
Author: User

Test instructions: Read the data of XX, establish a binary tree, if the successful establishment of error-free, then output its sequence traversal.

Tell me about the deal.

The first is the input function. We read into a function, if the input is correct, for a set of data should be read to (), so the s== "()" as a set of data read into the end of the flag, return a true to the main function, the operation of the group of data, but if the input is complete, Should be read into empty so return a false to the main function.

When reading the contents of a node of a set of data, the value of the node is read out using sscanf (S[1], "%d", &v) . Then using strchr (S, ', ') to get the address of the first comma in the string s , then STRCHR (S, ', ') +1 is the starting address of the part of the string that represents the location of the node later.

So we know the value and position of a node, we can add a node, that is, execute the AddNode (v,postion) function.

Create a new node to point to the root node, based on the string representing postion s ' to continue to simulate, if s ' [i]==l into the left subtree, but first to determine whether Zuozi exists , does not exist then creates a new, and then into the left subtree. The right subtree is the same. When the content of S ' is read, it is the location of the node to be added, and its weight is changed to V.

Then the tree has been built, followed by sequence traversal, and the sequence traversal is handled using the queue.

The next thing to note is which factors in the process make the input error, and the output not complete

0. Re-enter data for a node, that is, in the AddNode function

if (U->vis) error=true;

1. The node that was removed has not been visited. (Because U->vis=true is recorded every time a node is established, it means that the node is only built, but there is no corresponding weight, that is, the input has a problem)

node* u =q.front (); Q.pop ();         if return false;

Also, be aware that each vector in the BFS function is re-emptied because it is a global variable

vector<int>ans;
Ans.clear ();

  

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5#include <queue>6#include <string.h>7 using namespacestd;8 Const intmaxn= -;9 BOOLerror;Tenvector<int>ans; One CharS[MAXN]; A structNode - { -     BOOLVis; the     intV// -Node *left, *Right ; -Node (): Vis (false), left (null), right (null) {} - }; +  -Node *Root; +Node *newnode () {return NewNode ();} A  at voidAddNodeintVChar*s) - { -     intn=strlen (s); -Node *u=Root; -      for(intI=0; i<n;i++) -     { in         if(s[i]=='L') -         { to             if(U->left = =NULL) +u->left=NewNode (); -U=u->Left ; the         } *         Else if(s[i]=='R') $         {Panax Notoginseng             if(u->right==NULL) -u->right=NewNode (); theU=u->Right ; +         } A     } the  +         if(U->vis) error=true; -u->v=v; $u->vis=true; $  -  - } the BOOLinput () - {Wuyi  theError=false; -root=NewNode (); Wu      for(;;) -     { About         if(SCANF ("%s", s)! =1)return false; $         if(!STRCMP (s),"()") ) Break; -         intv; -SSCANF (&s[1],"%d",&v); -AddNode (V, STRCHR (s),',')+1); A  +     } the     return true; - } $ BOOLBFS (vector<int>&ans) the { theQueue<node*>Q; the ans.clear (); the Q.push (root); -      while(!q.empty ()) in     { thenode* U =Q.front (); Q.pop (); the         if(!u->vis)return false;//**????? AboutAns.push_back (u->v); the         if(U->left! =NULL) theQ.push (u->Left ); the         if(U->right! =NULL) +Q.push (u->Right ); -     } the     return true;Bayi } the voidShow () the { -     intt=0; -      for(intI=0; I<ans.size (); i++) the     { the         if(i) printf (" "); theprintf"%d", Ans[i]); the     } -printf"\ n"); the ans.clear (); the } the intMain ()94 { the      while(Input ()) the     { the BFS (ans);98         if(error| |! BFS (ANS)) printf ("Not complete\n"); About         ElseShow (); -     }101     return 0;102 }103 /*104 ( 11,ll) (7,lll) (8,r) (5,) (4,l) (13,RL) (2,LLR) (1,RRR) (4,RR) () the ( 3,l) (4,r) ()106 */

Uva 122 Trees on the level

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.