# Blue Bridge Cup practice system previous questions transverse print binary tree

Source: Internet
Author: User

Problem description

Binary trees can be used for sorting. The principle is very simple: for a sort of binary tree to add a new node, the first comparison with the root node, if the small hand to Zuozi continue to deal with, otherwise to the right subtree.

When the empty tree is encountered, the node is placed in that position.

For example, 10 8 5 7 12 4 of the input sequence, should be built into a two-tree as shown in, where. Represents a blank.

...| -12
10-|
...| -8-|
.......| ...| -7
.......| -5-|
...........| -4

This topic requires: To establish a sort binary tree based on known numbers, and print the two-fork tree horizontally in the standard output.

Input format

The input data is n integers separated by a single line of space. N<100, each number does not exceed 10000.

There are no duplicate numbers in the input data.

Output format

Outputs the horizontal representation of the sorted binary tree. In order to facilitate the marking process compared to the number of spaces, please replace the space with a period:

Sample Input 110 5 20 sample output 1...| -20
10-|
...| -5 Example Input 25 10 20 8 4 7 Sample Output 2.......| -20
..| -10-|
..| ....| -8-|
..| ........| -7
5-|
..| -4 Ideas:

The first is test instructions, rotate the picture clockwise by 90°.
Then, to build a sort of binary tree without problems,
Output...
1. The middle-order traversal recursively takes a string of rows from each node to save, documenting the number of tiers in the current node in STR and the current node needs to expand the | position.
2. Recursively each node to its left and right sub-tree in the same layer +|.

Very good question about the binary tree .... I really like this recursion ..... t_t

Attached code:

`/* Blue Bridge cup transverse print binary tree first is test instructions, rotate the picture clockwise 90°. Then, set up sort binary tree without problem, output ... 1. The middle sequence traversal recursively saves the string that contains the row for each node, recording the number of tiers in the current node in STR and the current node needs to expand the | position. 2. Recursively each node to its left and right sub-tree layer +|. */#include <stdio.h> #include <algorithm> #include <iostream>using namespace Std;char str[30000 ];int cnt = 0;int Cntstr = 0;struct node {int val;//The value of the current node int lson, Rson;//left and right child node number int num;//current node in array number int tier; Indicates that the current node is a tier int ls in STR;        The current node should extend the | position node () {num =-1;        Lson =-1;        Rson =-1;        Tier =-1;    ls =-1; }}tree;void Add (int rt, int val, int num) {//parameter: The value added by the root node adds the ordinal of the value if (Tree[rt].val < val) {//to the right subtree if (Tree[rt].rson = =-1)            {Tree[rt].rson = num;            Tree[num].val = val;        Tree[num].num = num;    } else Add (Tree[rt].rson, Val, num);            } else {if (Tree[rt].lson = =-1) {Tree[rt].lson = num;            Tree[num].val = val;        Tree[num].num = num; } else add(Tree[rt].lson, Val, num);    }}void midsearch (int rt) {//middle order traversal if (tree[rt].rson! =-1) {midsearch (Tree[rt].rson);    } printf ("%d\n", tree[rt].val); if (Tree[rt].lson! =-1) midsearch (Tree[rt].lson);}    int getlen (int num) {int cnt = 0;        while (num) {num/= 10;    cnt++; } return CNT;}        void Printtostr (int rt, int len) {//The string that contains each node is output to the string str where Len represents the length of the previous determined if traversing to the current number if (Tree[rt].rson! =-1) {    Printtostr (Tree[rt].rson, Len + getlen (tree[rt].val)-1 + (RT = = 0? 2:4));    } for (int i=0; i<len; ++i) str[cntstr][i] = '. '; sprintf (Str[cntstr]+len, "%s%d%s", rt = = 0?) "\": "|", Tree[rt].val, ((Tree[rt].lson = =-1 && Tree[rt].rson = =-1)?    "\ n": "-|\n"));    Tree[rt].tier = Cntstr;    if (Tree[rt].lson! =-1 | | Tree[rt].rson! =-1) tree[rt].ls = Len + (Rt = = 0? 0:2) + Getlen (tree[rt].val) + 1;    cout << tree[rt].ls << "*\n";    cntstr++; if (Tree[rt].lson! =-1) {PRINTTOSTR (Tree[rt].lson, Len + getlen (tree[rt].val)-1 + (RT = = 0? 2:4));        }}void format (int rt) {///the | plus Len in the string represents the | position of the current node if (Tree[rt].rson! =-1) {format (Tree[rt].rson);        for (int i=tree[tree[rt].rson].tier; i<=tree[rt].tier; ++i) {str[i][tree[rt].ls] = ' | ';        }} if (Tree[rt].lson! =-1) {format (Tree[rt].lson);        for (int i=tree[rt].tier; i<=tree[tree[rt].lson].tier; ++i) {str[i][tree[rt].ls] = ' | ';    }}}int Main () {//Freopen ("1.in.cpp", "R", stdin);    int n;    scanf ("%d", &n); Tree.val = n;    The root node is 0 tree.num = 0; while (~SCANF ("%d", &n)) {Add (0, N, ++cnt);//Add n} printtostr (0, 0) to the binary tree;    A string corresponding to each node is added to STR in format (0);    for (int i=0; i<cntstr; ++i) {printf ("%s", Str[i]); } return 0;}`

Blue Bridge Cup practice system previous questions transverse print binary tree

Related Keywords:

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.