跳到主要内容

東京大学 情報理工学系研究科 コンピュータ科学専攻 2017年8月実施 専門科目II 問題3

Author

kainoj, Zephyr, 祭音Myyura

Description

Let L(v)L(v) denote the set of leaves in the descendants of node vv in a tree, and let p(v,w)p(v, w) denote the number of edges of the simple path from node vv to node ww. For a non-leaf node vv, maxwL(v)p(v,w)\max_{w \in L(v)} p(v, w) is called the height of vv. Let the height of a leaf be 00. The height of the root of a tree is called the height of the tree.

Here, we have a binary tree TnT_n with height n0n \geq 0, in which each node vv must have one of the following properties:

  • vv is a leaf.
  • vv has only one child, and the height of vv is 11.
  • vv has two children, and the heights of the two children of vv differ by 11.

Let NnN_n denote the number of nodes in TnT_n for n0n \geq 0. Let r=1+52r = \frac{1 + \sqrt{5}}{2}.

Answer the following questions:

(1) Calculate N5N_5.

(2) Express NnN_n in terms of Nn1N_{n-1} and Nn2N_{n-2} for n2n \geq 2.

(3) Prove that NnrnN_n \geq r^n for every n0n \geq 0.

(4) Prove that Nnrn+2N_n \leq r^{n+2} for every n0n \geq 0.

(5) Consider the problem of assigning each of the given NnN_n integers to a distinct node of TnT_n. The integer assigned to each node vv must be no smaller than any of the integers assigned to vv's children. Show an O(rn)O(r^n) algorithm that computes such an assignment, with a proof that the algorithm runs indeed in O(rn)O(r^n) time. Note that the NnN_n integers may not be sorted in the input.

题目描述

在一棵树中,记 L(v)L(v) 为结点 vv 的后代叶结点集合,p(v,w)p(v,w) 为从 vvww 的简单路径所含边数。非叶结点 vv 的高度定义为 maxwL(v)p(v,w)\max_{w\in L(v)}p(v,w),叶结点的高度为 00,树根的高度称为树的高度。

给定高度为 n0n\ge0 的二叉树 TnT_n,其中每个结点 vv 必须满足以下三种情形之一:

  • vv 是叶结点;
  • vv 只有一个子结点,且 vv 的高度为 11
  • vv 有两个子结点,且两个子结点的高度相差 11

NnN_n 表示 TnT_n 的结点数,并令 r=(1+5)/2r=(1+\sqrt5)/2。回答下列问题。

(1)计算 N5N_5

(2)对 n2n\ge2,用 Nn1N_{n-1}Nn2N_{n-2} 表示 NnN_n

(3)证明对每个 n0n\ge0,都有 NnrnN_n\ge r^n

(4)证明对每个 n0n\ge0,都有 Nnrn+2N_n\le r^{n+2}

(5)现有 NnN_n 个未必已排序的整数,需要将它们一一分配给 TnT_n 的不同结点,并使每个结点上的整数都不小于其任一子结点上的整数。给出完成该分配的 O(rn)O(r^n) 算法,并证明其运行时间确为 O(rn)O(r^n)

Kai

Setting: TnT_n stands for AVL tree of height nn. Denote NnN_n for number of nodes in TnT_n.

(1)

2020

(2)

Nn=Nn1+Nn2+1N_n = N_{n-1} + N_{n-2} + 1

Bonus: note that Nn=Fibn+31N_n = \text{Fib}_{n+3} - 1 where Fib0=0\text{Fib}_0 = 0.

Proof goes by induction:

N(0)=1=21=Fib31,N(0) = 1 = 2 - 1 = \text{Fib}_3 - 1,
N(1)=2=31=Fib41.N(1) = 2 = 3 - 1 = \text{Fib}_4 - 1.

Inductive step:

Nn=Nn1+Nn2+1=(Fibn+21)+(Fibn+11)+1=Fibn+31N_n = N_{n-1} + N_{n-2} + 1 = (\text{Fib}_{n+2} - 1) + (\text{Fib}_{n+1} - 1) + 1 = \text{Fib}_{n+3}-1

(3)

Note that rn=rn1+rn2r^n = r^{n-1} + r^{n-2}. To see that start with:

r2=(1+52)2=1+52+1=r+1 r^2 = \left(\frac{1+\sqrt{5}}{2}\right)^2 = \frac{1+\sqrt{5}}{2} + 1 = r+1

and multiply it by rn2r^{n-2} both sides.

The rest goes by induction.

Base case: N0=1r0N_0 = 1 \geq r^0, N1=2>r1N_1 = 2 > r^1.

Inductive step:

Nn=Nn1+Nn2+1rn1+rn2=rnN_n = N_{n-1} + N_{n-2} + 1 \geq r^{n-1} + r^{n-2} = r^n

(4)

Put Mn=Nn+1M_n=N_n+1. Then M0=2r2M_0=2\leq r^2, M1=3r3M_1=3\leq r^3, and

Mn=Mn1+Mn2rn+1+rn=rn+2.M_n=M_{n-1}+M_{n-2}\leq r^{n+1}+r^n=r^{n+2}.

Thus Nn<Mnrn+2N_n<M_n\leq r^{n+2}.

(5)

Assign the integers arbitrarily, then process the nodes in postorder. At each node, repeatedly swap its value with the larger child while that child is larger. Since both child subtrees are already heaps, this produces the required max-heap order.

A sift at the root of a height-hh subtree costs O(h)O(h). Hence the total cost satisfies

Cn=Cn1+Cn2+O(n).C_n=C_{n-1}+C_{n-2}+O(n).

Unrolling this recurrence gives

Cn=O(j=0n(j+1)rnj)=O(rn),C_n=O\left(\sum_{j=0}^n (j+1)r^{n-j}\right) =O(r^n),

because j0(j+1)/rj\sum_{j\geq0}(j+1)/r^j converges.