无码av一区二区三区无码,在线观看老湿视频福利,日韩经典三级片,成 人色 网 站 欧美大片在线观看

歡迎光臨散文網(wǎng) 會(huì)員登陸 & 注冊(cè)

【Python】PAT 甲級(jí) A1147:Heaps(大小堆)

2021-02-16 20:25 作者:曉霧喵  | 我要投稿

題目?jī)?nèi)容

In computer science, a?heap?is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at?https://en.wikipedia.org/wiki/Heap_(data_structure))

Your job is to tell if a given complete binary tree is a heap.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers: M (?100), the number of trees to be tested; and N (1<N?1,000), the number of keys in each tree, respectively. Then M lines follow, each contains N distinct integer keys (all in the range of?int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:

For each given tree, print in a line?Max Heap?if it is a max heap, or?Min Heap?for a min heap, or?Not Heap?if it is not a heap at all. Then in the next line print the tree's postorder traversal sequence. All the numbers are separated by a space, and there must no extra space at the beginning or the end of the line.

Sample Input:

Sample Output:

題目要點(diǎn)

本題 30 分,考察的是堆的判定,并輸出堆的后序遍歷序列。

處理堆的問(wèn)題時(shí),首先要利用堆本身是完全二叉樹(shù)的性質(zhì)迅速定位左右孩子節(jié)點(diǎn)。根節(jié)點(diǎn)要從下標(biāo)1開(kāi)始存儲(chǔ),對(duì)于任意節(jié)點(diǎn)root來(lái)說(shuō),其左孩子下標(biāo)為2*root,右孩子下標(biāo)為2*root+1,父親下標(biāo)為root//2(向下取整)。有了這些結(jié)論就可以通過(guò)遍歷各個(gè)節(jié)點(diǎn)判斷是否符合堆的性質(zhì)。

如果完全二叉樹(shù)的所有孩子節(jié)點(diǎn)都比其父親節(jié)點(diǎn)小,那么這就是一個(gè)最大堆;反之,所有孩子節(jié)點(diǎn)都比父親節(jié)點(diǎn)大是最小堆。如果既不是最小堆、又不是最大堆,那么該完全二叉樹(shù)就不是堆。

源代碼


【Python】PAT 甲級(jí) A1147:Heaps(大小堆)的評(píng)論 (共 條)

分享到微博請(qǐng)遵守國(guó)家法律
乡城县| 枣阳市| 贺兰县| 吉安市| 海盐县| 蒙自县| 墨江| 通山县| 芮城县| 垣曲县| 望奎县| 德兴市| 宜州市| 施甸县| 龙江县| 南澳县| 江达县| 彰化县| 海门市| 赤壁市| 六盘水市| 杭锦后旗| 深州市| 郎溪县| 九江市| 潮州市| 中江县| 临沧市| 榕江县| 大渡口区| 普兰县| 和政县| 麻栗坡县| 河北区| 三原县| 砚山县| 淮安市| 临漳县| 民和| 安国市| 陈巴尔虎旗|