五月天青色头像情侣网名,国产亚洲av片在线观看18女人,黑人巨茎大战俄罗斯美女,扒下她的小内裤打屁股

歡迎光臨散文網(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ó)家法律
扎兰屯市| 巩留县| 托克逊县| 荣昌县| 中阳县| 沿河| 凌海市| 隆林| 托克托县| 普陀区| 买车| 东城区| 包头市| 四会市| 鹤庆县| 甘德县| 库尔勒市| 香港| 广灵县| 通化县| 庆元县| 兴隆县| 岢岚县| 库伦旗| 五台县| 瓦房店市| 金阳县| 佛冈县| 龙口市| 九寨沟县| 罗定市| 安仁县| 淳安县| 洪雅县| 海晏县| 四会市| 天等县| 微山县| 克东县| 扶沟县| 长子县|