1155

#include<iostream>
#include<vector>
#include<stack>
using namespace std;
bool IsMaxHeap(vector<int>&A);
bool IsMinHeap(vector<int>&A);
void Print(vector<int>&A);
int n;
int main()
{
	vector<int> A;
	
	cin >> n;
	A.push_back(0);
	for (int i = 0; i < n; i++)
	{
		int a;
		cin >> a;
		A.push_back(a);
	}
	Print(A);
	if (IsMaxHeap(A))
		cout << "Max Heap" << endl;
	else if (IsMinHeap(A))
		cout << "Min Heap" << endl;
	else
		cout << "Not Heap" << endl;
	system("pause");
	return 0;
}
void Print(vector<int>&A)
{
	int pos;
	int i = 1;
	while (true)
	{
		if (i * 2 + 1 <= n)
			i = i * 2 + 1;
		else if (i * 2 <= n)
			i = i * 2;
		else
		{
			pos = i;
			break;
		}
	}
	int start = n / 2 + 1;
	int end = n;
	for (i = pos; i >= start; i--)
	{
		stack<int> ans;
		int j = i;
		while (j >= 1)
		{
			ans.push(A[j]);
			j = j / 2;
		}
		if (!ans.empty())
		{
			cout << ans.top();
			ans.pop();
		}
		while (!ans.empty())
		{
			cout << ' ' << ans.top();
			ans.pop();
		}
		cout << endl;
	}
	for (i = n; i > pos; i--)
	{
		stack<int> ans;
		int j = i;
		while (j >= 1)
		{
			ans.push(A[j]);
			j = j / 2;
		}
		if (!ans.empty())
		{
			cout << ans.top();
			ans.pop();
		}
		while (!ans.empty())
		{
			cout << ' ' << ans.top();
			ans.pop();
		}
		cout << endl;

	}
}
bool IsMaxHeap(vector<int>&A)
{
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		l = i * 2;
		r = l + 1;
		if (l <= n&&A[i] < A[l])
			return false;
		if (r <= n&&A[i] < A[r])
			return false;
	}
	return true;
}
bool IsMinHeap(vector<int>&A)
{
	for (int i = 1; i <= n; i++)
	{
		int l, r;
		l = i * 2;
		r = l + 1;
		if (l <= n&&A[i] > A[l])
			return false;
		if (r <= n&&A[i] > A[r])
			return false;
	}
	return true;
}
发布了195 篇原创文章 · 获赞 9 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/zero_1778393206/article/details/88053793
今日推荐