View the graph write tree by using the image search function of the Apsara stack management framework (WordPress) 10562.

Source: Internet
Author: User

Question: The question is to give a Tree Graph composed of ASCII characters, and then convert it into a tree representation composed of character brackets. As you can know, if a tree has a child node, there will be a character '|' At the bottom, followed by a character sequence "-------" covering all child nodes, and then there will be a child node.

Idea: read all the data first. Then recursive processing, given a left and right boundary, checks characters one by one within the boundary. If it is a node character, determines whether there are children or not.

Note: because each line has spaces, scanf cannot be used for input. If it is replaced with fgets, there will be a line break (of course, it can be processed or determined later). Here, gets is used for reading, because it ensures that no overflow occurs.

In addition, after you change the value from scanf to gets, you must read the first integer and then discard the linefeed for getchar. It is found that this problem is easy to occur every time you change the input. Pay attention to this problem the next time you change the input. When using gets and scanf, pay attention to line breaks.

In addition, do not think that the string will be re-read every time, So memset Initialization is not required. If the number of new data lines is less than the old one, the residual data may still be affected, because you have checked H + 1 and H + 2 and did not judge whether the total number of rows has exceeded.

In addition, because the other characters in the array are '\ 0', a non-null character is added when determining whether it is a node character. (Of course, you can solve the problem of null characters by specifying a strict left and right range)

Tucao: Because the freopen is not commented out, WA is formed. We thought it was caused by adding not equal to null characters ~~

Recursion is a good thing. It has been written many times recently ~

Finally, I finished the binary tree theme ~ It feels a little faster than creating a linear table ~

Code:

# Include <stdio. h> # include <string. h> # define maxn 210 void DFS (int h, int left, int right); char tree [maxn] [maxn]; int main () {// freopen ("10562.in ", "r", stdin); // freopen ("10562.out"," W ", stdout); int t; scanf (" % d ", & T); getchar (); // The line break is discarded, not in the while .. While (t --> 0) {int HS = 0; memset (tree, 0, sizeof (tree); // do not think that if gets is input, it will not be memset, the old data may be affected. While (gets (tree [Hs])! = NULL & tree [Hs ++] [0]! = '#'); DFS (0, 0, maxn); printf ("\ n");} return 0;} void DFS (int h, int left, int right) {printf ("("); For (INT I = left; I <right; ++ I) {char c = tree [H] [I]; If (C! = '-' & C! = '|' & C! = ''& C! = '#' & C! = '\ 0') {// It is a node character printf ("% C", c); If (tree [H + 1] [I] =' | ') {// child int x = I-1; while (tree [H + 2] [x] = '-') x --; // X ++; // This statement is not required because the for loop is <right, not less than or equal to int y = I + 1; while (tree [H + 2] [Y] = '-') y ++; // y --; DFS (H + 3, x, y );} else printf ("()"); // printf ("% C :()", c) ;}// for printf (")");}


View the graph write tree by using the image search function of the Apsara stack management framework (WordPress) 10562.

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.