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也只是知道名字,所以只能看着别人的代码来反向研究,记录下我今天思考的成果吧。希望以后可以有所进步,掌握这两个知识。代码来源:https://blog.youkuaiyun.com/liuchuo/article/details/84973009
这道题目其实完全不需要堆排序的相关知识,甚至你只需要知道名字。难点其实在于,对于二叉树的各个节点的打印。给出的数据是按照二叉树的层序来的,由于给出的是完全二叉树,所以也不需要再去建树等一些复杂的想法。直接通过下标去访问左右孩子即可。如何去打印节点?这个问题需要用DFS的思想去解决。将所有节点值存在数组中,由于是利用下标,所以要从1开始(0怎么乘都是0)函数的调用便是dfs(1).解决完调用后,下一个问题便是对dfs函数内部完善,dfs函数代码如下:
void dfs(int index)
{
if((index*2>n) &&(index*2+1>n))
{
if(index<=n)
{
for(int i=0; i<v.size(); i++)
{
cout<<v[i];
if(i!=v.size()-1)
{
cout<<" ";
}
else if(i==v.size()-1)
{
cout<<endl;
}
}
}
}
else
{
v.push_back(a[index*2+1]);
dfs(index*2+1);
v.pop_back();
v.push_back(a[index*2]);
dfs(index*2);
v.pop_back();
}
}
建立一个vector<int> v,对其进行维护。当下标已经到达了叶子节点,即index*2>n && index*2+1>n ,对于路径进行输出,只不过假定这时候还不知道代码如何。下标未达到叶子节点时,先进行右子树后进行左子树的dfs,先将右子树节点插入v中,在dfs完成后退出,再左子树完整走一遍。这样便可以完善输出的代码了,对于输出,在插入了右子树节点后,遍历v将v中元素全部输出即可。注意控制格式,以及v中的元素下标由1开始。
第二步是判断是最大堆或者最小堆,对此,在二叉树的数组a中,通过控制下标进行判断。这里一开始将下标由1开始逐步增大,<=n/2,但这样比较复杂,还要讨论是否存在最后一个右子树。由2-n直接通过整除2便可以访问二叉树的父节点。
完整代码如下:
#include <bits/stdc++.h>
using namespace std;
vector <int> v;
int n;
int a[1010]= {0};
int is_max=1,is_min=1;
void dfs(int index)
{
if((index*2>n) &&(index*2+1>n))
{
if(index<=n)
{
for(int i=0; i<v.size(); i++)
{
cout<<v[i];
if(i!=v.size()-1)
{
cout<<" ";
}
else if(i==v.size()-1)
{
cout<<endl;
}
}
}
}
else
{
v.push_back(a[index*2+1]);
dfs(index*2+1);
v.pop_back();
v.push_back(a[index*2]);
dfs(index*2);
v.pop_back();
}
}
int main()
{
cin>>n;
int i;
for(i=1; i<=n; i++)
{
cin>>a[i];
}
v.push_back(a[1]);
dfs(1);
for(i=2;i<=n;i++)
{
if(a[i/2]>a[i])
{
is_min=0;
}
else if(a[i/2]<a[i])
{
is_max=0;
}
}
if(is_min==1)
{
cout<<"Min Heap"<<endl;
}
else if(is_min==0)
{
if(is_max==1)
{
cout<<"Max Heap"<<endl;
}
else
{
cout<<"Not Heap"<<endl;
}
}
return 0;
}