Bootstrap

C++实现二叉树同构

浙大数据结构C++实现,写的一塌糊涂到处借鉴,,最后的逻辑也看不懂,有需要改正的地方请指出,不胜感激

#pragma once
#include<iostream>
using namespace std;

template<class T>
class Node {
public:
	T data;
	int left;
	int right;
public:
	Node() :data(0), left(-1), right(-1) {};
	Node(T d, int l, int r) : data(d), left(l), right(r) {};
	
	Node& operator=(Node& n) {
		this->data = n.data;
		this->left = n.left;
		this->right = n.right;
		return *this;
	}
};
template<class T>
class Stalinktree {
public:
	Stalinktree() : m_len(0), m_maxsize(10) {
		this->m_arrptr = new Node<T>[this->m_maxsize];
	}
	int Bulidtree(Node<T>* nodearrptr);
	int judge(Stalinktree<T> st1,Stalinktree<T> st2, int r1, int r2);

public:
	int m_maxsize;
	int m_len;
	Node<T>* m_arrptr;

};

template<class T>
int Stalinktree<T>::Bulidtree(Node<T>* nodearrptr) {
	int temp_len;
	cin >> temp_len;
	int*check = new int[temp_len];
	for (int i = 0; i < temp_len; i++) {
		check[i] = 0;
	}
	for (int i = 0; i < temp_len; i++) {
		T temp_data;
		int temp_left;
		int temp_right;
		cin >> temp_data >> temp_left >> temp_right;
		Node<T> temp_n(temp_data, temp_left, temp_right);
		nodearrptr[i] = temp_n;
		if (nodearrptr[i].left != -1) {
			check[nodearrptr[i].left] = 1;
		}
		if (nodearrptr[i].right != -1) {
			check[nodearrptr[i].right] = 1;
		}
	}
	this->m_len = temp_len;
	for (int i = 0; i < temp_len; i++) {
		if (!check[i]) {
			return i;
		}
	}
	delete[] check;
	return -1;
	
}

template<class T>
int Stalinktree<T>::judge(Stalinktree<T> st1 ,Stalinktree<T> st2, int r1, int r2) {
	//
	/*int r1 = st1.Bulidtree(st1.m_arrptr);
	int r2 = st2.Bulidtree(st2.m_arrptr);*///Buildtre应该是在judge调用之前就已经完成的

	if (r1 == -1 && r2 == -1) {//两树都是空
		return 1;
	}
	if ((r1 != -1 && r2 == -1) || (r1 == -1 && r2 != -1)) {//有一个非空
		return 0;
	}
	if (st1.m_arrptr[r1].data != st2.m_arrptr[r2].data) {
		return 0;
	}
	//两个树都是存在的,并且值相等,开始递归判断子树
	if ((st1.m_arrptr[r1].left == -1) && (st2.m_arrptr[r2].left == -1)) {
		// 此时左子树都为空
		//return judge(*st1.m_arrptr[st1.m_arrptr[r1].right], *st2.m_arrptr[st2.m_arrptr[r2].right]);。。。
		//如何实现递归调用右子树判断
		return judge( st1, st2, st1.m_arrptr[r1].right, st2.m_arrptr[r2].right);
	}
	//左子树存在,继续判断
	if ((st1.m_arrptr[r1].left != -1) && (st2.m_arrptr[r2].left != -1)
		&& (st1.m_arrptr[st1.m_arrptr[r1].left].data) == (st1.m_arrptr[st2.m_arrptr[r2].left].data))
	{	// 此时左子树都非空//并且左子树的值相等
		// 看不懂
		return (judge(st1, st2, st1.m_arrptr[r1].left, st2.m_arrptr[r2].left) && judge(st1, st2, st1.m_arrptr[r1].right, st2.m_arrptr[r2].right));
		
	}
	else {//判断是不是左右同构
		return judge(st1, st2, st1.m_arrptr[r1].left, st2.m_arrptr[r2].left)
			&& judge(st1, st2, st1.m_arrptr[r1].right, st2.m_arrptr[r2].right);
	}
}
#include"staticlinktree.hpp"
using namespace std;

void test01() {
	//Linktree<int> lt1;
	Stalinktree<char>* slt1 = new Stalinktree<char>();
	int r1 = slt1->Bulidtree(slt1->m_arrptr);
	Stalinktree<char>* slt2 = new Stalinktree<char>();
	int r2 = slt2->Bulidtree(slt2->m_arrptr);
	cout << slt1->judge(*slt1, *slt2, r1, r2) << endl;
}

int main() {
	test01();
	system("pause");
	return 0;
}

;