编程介的小学生 2017-09-18 10:00 采纳率: 20.5%
浏览 771
已采纳

Which is Next

Description

Every computer science student knows binary trees. Here is one of many possible definitions of binary trees. Binary trees are defined inductively. A binary tree t is either an external node (leaf) ○ or an ordered pair t = (t1, t2) representing an internal node ● with two subtrees attached, left subtree t1 and right subtree t2. Under this definition the number of nodes in any binary tree is odd. Given an odd integer n let B(n) denote the set of all binary trees with n nodes, both internal and external. For instance B(1) consists of only one tree ○, B(3) = {(○,○ )} and B(5) = {(○, (○,○ )), ((○,○ ),○ )}. The trees of B(5) are depicted in the figure below.


Denote by |t| the number of nodes in a tree t. Given a tree t we define its unique integer identifier N (t) as follows:

N (○) = 0
N (t1, t2) = 2|t1|+|t2| + 2|t2| * N(t1) +N (t2)
For instance, N (○,○) = 22 + 21 * 0 + 0 = 4, N (○, (○,○ )) = 24 + 23 * 0 + 4 = 20,
N ((○,○ ),○ ) = 24 + 21 * 4 + 0 = 24.

Consider the following linear order on all binary trees:

1) ○ < = t
2) (t1, t2) < = (u1, u2) when t1 < u1, or t1 = u1 and t2 < = u2

In this order a single leaf is the smallest tree and given two nonleaf trees, the smaller one is that with the smaller left tree, if the left subtrees are different, and that with the smaller right subtree, otherwise. Hence for instance (○, (○,○ )) < ((○,○ ),○ ), since we have ○ < (○,○ ). Assume now that the trees in B(n) were sorted using the relation < =. Then, for each tree t in B(n) we define the successor of t as the tree that immediately follows t in B(n). If t is the largest one in B(n) then the successor of t is the smallest tree in set B(n). For instance, the successor of (○,○ ) in B(3) is the same tree (○, ○) and the successor of (○, (○,○ )) in B(5) is ((○,○ ),○ ). Given the integer identifier of some tree t can you give the identifier of the successor of t in B(|t|)?

Task
Write a program that:

reads the identifier of some binary tree t,
computes the identifier of the successor of t in B(|t|),
writes the result.
Input

The first and only line of the input contains one integer n (0 <= n < = 230) - the identifier of some binary tree
Output

The first and only line of the output should contain one integer s - the identifier of the successor of t in B(|t|).
Sample Input

20
Sample Output

24

  • 写回答

1条回答 默认 最新

  • threenewbee 2017-10-02 15:47
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛