问题 问答题

[说明]
(1)对给定的字符集合及相应的权值,采用哈夫曼算法构造最优二叉树,并用结构数组存储最优二叉树。例如,给定字符集合{a,b,c,d)及其权值2、7、4、5,可构造如图3-26所示的最优二叉树,以及相应的结构数组Ht(如表3-12所示,其中数组元素Ht[0]不用)。

表3-12 结构数组Ht

数组下标chweightparentlchildrchild
1a2500
2b7700
3c4500
4d5600
56613
618026
7

结构数组Ht的类型定义如下:


(2)用“0”或“1”标识最优二叉树中分支的规则是:从一个结点进入其左(右)孩子结点,就用“0”(或“1”)标识该分支(示例见图3-26)。


(3)若用上述规则标识最优二叉树的每条分支后,从根结点开始到叶子结点为止,按经过分支的次序将相应标识依次排列,可得到由“0”、“1”组成的一个序列,称此序列为该叶子结点的前缀编码。例如图3-26所示的叶子结点a、b、c、d的前缀编码分别是110、0、111、10。
[函数说明1]
函数void LeafCode (int root,int n)的功能是:采用非递归方法,遍历最优二叉树的全部叶子结点,为所有的叶子结点构造前缀编码。其中,形参root为最优二叉树的根结点下标;形参n为叶子结点个数。
在函数void LeafCode (int root,int n)构造过程中,将Ht[p].weight域用做被遍历结点的遍历状态标志。
[函数4.1]



[函数说明2]
函数void Decode (char (作图)buff,int root)的功能是:将前缀编码序列翻译成叶子结点的字符序列,并输出。其中,形参root为最优二叉树的根结点下标;形参buff指向前缀编码序列。
[函数4.2]

答案

参考答案:

解析:这是一道要求读者在用哈夫曼算法构造的最优二叉树上进行编码和译码的程序设计题。本题的解答思路如下。 哈夫曼算法构造最优二叉树的过程如下。 ①根据给定的n个权值{W1,W2,W3,……Wn)构成n棵二叉树的集合F={T1,T2,T3,……Tn),其中每棵二叉树Ti中只有一个带权为Wi的根结点,其左、右子树均空。 ②在F中选取两棵根结点权值最小的树作为左、右子树构造一棵新的二叉树,且置新的二叉树的根结点的权值为其左、右子树根结点的权值之和。 ③在F中删除这两棵二叉树,同时将新得到的二叉树加入F中。 ④重复步骤②和③,直到F只含一棵树为止。这棵树便是最优二叉树。 综上所述,最优二叉树是从叶子到根构造起来的,每次都是先确定一棵二叉树的左、右子树,然后再构造出树根结点,因此最优二叉树中只有叶子结点和分支数为2的内部结点。若已知叶子的数目为n,则内部结点数比叶子少1,因此整棵树所需的存储空间规模是确定的,可以采用数组空间来存储最优二叉树。 例如,给定字符集合{a,b,c,d)及其权值2、7、4、5,构造最优二叉树的过程如图3-31所示。 由于算法中对构成左、右子树的二叉树不进行限定,因此用哈夫曼算法构造出的最优二叉树的形态不是惟一的。另外,题干中已给出了存放最优二叉树的结构数组Ht的类型定义,以及存储图3-31所构造出的最优二叉树的结构数组Ht(见表3-12)。

由于二叉树中的结点最多只有两个分支,若用“0”和“1”分别标识最优二叉树中的左子树分支和右子树分支,那么从根结点开始到叶子结点为止,按经过分支的次序将相应标识依次排列,可得到由“0”和“1”组成的一个序列,称此序列为该叶子结点的前缀编码。例如图3-26所示的最优二叉树叶子结点a、 b、c、d的前缀编码分别是110、0、111、10。 当最优二叉树的构造完成后,每个结点的weight域就可挪作他用,在构造哈夫曼编码的过程中,weight域用做被遍历结点的遍历状态标志。从树根出发,以非递归方式遍历最优二叉树的方法是:先沿着树根的左分支向叶子方向搜索,并用code[]记下所经过的分支的标识,同时用cdlen记录结点的路径长度,一直到叶子结点为止,即可得到当前正在访问的叶子的编码。然后,从该叶子结点回退到其父结点F。若刚才是从F的左子树回到F,则下一次应进入F的右子树进行遍历;若是从F的右子树回到F结点,则下一步应继续向F的父结点回退。 由以上分析可知,对于结点F,遍历过程中最多可能以3种不同的情况经过该结点,因此要为F结点的weight域赋予不同的值进行标识。初始时weight=0,当沿遍历路径到达该结点时其weight域值等于0,则进入其左子树分支进行遍历,并将weight置为1;若沿遍历路径到达该结点时其weight域值等于1,则说明刚从其左子树返回,下面应进入其右子树进行遍历并将weight置为2;若沿遍历路径到达该结点时其 weight域值等于2,则说明刚从其右子树返回,下面应继续向该结点的父结点回退,并将weight置为0。遍历路线如图3-32中箭头方向所示。

函数void LeafCode (int root,int n)的功能是:采用非递归方法,遍历最优二叉树的全部叶子结点,为所有的叶子结点构造前缀编码。由于在该函数(1)空缺处之后的语句“strcpy (Hc[p],code);”,是进行字符串的复制运算,则需要对源串中的串结束标志进行设置,因此(1)空缺处所填写的语句是“code[cdlen] =‘\0’”或“code[cdlen]=0”。 (2)空缺处是从右子树向父结点回退的处理,因此该空缺处所填入的内容是“Ht[p].parent”。由于每向上层回退一次,结点的路径长度就会减1,因此(3)空缺处所填写的语句是“--cdlen”或其等价形式。 函数void Decode (char (作图)buff,int root)的功能是:将前缀编码序列翻译成叶子结点的字符序列,并输出。译码的过程是:从根出发,若编码序列的当前字符是“0”,则进入左子树分支,否则进入右子树分支,直到到达一个叶子结点时为止,此时叶子所表示的字符就是翻译出的字符。若编码序列还没有结束,则重新从树根出发,重复上述过程,直到将编码序列结束。所以(4)空缺处所填写的语句是“(作图)buff==‘0’”或其等价形式。 由于到达一个叶子结点时,超前读入了一个编码序列中的字符,因此(5)空缺处所填写的语句是“buff--”或其等价形式。

问答题
阅读理解

When you practice reading with passages shorter than book length, do not try to take in each word separately, one after the other. It is much more difficult to grasp the broad theme of the passage this way, and you will also get the stuck on individual words which may not be absolutely essential to a general understanding of the passage. It is a good idea to skim through the passage very quickly first to get the general idea of each paragraph. Titles, paragraph headings and emphasized word can be a great help in getting this skeleton outline of the passage. It is surprising how many people do not read titles, introductions or paragraph headings. Can you, without looking back, remember the title of this passage and the heading of this paragraph?

Most paragraphs of a passage or chapter have a 'topic sentence' which expresses the central idea. The remaining sentence expand or support that idea. It has been estimated that between 60% and 90% of all expositive(说明的)paragraphs in English have the topic sentence first. Always pay special attention to the first sentence of a paragraph; it is most likely to give you the main idea.

Sometimes , though , the first sentence in the paragraph does not have the feel of 'main idea' sentence. It does not seem to give us enough new information to justify a paragraph. The next most likely place to look for the topic sentence is the last sentence of the paragraph.

Remember that the opening and closing paragraphs of a passage or chapter are particularly important . The opening paragraph suggests the general direction and content of the piece, while the closing paragraph often summarizes the very essence (精髓).

小题1:It is a good idea to skim through a passage quickly first ________.

A.at about 350 w. P.m.(words per minute)

B.to get the general idea of each paragraph

C.so that you can take in each word separately

D.to make sure you get to the end at least once小题2:The topic sentence of an expository paragraph in English_______.

A.usually comes in the middle

B.is most likely to be found at the end

C.is most often at the beginning

D.is usually left out in expository writing小题3:Most expository paragraphs in English have a clearly defined topic sentence. In such paragraphs the topic sentence comes first ________.

A.in about 40% of cases

B.in about 80% of cases

C.in about 20% cases

D.very rarely小题4:Some times we know the first sentence is not the topic sentence because ________.

A.it does not seem to give us enough new information

B.it is not long enough

C.it does not come at the beginning

D.it does not make complete sentence