PAT 1147 Heaps(30 分)
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:
3 8
98 72 86 60 65 12 23 50
8 38 25 58 52 82 70 60
10 28 15 12 34 9 8 56
Sample Output:
Max Heap
50 60 65 72 12 23 86 98
Min Heap
60 58 52 38 82 70 25 8
Not Heap
56 12 34 28 9 8 15 10
1 #include<iostream> 2 #include<vector> 3 #include<algorithm> 4 #include<stack> 5 using namespace std; 6 int n, m; 7 bool isHeap1(vector<int>& v){//判断大堆顶 8 int j = n/2; 9 while(j>0){ 10 if(v[j]>=v[j*2]){ 11 if(j*2+1<=n && v[j]>=v[j*2+1] || j*2+1>n) j--; 12 else return false; 13 } 14 else return false; 15 } 16 return true; 17 } 18 19 bool isHeap2(vector<int>& v){//判断小堆顶 20 int j=n/2; 21 while(j>0){ 22 if(v[j]<=v[j*2]){ 23 if(j*2+1<=n && v[j]<=v[j*2+1] || j*2+1>n) j--; 24 else return false; 25 } 26 else return false; 27 } 28 return true; 29 } 30 vector<int> vis(1001, false), v(1001); 31 void postOrder(){//非递归后序遍历 32 stack<int> s; 33 vector<int> post; 34 int idx; 35 s.push(1); 36 while(s.size()){ 37 idx = s.top(); 38 if(idx*2<=n && !vis[idx*2]){ 39 s.push(idx*2); 40 vis[idx*2] = true; 41 }else{ 42 post.push_back(v[s.top()]); 43 s.pop(); 44 if(s.size()){ 45 idx = s.top()*2+1; 46 if(idx<=n && !vis[idx]){ 47 s.push(idx); 48 vis[idx] = true; 49 } 50 } 51 } 52 } 53 for(int i=0; i<post.size(); i++){ 54 if(i==0) printf("%d", post[i]); 55 else printf(" %d", post[i]); 56 } 57 printf("\n"); 58 } 59 int main(){ 60 int i, j; 61 scanf("%d %d", &m, &n); 62 for(i=0; i<m; i++){ 63 for(j=1; j<=n; j++)scanf("%d", &v[j]); 64 bool flag; 65 fill(vis.begin(), vis.end(), false); 66 if(v[1]>=v[2] && v[1]>=v[3]){ 67 flag = isHeap1(v); 68 if(flag) printf("Max Heap\n"); 69 else printf("Not Heap\n"); 70 postOrder(); 71 }else { 72 flag = isHeap2(v); 73 if(flag) printf("Min Heap\n"); 74 else printf("Not Heap\n"); 75 postOrder(); 76 } 77 } 78 return 0; 79 }