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))
One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.
Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line 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, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.
Finally 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.
Sample Input 1:
8
98 72 86 60 65 12 23 50
Sample Output 1:
98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap
Sample Input 2:
8
8 38 25 58 52 82 70 60
Sample Output 2:
8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap
Sample Input 3:
8
10 28 15 12 34 9 8 56
Sample Output 3:
10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap
題目大意:給出一個層序遍歷的完全二叉樹,要你打印所有到葉子節點的路徑(先右後左)+判斷這棵樹是大頂堆還是小頂堆還是根本就不是堆。
首先dfs打印路徑:可以用vector存儲路徑,也可以用一維數組path[]存儲。
1.vector存儲路徑(不用維護表示層數的變量level,但是要在dfs返回的時候彈出一個元素,也就是父結點)
void dfs(int root){
if(root>n)return;
path.push_back(heap[root]);
//如果是葉子節點就打印路徑
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
//先右後左
dfs(root*2+1,level+1);
dfs(root*2,level+1);
//彈出父結點
path.pop_back();
}
2.path數組存儲路徑
void dfs(int root,int level){
if(root>n)return;
path[level] = heap[root];
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
dfs(root*2+1,level+1);
dfs(root*2,level+1);
}
然後是堆排序時調整的函數downAdjust:其實這題可以直接用i/2表示父結點,與孩子結點i對比,就可以看出是什麼堆。但是爲了複習堆調整,還是把調整函數寫一遍,體會一下堆是怎麼調整的。
本題採用的思路是從n/2號結點開始向樹的上層遍歷(因爲從n/2號結點開始纔有孩子結點),每次調用downAdjusth函數,檢查該結點是否符合大頂堆or小頂堆的要求
//檢查是否是大頂堆
bool downAdjustMax(int low,int high){
//i一開始爲該結點編號,j爲其左孩子編號
//high爲總結點數
int i = low;
int j = i*2;
while(j<=high){
//如果右孩子比較大,就把右孩子編號賦值給j
if(j+1<=high&&heap[j]<heap[j+1]){
++j;
}
//如果不符合大頂堆要求,就return
if(heap[i]<heap[j])return false;
//否則接着向下檢查
else {
i = j;
j = i*2;
}
}
return true;
}
完整代碼:
#include <iostream>
#include <vector>
using namespace std;
int heap[1001],n;
vector<int> path;
//打印路徑
void dfs(int root,int level){
if(root>n)return;
path.push_back(heap[root]);
if(root*2>n&&root*2+1>n){
bool flag = false;
for(int i = 0;i<=level;i++){
if(flag==false){
cout<<path[i];
flag = true;
}
else{
cout<<" "<<path[i];
}
}
cout<<endl;
}
dfs(root*2+1,level+1);
dfs(root*2,level+1);
path.pop_back();
}
//檢查是否是大頂堆
bool downAdjustMax(int low,int high){
int i = low;
int j = i*2;
while(j<=high){
if(j+1<=high&&heap[j]<heap[j+1]){
++j;
}
if(heap[i]<heap[j])return false;
else {
i = j;
j = i*2;
}
}
return true;
}
//檢查是否是小頂堆
bool downAdjustMin(int low,int high){
int i = low;
int j = i*2;
while(j<=high){
if(j+1<=high&&heap[j]>heap[j+1]){
++j;
}
if(heap[i]>heap[j])return false;
else {
i = j;
j = i*2;
}
}
return true;
}
int main(){
cin>>n;
for(int i = 1;i<=n;i++){
cin>>heap[i];
}
dfs(1,0);
bool isMax= true,isMin = true;
//從n/2號結點到根節點遍歷檢查
for(int i = n/2;i>=1;i--){
if(downAdjustMax(i,n)==false)isMax = false;
}
if(isMax==false){
for(int i = n/2;i>=1;i--){
if(downAdjustMin(i,n)==false)isMin = false;
}
}
if(isMax)cout<<"Max Heap";
else if(isMin)cout<<"Min Heap";
else cout<<"Not Heap";
return 0;
}