広島大学 先進理工系科学研究科 情報科学プログラム 2019年8月実施 専門科目II 問題2
Author
samparker, 祭音Myyura
Description
子の数に制約のない根付き木を 分木によって表す方法として、左子・右兄弟表現がある。 2 分木の各節点 は、キー の他に、親 と左端の子 、すぐ右の兄弟 の3つのポインタを持つ。 親や左端の子、すぐ右の兄弟がいない節点では、それぞれのポインタ の値は とする。 例えば、図 1 (a) の根付き木を上述の表現の二分木で表すと図 1 (b) のようになる。
(1) 根付き木の節点の数を 、枝の数を とする。 と の関係を書け。
(2) 図 2 の根付き木を、左子・右兄弟表現の 2 分木で表せ。ポインタ は省略せよ。
(3) 根付き木の根 が与えられたとき、一番左下の子孫の を出力する手続きの擬似コード Print-Leftmost() を示す。空欄 と を適切に埋めよ。
(4) 根付き木の根 が与えられたとき、すべての節点の を出力する手続き Tree-Walk() を擬似コードで書け。ただし、節点を出力する順序は問わない。
(5) (4) の手続き Tree-Walk() の時間計算量をオーダー表記で書け。
There is the left-child, right-sibling representation that uses a binary tree to represent a rooted tree with arbitrary number of children. Each vertex of the binary tree contains a key , a parent pointer , a pointer to the leftmost child , and a pointer to the sibling immediately to its right . We assume that, in case of no parent, no child, or no sibling, the value of the pointer or is null, respectively. For example, a rooted tree shown in Fig. 1 (a) is represented by a binary tree of the above representation as shown in Fig. 1 (b).
(1) We assume that a rooted tree has vertices and edges. Describe the relationship between and .
(2) Draw a binary tree of the left-child, right-sibling representation, to represent a rooted tree shown in Fig. 2. The pointer should not be drawn.
(3) The pseudo code Print-Leftmost() is a procedure that prints the key of the leftmost descendant of a tree rooted at a given node . Fill the blanks and .
(4) Write a pseudo code of the procedure Tree-Walk() that prints keys of all vertices in a tree rooted at a given node . The order of vertices to print is not the matter.
(5) Show the execution time of Tree-Walk() using big-O notation.
题目描述
用二叉树表示子节点数不受限制的有根树时,可采用“左孩子—右兄弟”表示法。二叉树中的每个节点 除键值 外,还含有三个指针:父节点 、最左孩子 和紧邻的右兄弟 ;相应节点不存在时,对应指针值为 。图 1(a) 与图 1(b) 给出了普通有根树及其这种二叉树表示的示例。
- 若有根树有 个节点、 条边,写出 与 的关系。
- 将图 2 中的有根树画成左孩子—右兄弟表示的二叉树,省略父指针 。
- 给定有根树的根 ,伪代码
Print-Leftmost(x)输出最左下方后代的key;填写图中的空白 、。 - 编写伪代码
Tree-Walk(x),输出以 为根的树中所有节点的key,输出顺序不限。 - 用大 记号写出
Tree-Walk(x)的运行时间。
相关树结构、伪代码空白和待转换的树均见图 1、图 2。
考点
- 树形数据结构:理解左孩子—右兄弟表示法,完成指针遍历伪代码,并按节点数分析整棵树遍历的复杂度。
Kai
(1)
(2)
2
/
5
/ \
13 8
/ \
16 10
\ /
18 25
\ \
21 27
(3)
(4)
Tree-Walk(x)
if (x != null) then
print(x.key)
Tree-Walk(x.lc)
Tree-Walk(x.rs)