Number of students in HDU Course Design Assessment

Source: Internet
Author: User

Description

The final assessment of the Data Structure Course Design of a school is coming soon. In order to examine students' understanding of the tree structure and test their programming ability, the school sets one item of the examination: write a program to print all the Binary Trees with a node number not less than m in order of numbers.
Numbering rules:

· The number of trees with only one node is 1.

· When the following conditions are met, the number of Binary Tree A is larger than that of B:

1. A has more nodes than B.

2. If the number of nodes of a is equal to that of B, and the left subtree number of a is larger than that of B.

3. The number of nodes of a and the number of left subtree are equal to that of B, and the number of right subtree of A is larger than that of B.

Nodes of a binary tree are represented in uppercase X, for example:

 

Of course, when m is large, it is also very heavy to check whether the answer is correct or not. Therefore, the teacher only wants to spot check on the Binary Trees of several numbers, compile a program to generate a standard answer for a binary tree numbered n.

Input

The input data is composed of multiple groups of data. Each group of data has only one integer, indicating the value of N (1 ≤ n ≤ 10 ^ 8. The input data ends with n = 0. Do not process the data.

Output

For each group of data, only one row is output, that is, the standard answer you have obtained.

The output format of a binary tree is:

(Left subtree) {omitted if left subtree is empty} X {root} (right subtree) {omitted if right subtree is empty}

{...} The content in is instructions and does not need to be output. For example, in a tree numbered 5, it can be represented as X (x) X); in a tree numbered 6, it is represented as (x) x (x ).

 

Sample Input

Sample output

20

0

 

(X) x

 

The main difficulty of this question is that if we construct a recursive method to output this image, we can first think of the catlan number for the binary tree type, in addition, the questions are first sorted by the number of Tree nodes. For example, if the question is given 20th, the first few of the qataran numbers are 1 2 5 14, so we know (1 + 2 + 5 <20 <1 + 2 + 5 + 14) the 20th trees consist of four nodes in the tree, so we need a function that accepts two parameters, one is the number of knots, and the other is to print the first several forms of trees, so when the input is 20, the first function is passed as (4, 20-1-2-5 ), after entering the tree, we need to enumerate the number of subtree knots on both sides to further determine the tree form. For example, input 20. The two parameters after entering the tree are 4, 12 so we can enumerate according to this rule. Of course, we guarantee that the sequence is small to large. Set the number of knots in the left and right subtree to (a, B) respectively, then (0, 3)-> (1, 2)-> (2, 1) -> (3, 0) (the root node itself occupies one node) by enumeration, we always ensure that the Left subtree has as few nodes as possible, that is, our enumeration is ordered, after enumerating one node, We need to subtract the previous configuration. When there is a negative number, we will know the numbers of the left and right nodes. The specific process is as follows: (H [x] indicates the catlan number, which indicates the number of tree trees under x nodes)

12 --> enumeration (0, 3) --> 12-H [0] * H [3] = 7

7 --> enumeration (1, 2) --> 7-H [1] * H [2] = 5

5 --> enumeration (2, 1) --> 5-H [2] * H [1] = 3

3 --> enumeration (3, 0) --> 3-H [3] * H [0] =-2

So we get that the configuration of the tree should be 3 to 0 on the left, and the next step is to calculate the configuration of the number of left and right sub-trees. Set the remaining model number to N and the number of knots in the left sub-tree to L, the number of knots in the right subtree is R, and the configuration of the Left subtree is:

No_l = Ceil (1.0 * n/H [R]);

No_r = n % H [R], if (N % H [R] = 0) no_r = H [R];

This formula can be introduced. The above rounded up indicates that when the weight of the Left subtree is less than 1, it must be an integer, the right subtree is directly linked to the remainder of N pairs of H [R.

The Code is as follows:

# Include <cstdlib> # include <cstring> # include <cstdio> # include <algorithm> # include <cmath> using namespace STD; typedef long int int64; int64 H [20]; int N; void pre () {H [0] = 1; for (INT I = 1; I <= 20; ++ I) {for (Int J = 0; j <= I; ++ J) {H [I] + = H [J] * H [i-j-1] ;}} void draw (INT tot, int no) // pass two parameters. One is the number of X required, and the other is the number of printed signs. {int T = No, L, R; for (INT I = 0; t> 0; ++ I) {T-= H [I] * H [tot-i-1]; If (T <= 0) {T + = H [I] * H [tot-i-1]; L = (INT) Ceil (1. * t/H [tot-i-1]); r = T % H [tot-i-1]; If (! R) r = H [tot-i-1]; if (I) {printf ("("); Draw (I, L); printf (")");} printf ("X"); If (tot-i-1) {printf ("); Draw (tot-i-1, R); printf (") ") ;}break ;}}} int main () {pre (); int t; while (scanf ("% d", & N), n) {T = N; For (INT I = 1; t> 0; ++ I) {T-= H [I]; If (T <= 0) {T + = H [I]; Draw (I, T ); break ;}} puts ("");} return 0 ;}

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.