7-4 Structure of a Binary Tree (30 分)

2019 春季PAT考试,最后一题
7-4 Structure of a Binary Tree (30 分)

/*
题意:
给出一个二叉树的后序遍历和中序遍历序列,
然后给出一些语句,判断这些语句是否正确
正确:输出Yes
错误:输出No
输入:
1.第一行:n–元素个数
2.第二三行:分别给出后序,中序序列
3.第四行:m–判断语句个数
输出:
正确:输出一行Yes
错误:输出一行No

		判断的语句有
		A is the root--A是根节点
		A and B are siblings--两个节点是兄弟节点
		A is the parent of B--一个节点是另一个的父节点
		A is the left child of B--一个节点是另一个的左孩子
		A is the right child of B--一个节点是另一个的右孩子
		A and B are on the same level--两个节点在同一层
		It is a full tree--二叉树是full tree(除叶子节点外,其他节点都有两个孩子节点)
思路:
	1.二叉树的表示
		用数组表示,把输入的节点数据当成数组中的下标来处理
		这样判断某个节点时,只需要将数据作为下标来使用,省去了查找的时间
		节点需要添加layer和parent来判断父节点和同层的情况
	2.还原二叉树
	3.提取字符串中的整数 
		用sscanf()函数

*/

#include <iostream>
#include <vector>
#include <string>

#pragma warning(disable:4996)
#define MAXLEN 1000
using namespace std;

//定义数据结构
//没有数据域,直接把输入的数据当做节点在二叉树数组中的下标
struct Node {
	int layer;//层(根节点从0开始)
	int left, right, parent;
};
Node BTree[MAXLEN];
int root;
bool isFullTree = true;
int n, m;
int postOrder[MAXLEN], inOrder[MAXLEN];

void inputData()
{
	cin >> n;
	int temp;
	for (int i = 0; i < n; ++i)
	{
		cin >> postOrder[i];
	}
	for (int i = 0; i < n; ++i)
	{
		cin >> inOrder[i];
	}
}

int mfind(int start, int end, int key)
{
	for (int i = start; i <= end; ++i)
	{
		if (inOrder[i] == key)
			return i;
	}
	return -1;
}

//根据后续遍历和中序遍历还原二叉树
int createTree(int parent,int pl,int pr,int inl,int inr)
{
	if (inl > inr)
		return -1;
	int key = postOrder[pr];//后续遍历最后一个节点是根节点	
	int mid = mfind(inl, inr,key);//寻找根节点在中序遍历中的位置
	if (parent == -1)
	{
		BTree[key].layer = 0;
		root = key;
	}
	else
	{
		BTree[key].parent = parent;
		BTree[key].layer = BTree[parent].layer + 1;//更新层数
	}

	BTree[key].left = createTree(key,  pl, pl + mid - inl - 1, inl, mid - 1);
	BTree[key].right = createTree(key, pl + mid - inl,pr-1, mid + 1, inr);
	//此处判断刚创建的左右节点是否满足 full tree的条件
	if ((BTree[key].left == -1 && BTree[key].right != -1) || (BTree[key].right == -1 && BTree[key].left != -1))
		isFullTree = false;//有一个节点为空,另一个不为空,就不是full tree
	return key;
}

int main()
{
	inputData();
	createTree(-1, 0, n - 1, 0, n - 1);
	string comm;
	int a, b;
	bool flag = true;
	cin >> m;
	getchar();
	for (int i = 0; i < n; ++i)
	{
		getline(cin, comm);
		if (comm.find("root") != -1)
		{
			sscanf(comm.c_str(), "%d is the root", &a);
			flag = (root == a);
		}
		else if (comm.find("siblings") != -1)
		{
			sscanf(comm.c_str(), "%d and %d are siblings", &a, &b);
			flag = (BTree[a].parent == BTree[b].parent);
		}
		else if (comm.find("parent") != -1)
		{
			sscanf(comm.c_str(), "%d is the parent of %d", &a, &b);
			flag = (a == BTree[b].parent);
		}
		else if (comm.find("left") != -1)
		{
			sscanf(comm.c_str(), "%d is the left child of %d", &a, &b);
			flag = (a == BTree[b].left);
		}
		else if (comm.find("right") != -1)
		{
			sscanf(comm.c_str(), "%d is the right child of %d", &a, &b);
			flag = (a == BTree[b].right);
		}
		else if (comm.find("level") != -1)
		{
			sscanf(comm.c_str(), "%d and %d are on the same level", &a, &b);
			flag = (BTree[a].layer == BTree[b].layer);
		}
		else if (comm.find("full") != -1)
		{
			flag = isFullTree;
		}
		if (flag)
			cout << "Yes" << endl;
		else
			cout << "No" << endl;
	}

	system("pause");
	return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值