1155 Heap Paths (30 分)【dfs】

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

题意:给出完全二叉树的层序遍历,要求出所有从根节点到叶子节点的路径(右子树优先),还要判断是否为堆,如果是判断是大顶堆还是小顶堆。

解题思路:完全二叉树的层序遍历,我们知道下标的关系,节点的左右子节点的关系是2*i+1、2*i+2(从0开始),求出根节点到叶子节点所有路径,深度优先搜索dfs,用vector来保存经过路径的节点,通过push_back()和push_pop()来控制路的节点删除插入。当2*index+1和2*index+2都大于等于n,就说明这个节点已经是叶子节点,现在要做的就是将这条路径的节点打印出来,这里要注意的是要考虑只有左结点的情况,所以在打印的前面加个条件(index<n),若不是到叶子节点,那我们先将右结点压入vector中,然后对右结点递归,递归完接下来就要push_pop(),删除上一个叶子节点,继续将左子节点压入到vector中,然后对左子节点递归,递归完也删除上一个叶子节点。判断堆这方面,我们先假设都是大顶堆小顶堆,然后我们for循环,从第二个循环,如果v[i]>v[(i-1)/2]就将小顶堆置为false,相反将大顶堆置为false,然后其中一个为true,就说明是什么堆,如果都是false,说明不是堆,直接输出Not Heap。

#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

int n;
vector<int>v,path;
bool Minheap=true,Maxheap=true;
void dfs(int index)
{
	if(2*index+1>=n&&2*index+2>=n)
	{
		if(index<n){//考虑到只有一个左结点(要是不考虑,会给不存在的右结点随机数)
		    for(int i=0;i<path.size();i++)
		    printf("%d%s",path[i],i==path.size()-1?"\n":" ");
	   }
	}
	else{
		path.push_back(v[2*index+2]);
		dfs(2*index+2);
		path.pop_back();
		path.push_back(v[2*index+1]);
		dfs(2*index+1);
	    path.pop_back();
	
	}
}
int main(void)
{
	scanf("%d",&n);
	v.resize(n);
	for(int i=0;i<n;i++)
	{
		scanf("%d",&v[i]);
	}
	path.push_back(v[0]);
	dfs(0);
	for(int i=1;i<n;i++)
	{
		if(v[i]<v[(i-1)/2]) Minheap=false;
		if(v[i]>v[(i-1)/2]) Maxheap=false;
	}
	if(Minheap==true)
	printf("Min Heap");
	else 
	printf("%s",Maxheap==true?"Max Heap":"Not Heap");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Imagirl1/article/details/85609678