HDU-1100 Trees Made to Order

Trees Made to Order

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)

Problem Description

We can number binary trees using the following scheme:
The empty tree is numbered 0.
The single-node tree is numbered 1.
All binary trees having m nodes have numbers less than all those having m+1 nodes.
Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either
Left subtrees numbered higher than L, or
A left subtree = L and a right subtree numbered higher than R.

The first 10 binary trees and tree number 20 in this sequence are shown below:

Alt

Your job for this problem is to output a binary tree when given its order number.

Input

Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)

Output

For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme:

A tree with no children should be output as X.
A tree with left and right subtrees L and R should be output as (L’)X(R’), where L’ and R’ are the representations of L and R.
If L is empty, just output X(R’).
If R is empty, just output (L’)X.

Sample Input

1
20
31117532
0

Sample Output

X
((X)X(X))X
(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)

Reference Code

#include <cstdio>
#include <cstring>
const int MAXN = 5e8 + 10;
int n;
int lst1[19] = {1,1,2,5,14,42,132,429,1430,4862,16796,58786,208012,
                742900,2674440,9694845,35357670,129644790,477638700};
int lst2[19] = {0,1,3,8,22,64,196,625,2055,6917,23713,82499,290511,
                1033411,3707851,13402696,48760366,178405156,656043856};
int count_node(int idx){
    for (int i = 1; i < 19; ++i)
        if (lst2[i] >= idx)
            return i;
}
int count_lson(int node_num, int &idx_diff){
    int sum = 0;
    for (int i = 0; i <= node_num; ++i){
        sum += lst1[i] * lst1[node_num - 1 - i];
        if (sum >= idx_diff){
            sum -= lst1[i] * lst1[node_num - 1 - i];
            idx_diff -= sum;
            return i;
        }
    }
}
void print_tree(int node_num, int idx_diff){
    int lson_num = count_lson(node_num, idx_diff);
    int rson_num = node_num - 1 - lson_num;
    if (lson_num){
        int l_idx_diff = (idx_diff + lst1[rson_num] - 1) / lst1[rson_num];
        printf("(");
        print_tree(lson_num, l_idx_diff);
        printf(")");
    }
    printf("X");
    if (rson_num){
        int r_idx_diff = (idx_diff - 1) % lst1[rson_num] + 1;
        printf("(");
        print_tree(rson_num, r_idx_diff);
        printf(")");
    }
}
int main(){
    while (~scanf("%d",&n) && n){
        int node_num = count_node(n);
        int idx_diff = n - lst2[node_num - 1];
        print_tree(node_num, idx_diff);
        printf("\n");
    }
    return 0;
}

Tips

题意:

给定一个二叉树的排序标准,然后输入一个序号,输出这个序号所对应的二叉树。
排序规则如下:

  1. 节点个数多的更“大”
  2. 节点个数一样,左子树高度高的更“大”
  3. 左子树相同,右子数高度高的更“大”

解法:

考虑左子树和右子数分别有多少节点。

若设节点数为 n n n 的二叉树共有 f ( n ) f(n) f(n) 个,则有
f ( n ) = ∑ i = 0 n − 1 f ( i ) f ( n − 1 − i ) f(n)=\sum_{i=0}^{n-1}f(i)f(n-1-i) f(n)=i=0n1f(i)f(n1i) f ( 0 ) = f ( 1 ) = 1 f(0)=f(1)=1 f(0)=f(1)=1

对于任给的一个编号 idx \text{idx} idx ,其对应二叉树的节点数量为 node_num = min ⁡ { x ∣ ∑ i = 1 x f ( i ) ⩾ idx } \text{node\_num}=\min\{x \vert \sum_{i=1}^{x}f(i)\geqslant\text{idx}\} node_num=min{xi=1xf(i)idx}

对于一个编号为 idx \text{idx} idx ,且有 node_num \text{node\_num} node_num 个节点的二叉树,其左子树含有的节点数量为 l_son_num = min ⁡ { x ∣ ∑ i = 0 x f ( i ) f ( node_num − 1 − i ) ⩾ idx_diff } \text{l\_son\_num}=\min\{x \vert \sum_{i=0}^{x}f(i)f(\text{node\_num}-1-i)\geqslant\text{idx\_diff}\} l_son_num=min{xi=0xf(i)f(node_num1i)idx_diff} 其中相对编号 idx_diff = idx − ∑ i = 1 node_num − 1 f ( i ) \text{idx\_diff}=\text{idx}-\sum_{i=1}^{\text{node\_num}-1}f(i) idx_diff=idxi=1node_num1f(i) 表示该二叉树在同节点数量的所有二叉树中排第几。

其右子数含有节点数量为 r_son_num = node_num − 1 − l_son_num \text{r\_son\_num}=\text{node\_num}-1-\text{l\_son\_num} r_son_num=node_num1l_son_num

若将两棵子树也看成如题所述的二叉树,则左子树的相对编号为 l_son_idx_diff = ⌈ idx_diff f ( r_son_num ) ⌉ \text{l\_son\_idx\_diff}=\left\lceil\frac{\text{idx\_diff}}{f(\text{r\_son\_num})}\right\rceil l_son_idx_diff=f(r_son_num)idx_diff右子数的相对编号为 r_son_idx_diff = [ ( idx_diff − 1 ) &VeryThinSpace; m o d &VeryThinSpace; f ( r_son_num ) ] + 1 \text{r\_son\_idx\_diff}=\left[(\text{idx\_diff}-1)\bmod f(\text{r\_son\_num})\right]+1 r_son_idx_diff=[(idx_diff1)modf(r_son_num)]+1

如此递归即可求出所得。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值