東京大学 情報理工学系研究科 コンピュータ科学専攻 2017年8月実施 専門科目II 問題3
Author
Description
Let denote the set of leaves in the descendants of node in a tree, and let denote the number of edges of the simple path from node to node . For a non-leaf node , is called the height of . Let the height of a leaf be . The height of the root of a tree is called the height of the tree.
Here, we have a binary tree with height , in which each node must have one of the following properties:
- is a leaf.
- has only one child, and the height of is .
- has two children, and the heights of the two children of differ by .
Let denote the number of nodes in for . Let .
Answer the following questions:
(1) Calculate .
(2) Express in terms of and for .
(3) Prove that for every .
(4) Prove that for every .
(5) Consider the problem of assigning each of the given integers to a distinct node of . The integer assigned to each node must be no smaller than any of the integers assigned to 's children. Show an algorithm that computes such an assignment, with a proof that the algorithm runs indeed in time. Note that the integers may not be sorted in the input.
题目描述
在一棵树中,记 为结点 的后代叶结点集合, 为从 到 的简单路径所含边数。非叶结点 的高度定义为 ,叶结点的高度为 ,树根的高度称为树的高度。
给定高度为 的二叉树 ,其中每个结点 必须满足以下三种情形之一:
- 是叶结点;
- 只有一个子结点,且 的高度为 ;
- 有两个子结点,且两个子结点的高度相差 。
令 表示 的结点数,并令 。回答下列问题。
(1)计算 。
(2)对 ,用 和 表示 。
(3)证明对每个 ,都有 。
(4)证明对每个 ,都有 。
(5)现有 个未必已排序的整数,需要将它们一一分配给 的不同结点,并使每个结点上的整数都不小于其任一子结点上的整数。给出完成该分配的 算法,并证明其运行时间确为 。
考点
- 高度平衡二叉树的最少结点数:由两个子树高度相差一建立结点数递推。
- 斐波那契型递推与黄金比界:利用 和归纳法证明 的上下界。
- 线性时间建堆:在树结构上构造满足父结点不小于子结点的赋值,并结合 证明复杂度。
Kai
Setting: stands for AVL tree of height . Denote for number of nodes in .
(1)
(2)
Bonus: note that where .
Proof goes by induction:
Inductive step:
(3)
Note that . To see that start with:
and multiply it by both sides.
The rest goes by induction.
Base case: , .
Inductive step:
(4)
Again, by induction.
Base: tree of height has nodes.
Induction:
(5)
We are tasked with constructing an AVL tree of height while assigning integers to each node such that the AVL properties are preserved. By precomputing subtree sizes using dynamic programming, the assignment process can be streamlined.
Algorithm Steps:
Precompute Subtree Sizes: Use dynamic programming to calculate the sizes of all subtrees up to height . Define to store the size of a tree of height :
This step runs in time.
Main Function fillTree(arr, h):
- Base case: if , return a tree with a single node containing the first element of
arr. - Calculate the sizes for left and right subtrees using the precomputed :
- Use
quickSelectto find the(leftSize+1)thsmallest element inarrto be the root. - Recursively fill the left subtree with the smallest
leftSizeelements, and the right subtree with the largestrightSizeelements. - Return the tree.
QuickSelect: This algorithm finds the kth smallest element in an array in average time. It uses a pivot to partition the array and recursively selects the kth element from the appropriate partition.
To rigorously analyze the time complexity of constructing the AVL tree, we use the Akra-Bazzi Theorem, which is a generalization of the Master Theorem and is more suitable for handling recurrences with multiple recursive branches and non-uniform problem sizes.
The time complexity for constructing an AVL tree of height satisfies the following recurrence relation:
Determine for the Akra-Bazzi Theorem:
Hence we have