五月天青色头像情侣网名,国产亚洲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ó)家法律
农安县| 阿尔山市| 靖宇县| 南康市| 马鞍山市| 昔阳县| 兴山县| 神农架林区| 兴文县| 全州县| 辽源市| 黄梅县| 昌乐县| 屯昌县| 溧水县| 吉首市| 大理市| 封开县| 鲜城| 凉山| 潞城市| 长治市| 自贡市| 祥云县| 扎兰屯市| 吉水县| 甘孜县| 永善县| 井冈山市| 宕昌县| 永定县| 磐安县| 安吉县| 德惠市| 鸡东县| 建平县| 密云县| 临漳县| 南丰县| 河津市| 嵊州市|