数据结构_第三周编程作业_C++题解

DS_Lecture 3 Assignments_C++题解

中国大学MOOC_陈越、何钦铭_数据结构_第三周编程作业

1. 树的同构

小白专场有详细讲解,基本要求,必做;

#include<iostream>
using namespace std;
#define MAXTREE 10
#define ElementType char
#define Tree int
#define Null -1
struct TreeNode{
	ElementType data;
	Tree left;
	Tree right;
}T1[MAXTREE], T2[MAXTREE];

Tree BuildTree(struct TreeNode T[])
{
	int N;
	cin >> N;
	int root = Null;
	bool *check = new bool[N]();
	char cl, cr;
	if (N){
		for (int i = 0; i < N; i++){
			cin >> T[i].data >> cl >> cr;
			if (cl != '-'){
				T[i].left = cl - '0';
				check[T[i].left] = true;
			}
			else T[i].left = Null;
			if (cr != '-'){
				T[i].right = cr - '0';
				check[T[i].right] = true;
			}
			else T[i].right = Null;
		}
		for (int i = 0; i < N; i++){
			if (!check[i]){
				root = i;
				break;
			}
		}
	}
	return root;
}

bool Isomorphic(Tree root1, Tree root2)
{
	if (root1 == Null && root2 == Null) return true;
	if ((root1 == Null && root2 != Null) || (root1 != Null && root2 == Null)) return false;
	if (T1[root1].data != T2[root2].data) return false;
	Tree left1 = T1[root1].left;
	Tree right1 = T1[root1].right;
	Tree left2 = T2[root2].left;
	Tree right2 = T2[root2].right;
	if (left1 == Null && left2 == Null) return Isomorphic(right1, right2);
	if (right1 == Null && right2 == Null) return Isomorphic(left1, left2);
	if (left1 == Null && right2 == Null) return Isomorphic(right1, left2);
	if (right1 == Null && left2 == Null) return Isomorphic(left1, right2);
	if (left1 != Null && left2 != Null && right1 != Null && right2 != Null){
		if (T1[left1].data == T2[left2].data) return Isomorphic(left1,left2) && Isomorphic(right1,right2);
		if (T1[left1].data == T2[right2].data) return Isomorphic(left1,right2) && Isomorphic(right1,left2);
	}
	return false;
}

int main()
{
	Tree root1, root2;
	root1 = BuildTree(T1);
	root2 = BuildTree(T2);
	if (Isomorphic(root1, root2)) cout << "Yes";
	else cout << "No";
	return 0;
}

2. List Leaves

训练建树和遍历基本功,必做;

#include<iostream>
#include<queue>
using namespace std;
#define MAXTREE 10
struct TreeNode{
	int left;
	int right;
}T[MAXTREE];

int BuildTree(struct TreeNode T[])
{
	int N;
	cin >> N;
	bool *check = new bool[N]();
	char cl, cr;
	int root = -1;
	if (N){
		for (int i = 0; i < N; i++){
			cin >> cl >> cr;
			if (cl != '-'){
				T[i].left = cl - '0';
				check[T[i].left] = true;
			}
			else T[i].left = -1;
			if (cr != '-'){
				T[i].right = cr - '0';
				check[T[i].right] = true;
			}
			else T[i].right = -1;
		}
		for (int i = 0; i < N; i++){
			if (!check[i]){
				root = i;
				break;
			}
		}
	}
	return root;
}

void ListLeaves(int root)
{
	queue<int> q, leaves;
	q.push(root);
	while(!q.empty()){
		if (T[q.front()].left < 0 && T[q.front()].right < 0){
			leaves.push(q.front());
		}
		if (T[q.front()].left >= 0) q.push(T[q.front()].left);
		if (T[q.front()].right >= 0) q.push(T[q.front()].right);
		q.pop();
	}
	if (!leaves.empty()){
		cout << leaves.front();
		leaves.pop();
	}
	while (!leaves.empty()){
		cout << " " << leaves.front();
		leaves.pop();
	}
} 

int main()
{
	int tree = BuildTree(T);
	ListLeaves(tree);
}

思路

用层序遍历方法,遇到叶结点保存下来再用正确格式输出

3. PAT(A) 1086 Tree Traversals Again

  • 2014年秋季PAT甲级考试真题,稍微要动下脑筋,想通了其实程序很基础,建议尝试。
  • Orz 没思路参考了小姐姐的代码,写的过程出了很多循环控制上的问题,好好想想叭!
#include<iostream>
#include<stack>
#include<vector>
#include<string>
using namespace std;
vector<int> pre, in, post, value;
void PostOrder(int root, int begin, int end)
{
//	cout << endl << "root = " << root << " begin = " << begin << " end = " << end << endl;
	if (begin > end) return;
	int pos = begin;
	while (pos < end && in[pos] != pre[root]) ++pos;
//	cout << "pos = " << pos << endl;
	PostOrder(root+1, begin, pos-1);
	PostOrder(root+pos-begin+1, pos+1, end);
	post.push_back(pre[root]);
}
int main()
{
	int N, data;
	cin >> N;
	stack<int> s;
	string str;
	int key = 0;
	for (int i = 0; i < 2*N; i++){
		cin >> str;
		if (str == "Push"){
			cin >> data;
			value.push_back(data);
			pre.push_back(key);
			s.push(key++);
		}
		else{
			in.push_back(s.top());
			s.pop();
		}
	}
	PostOrder(0, 0, N-1);
	cout << value[post[0]];
	for (int i = 1; i < N; i++)
		cout << " " << value[post[i]];
	return 0;
}

思路

  • 压入栈的顺序:先序;PO操作提供的顺序:中序 =》已知先序中序求后序
  • 递归倒序:倒序整棵树 = 倒序左子树->倒序右子树->访问根结点

注意:

posted @ 2019-05-16 14:29  鲸90830  阅读(267)  评论(0)    收藏  举报