HDU 3791 Binary Search Tree

Source: Internet
Author: User
HDU 3791 binary search tree determines whether the two sequences are the same binary search tree Sequence

Input starts with n. (1 <= n <= 20) indicates that N records need to be determined. When n = 0, the input ends.
The next row is a sequence with a length less than 10 and contains (0 ~ 9). There are no repeated numbers. Based on this sequence, a binary search tree can be constructed.
The next n rows have N sequences. The format of each sequence is the same as that of the first sequence. Determine whether the two sequences can form the same binary search tree. If the output sequence is the same, yes is output; otherwise, no is output.
Sample Input

25674325432675763420

Sample output

YESNO

Solution:
This topic requires that a binary search tree be created with the first string, followed by N strings in each row.
The Set binary search tree is the same as the tree created by the first string and outputs yes. Otherwise, no is output.
You can design an input function to record each input string and use the input/output control class.
Istringstream creates an object to bind the string we recorded and simulates the input operation of the C ++ style stream.
Add it to a container for creation (of course, you can directly traverse the record string ). Improve the first-order traversal Function
Record the first-order traversal of the tree to a string, and then each first-order traversal string and the first-order traversal character of the first input tree
Strings can be compared.

1 # include <bits/stdc ++. h> 2 using namespace STD; 3 typedef char typedata; 4 struct node {5 typedata data; 6 node * leftchild; 7 node * rightchild; 8 node () {// constructor 9 leftchild = NULL; 10 rightchild = NULL; 11} 12}; 13 vector <typedata> data; // data records each element of the input string 14 string text, pattern; 15 // text is used to record the forward traversal of the Binary Search Tree created by the first string 16 // After the pattern record, the forward traversal of the Binary Search Tree created by each line of input 17 void input () {18 string temp1, temp2; // The word entered in the temp record String 19 typedata num; 20 temp2 = ""; 21 data. clear (); // clear the container for each build, and clear the temp2 string 22 CIN> temp1; 23 24 for (string: iterator it = temp1.begin (); it! = Temp1.end (); It ++) {25 if (it! = Temp1.begin () {26 temp2 + = ''; 27} // separate each element in temp1 with spaces and record 28 temp2 + = * It In temp2; 29} 30 istringstream cin_line (temp2); // istringstream binds temp231 while (cin_line> num) {32 data. push_back (Num); // Add the record value to container 33} 34} 35 void insertbst (node * & root, typedata X) {// Binary Search Tree insertion function 36 // note that the function needs to be inserted. The root node must reference 37 If (root = NULL) {// locate an empty position, even if the inserted position is 38 root = new node (); // The new node has the permission of x39 root-> DATA = x; 40 return; 41} 42 if (x = Ro Ot-> data) {// The 43 return; 44} else if (root-> DATA> X) is returned if the node to be inserted already exists) {// X is smaller than the data field of the root node, which must be inserted in the left subtree 45 insertbst (root-> leftchild, X ); // search the left subtree for 46} else if (root-> data <X) {// X is larger than the root node data field and needs to be inserted in the right subtree 47 insertbst (root-> rightchild, x); // search for 48} 49} 50 node * createbst () {// build 51 node * root = NULL; // create a node root52 for (vector <typedata>: iterator it = data. begin (); it! = Data. end (); It ++) {53 insertbst (root, * It); // Insert the processed value in the container into the Binary Search Tree 54} 55 return root; // return the root node 56} 57 void preorder (node * root, string & Str) {58 // improve the first-order traversal function and record the first-order traversal in Str, because you want to modify the STR, pass the reference 59 If (root = NULL) // reach the empty tree and set the recursive boundary 60 return; 61 STR + = root-> data; // access the root node to record data to str62 preorder (root-> leftchild, STR); // access the left subtree 63 preorder (root-> rightchild, STR ); // access the right subtree 64} 65 int main () 66 {67 int N; 68 while (CIN> N) {// multiple groups of test data in this question 69 If (n = 0) // If the input N is 0, jump out of the loop 70 break; 71 input (); // input string 72 node * root = createbst (); // build 73 text = ""; // clear text74 preorder (root, text ); // text records the first Binary Search Tree 75 while (n --) {// input n strings to build 76 input (); 77 node * root = createbst (); 78 pattern = ""; 79 preorder (root, pattern); 80 If (text = pattern) 81 cout <"yes" <Endl; 82 else83 cout <"no" <Endl; 84 85} 86} 87 return 0; 88}

 

HDU 3791 Binary Search Tree

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.