C++实现判断同一棵二叉搜索树

C++实现判断同一棵二叉搜索树,第1张

浙大数据结构C++实现

在二叉搜索树的基础上添加一部分程序

主程序:

#include"bstree.hpp"

void judge_one_tree(int len,int num);

void judge_while_tree() {
	int len = 0;
	int num = 0;
	cin >> len;
	while (len) {
		cin >> num;
		judge_one_tree(len, num);
		cin >> len;
	}
}

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

void judge_one_tree(int len, int num) {
	//建立一个长为len的树
	BStree bst;
	bst.creat_tree(len);
	while (num) {
		bst.reset_flag(bst.m_head);
		bool temp_flag = true;//默认1代表没有问题
		for (int i = 0; i < len; i++) {
			int temp_data = 0;
			cin >> temp_data;
			if (temp_flag && (!bst.check(temp_data, bst.m_head)) ) {
				//check返回0,代表检查到不是一个树
				temp_flag = false;
				//temp_flag为零的话就不用check,直接下一个读入
			}
		}
		if (temp_flag) {
			cout << "yes\n";
		}
		else {
			cout << "no\n";
		}
		num--;
	}
}

测试用例

 在二叉搜索树的基础上重构了creat_tree,加上了check函数以及reset_flag和析构,凑和看,如果有人看的话,直接贴完整代码了。如果有建议的话请指出,感谢

#pragma once
#include
using namespace std;

template
class Node {
public:
	Node() :data(0), left(NULL), right(NULL),flag(0) {}
public:
	T data;
	Node* left;
	Node* right;
	int flag;
};

template
class BStree {
public:
	BStree() {
		this->m_head = NULL;//为什么不能初始化列表呢?
	}
	~BStree() {
		FreeTree(this->m_head);
	}
	void creat_tree();
	void creat_tree(int n);
	void Preprint();
	void Insertele(T elem);
	bool check(int v, Node* head_ptr);
	void FreeTree(Node* head_ptr);
	void reset_flag(Node* head_ptr);
	Node* Findele(T elem, Node* head_ptr);
	Node* Maxele(Node* head_ptr);
	Node* Minele();
	Node* Delelem(T elem, Node* head_ptr);
	
private:
	Node* Insertele(T elem, Node*& head_ptr);
	void Preprint(Node* head_ptr);
public:
	Node* m_head;
};

template
Node* BStree::Insertele(T elem, Node*& head_ptr) {//不加引用好像不行?原因不知道
	if(head_ptr == NULL){
		head_ptr = new Node;
		head_ptr->data = elem;
	}
	else {
		if (elem >head_ptr->data ) {
			head_ptr->right = Insertele(elem, head_ptr->right);
		}
		else if (elem < head_ptr->data) {
			head_ptr->left = Insertele(elem, head_ptr->left);
		}
		//已经存在就什么都不做
	}
	return head_ptr;
}
template
void BStree::Insertele(T elem) {
	this->Insertele(elem, this->m_head);
}
template
void BStree::creat_tree() {
	int num = 0;
	cin >> num;
	for (int i = 0; i < num; i++) {
		T elem;
		cin >> elem;
		Insertele(elem, this->m_head);
	}
}
template
void BStree::creat_tree(int n) {
	for (int i = 0; i < n; i++) {
		T elem;
		cin >> elem;
		Insertele(elem, this->m_head);
	}
}

template
void BStree::Preprint(Node* head_ptr) {
	if (head_ptr) {
		cout << head_ptr->data << " ";
		Preprint(head_ptr->left);
		Preprint(head_ptr->right);
	}
}
template
void BStree::Preprint() {
	this->Preprint(this->m_head);
	cout << endl;
}

template
Node* BStree::Findele(T elem, Node* head_ptr) {
	if (head_ptr) {
		if (elem == head_ptr->data) {
			return head_ptr;
		}
		else if (elem > head_ptr->data) {
			return Findele(elem, head_ptr->right);
		}
		else if (elem < head_ptr->data) {
			return Findele(elem, head_ptr->left);
		}
	}
	return NULL;//返回位置有什么用?
}
template
Node* BStree::Maxele(Node* head_ptr) {
	if (head_ptr) {
		if (head_ptr->right) {
			Maxele(head_ptr->right);
		}
		else {
			return head_ptr;
		}
	}
	else {
		return NULL;
	}
}
template
Node* BStree::Minele() {
	Node* temp = this->m_head;
	while (temp) {
		if (temp->left) {
			temp = temp->left;
		}
		else {
			return temp;
		}
	}
	return temp;
}

template
Node* BStree::Delelem(T elem, Node* head_ptr) {
	if (!head_ptr) {
		cout << "not exist" << endl;
	}
	else if (elem > head_ptr->data) {
		head_ptr->right = Delelem(elem, head_ptr->right);//返回删除之后的地址
	}
	else if (elem < head_ptr->data) {
		head_ptr->left = Delelem(elem, head_ptr->left);
	}
	else {
		//找到元素之后进行处理
		if (head_ptr->left && head_ptr->right) {
			Node* temp = Maxele(head_ptr->left);
			head_ptr->data = temp->data;
			head_ptr->left = Delelem(temp->data, head_ptr->left);
		}
		else {
			if (!head_ptr->left) {
				head_ptr = head_ptr->right;//如果不返回地址,这里树就断了
			}
			else {
				head_ptr = head_ptr->left;
			}
		}
	}
	return head_ptr;
}

template
bool BStree::check(int v, Node* head_ptr) {
	if (head_ptr->flag) {
		if (v > head_ptr->data) {
			return check(v, head_ptr->right);
		}
		else if (v < head_ptr->data) {
			return check(v, head_ptr->left);
		}
		else {
			//说明已经查找过这个数,此时返回否
			return 0;
		}
	}
	else {//找到了flag不为0的节点
		if (v == head_ptr->data) {
			head_ptr->flag = 1;
			return 1;
		}
		else {
			return 0;
		}
	}
}
template
void BStree::reset_flag(Node* head_ptr) {
	if (head_ptr) {
		head_ptr->flag = 0;
		reset_flag(head_ptr->left);
		reset_flag(head_ptr->right);
	}
}

template
void BStree::FreeTree(Node* head_ptr) {
	if (head_ptr->left) {
		FreeTree(head_ptr->left);
	}
	if (head_ptr->right) {
		FreeTree(head_ptr->right);
	}
	delete head_ptr;
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/874807.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-13
下一篇 2022-05-13

发表评论

登录后才能评论

评论列表(0条)

保存