Background
Background
Trees are fundamental in your branches of computer science. 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 your algorithms in computer graphics.
Various trees (data structures) are the foundation of many computer branches. The fastest parallel computer, such as the CM-5 of the mind machine, is built on an architecture called fat trees. The Quadtree and the quadtree are many computer graphics.Algorithm.
This problem involves building and traversing Binary Trees.
This question is about the establishment and the binary tree of the same sample.
The Problem
Problem
Given a sequence of Binary Trees, you are to write a program 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 fewer than 256 nodes.
Given a binary tree sequence, you need to writeProgramAccess and print each tree according to the sequence. In this case, each section of a binary tree has a positive integer and the number of nodes in each tree is less than 256.
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.
When the sequence is a tree, all the knots in the specified row should be printed from left to right, and the k + 1 row can be printed only after the K row is printed.
For example, a level order traversal of the tree
For example, the result of the following tree in sequence examples is:
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.
In this problem, the binary tree is given by a series of binary groups (N, S), where N is the value of the node, and s is the path string from the root to the node. A path string consists of a series of "L" and "R". l represents the left subtree, and R represents the right subtree. In the binary tree shown in, nodes with a value of 13 are represented by (13, rl), and nodes with a value of 2 are represented by (2, LLR. The root node is represented by (5,), and its hollow path string represents the path from the root to the root. In a binary tree, if all the nodes in the path from the root to the node (given) have been given and only given once, this binary tree is considered to be a complete definition.
The input
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.
Input is a set of Binary Tree definitions according to the preceding descriptions. Each tree in the sequence contains several binary groups (N, S) separated by spaces. The last of each tree is (). No space exists between the left and right brackets.
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.
The values of all nodes are positive integers. Each input tree contains at least one node and no more than 256 nodes. The input is ended by EOF.
The output
Output
For each completely specified binary tree in the input file, the level order traversal of that tree shoshould 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" shocould be printed.
The Complete Binary Tree defined for each line of input must be repeated and printed according to the sequence. If a tree does not have a complete definition, that is, some nodes do not give its value, or one node repeatedly gives it, the string "not complete" should be printed ".
Sample Input
Input example
(11, LL) (7, lll) (8, R)
(5,) (4, L) (13, rl) (2, LLR) (1, rrr) (4, RR )()
(3, L) (4, R )()
Sample output
Output example
5 4 8 11 13 4 7 2 1
Not complete
Analysis
Analysis
This is a direct Binary Tree breadth example problem. You can solve it as required by the question. Note that the given binary group may have no numbers or path strings. These situations must be carefully handled. In addition, duplicate nodes and unspecified nodes are counted as input errors, and all nodes must output not complete. Before submitting your answers, make sure to test the situation by yourself. Otherwise, it is very likely that wa.
In addition, let's take a look at the extended examples. You need to create a linked list (to speed up the insert and delete operations) to store each layer of nodes. Add the root node to the linked list first, and then start to traverse each node in the linked list in sequence (from left to right. When a node is not empty, it is output and deleted, and its two subnodes are inserted in the original position, and the next node continues to the right. All nodes at the layer end of the examples, the nodes in the linked list are all nodes at the next layer, and the examples can be started from the beginning. Until the linked list is empty, the whole tree has ended. The algorithm time complexity is O (n ).
Solution
Answer
# Include <iostream> # include <list> # include <string> # include <sstream> using namespace STD; // node struct, value of the storage node and struct node {int nval; node * pl; node * PR;} // The nullnode is an empty node specimen const nullnode = {0, 0, 0}; // Delete the tree void deletetree (node * PHA) {If (PHA! = NULL) {// Delete the Left and Right subnodes, such as PPAR etree (pars-> PL) and deletetree (pars-> PR), and} Delete pars ;} // Add the node * addchild (node * PHA, int nval, const char * ppath) to the tree {// if the parent is empty, then, create the parent node if (PARs = 0) {pars = new node (nullnode);} // perform different operations on the switch (* ppath) according to the current path string) {// The right parenthesis indicates that the path is over and the current parent node is assigned with case ')': pars-> nval = (pars-> nval = 0 & nval! = 0 )? Nval:-1; break; // continue to create the left/right sub-node case 'l': PHA-> PL = addchild (PHA-> PL, nval, ppath + 1 ); break; Case 'r': PHA-> Pr = addchild (PHA-> PR, nval, ppath + 1); break;} return PHA ;} // main function int main (void) {// The root node of the tree * proot = 0; // cyclically process the data of each input node for (string strtoken; cin> strtoken;) {// get the node data string pointer const char * pstr = strtoken. c_str (); int nlen = strtoken. length (), nval = 0; // if the second character is not a right brace, add the node and enter if (pstr [1]! = ') {// Convert the string to a number for (; isdigit (* ++ pstr); nval = nval * 10 + * pstr-'0 '); if (* pstr! = ',') While (true); // Add the node proot = addchild (proot, nval, ++ pstr) in the tree; continue ;} // if the second character is a right brace, it means that the input of a tree is over, and the following examples are performed: List <node *> level (1, proot); stringstream ssresult. Use Level to store a layer of nodes. If level is not empty, continue to the example for (list <node * >:: iterator I = level. Begin ();! Level. Empty (); // if the end of a layer-by-layer example, return to the start point I = (I = level. End ()? Level. begin (): I) {// remove the current node * ptemp = * I; I = level. erase (I); // if this node is empty, continue directly to the next node if (ptemp = 0) {continue ;} // otherwise, add a subnode to the original position. // if the value of this node is smaller than or equal to 0, the subnode is repeatedly defined or if (ptemp-> nval <= 0) is not defined) {// clear the data and return an error. Level. clear (); ssresult. STR (""); break;} // correct node // enter its value, and insert its subnode ssresult <ptemp-> nval <''; I = level. insert (I, ptemp-> PL), ++ I; I = level. insert (I, ptemp-> PR), ++ I ;}// Delete the original tree to avoid Memory leakage. Deletetree (proot); proot = 0; strtoken = ssresult. STR (); // if the result is null, the given data is incorrect if (strtoken. empty () {strtoken = "not complete";} // remove the last space else {strtoken for the correct result. erase (strtoken. end ()-1);} // output result cout <strtoken <Endl;} return 0 ;}