aspglujalk_BinaryTree

基础文本二叉树功能实现

# include <iostream>
#include<queue>
#include<stack>
using namespace std;

template<class T>
struct Node
{
	T data;
	Node* next;
};
template<class T>
class BinarytreeNode {
	T data;
	BinarytreeNode* leftsibling;
	BinarytreeNode* rightsibling;
	
};


template <class T>
class binarytree
{
public:
	BinarytreeNode<T>* creatbinarytree();
	void Norepreorder (BinarytreeNode<T>*root);//非递归前序遍历
	void Noreinorder(BinarytreeNode<T>* root);//非递归中序遍历
	void Norepostorder(BinarytreeNode<T>* root);//非递归后序遍历
	void preorder(BinarytreeNode<T>* root);//递归前序遍历
	void inorder(BinarytreeNode<T>* root);//递归中序遍历
	void postorder(BinarytreeNode<T>* root);//递归后序遍历
	void levelorder(BinarytreeNode<T>* root);//层次遍历
	int leafnumber(BinarytreeNode<T>* p);//叶子数
	int nodenumber(BinarytreeNode<T>* p);//结点数
	int getdepth(BinarytreeNode<T>* p);//求深度
	void exchangeleftrightsibiling(BinarytreeNode<T>* root);//交换左右子树
	bool findX(BinarytreeNode<T>* root,T X);//查找树
	bool isEmpty(BinarytreeNode<T>* root);//是否为空树
	Node<T>* buildleaflist(Node<T>* root);//建立叶子链表
	void printflistnode(Node<T>* root);//打印列表
	BinarytreeNode<T>* copyBinaryTree(BinarytreeNode<T>* root);//复制树
	bool isEqual(BinarytreeNode<T>* p1, BinarytreeNode<T>* p2);//两树是否相同
	void LinkToSequence(BinarytreeNode<T>* p, T arr[], int i);//转为链表
	void MakeEmpty(BinarytreeNode<T>* root);//置空

};

template<class T>
BinarytreeNode<T> *binarytree<T>::creatbinarytree() {
	BinarytreeNode<T>* p;
	T num;
	cin >> num;
	if (num == '#') {
		p = NULL;
	}
	else
	{
		p = new BinarytreeNode<T>();
		if (p==NULL)
		{
			cout << "flase";
			exit(1);
		}
	}
	p->data = num;
	p->leftsibling=creatbinarytree();
	p->rightsibling=creatbinarytree();
};

template<class T>
void binarytree<T>::Norepreorder(BinarytreeNode<T>* root) {
	stack<BinarytreeNode<T>*>s;
	BinarytreeNode<T>* current;
	current = root;
	while (current != NULL) {
		cout << current->data << "";
		s.push(current);
		current = current->leftsibling;
	}
	if (!s.empty)
	{
		current = s.top;
		s.pop();
		current = current->rightsibling;
	}

}

template<class T>
void binarytree<T>::Noreinorder(BinarytreeNode<T>* root) {
	stack<BinarytreeNode<T>*>s;
	BinarytreeNode<T>* current;
	current = root;
	while (!s.empty||current!=NULL)
	{
		while (current!=NULL)
		{
			s.push(current);
			current = current->leftsibling;
		}
		if (!s.empty)
		{
			current = s.top;
			cout << current->data << "";
			current = current->rightsibling;
		}
	}

}

template<class T>
void binarytree<T>::Norepostorder(BinarytreeNode<T>* root) {
	std::stack<BinarytreeNode<T>*> s1;
	std::stack<int> s2;
	BinTreeNode<T>* current;
	int flag;

	current = root;
	while (!s1.empty() || current != NULL) {
		while (current != NULL) { // 当前结点非空
			s1.push(current); // 当前结点和第一次进栈标志进栈
			s2.push(0);
			current = current->leftChild; // 以当前结点的左孩子作为当前结点
		}
		if (!s1.empty()) { // 栈非空
			current = s1.top(); // s1,s2栈顶顶点出栈
			s1.pop();
			flag = s2.top();
			s2.pop();
			if (flag == 1) { // 如果该结点是第二次出栈
				std::cout << current->data << " "; // 访问结点
				current = NULL;
			}
			else {
				s1.push(current); // 结点第二次进栈
				s2.push(1);
				current = current->rightChild; // 以当前结点的右孩子作为当前结点
			} 
		} 
	}
} 

template<class T>
void binarytree<T>::preorder(BinarytreeNode<T>* p) {
	if (p!=NULL)
	{
		cout << p->data << "";
		preorder(p->leftsibling);
		preorder(p->rightsibling);
	}
}

template<class T>
void binarytree<T>::inorder(BinarytreeNode<T>* p) {
	if (p!=NULL)
	{
		preorder(p->leftsibling);
		cout << p->data << "";
		preorder(p->rightsibling);
	}
}

template<class T>
void binarytree<T>::postorder(BinarytreeNode<T>* p) {
	if (p!=NULL)
	{
		preorder(p->leftsibling);
		preorder(p->rightsibling);
		cout << p->data << "";
	}
}

template<class T>
void binarytree<T>::levelorder(BinarytreeNode<T>* root) {
	queue<binarytreeNode<T>*>q;
	BinarytreeNode<T>* current;
	if (current!=NULL)
	{
		q.push(current)
			while (!q.empty())
			{
				current = q.front();

			}
	}

}

template<class T>
int binarytree<T>::leafnumber(BinarytreeNode<T>* p) {
	if (P==NULL)
	{
		return 0;
	}
	else
	{
		if (p->leftsibling == NULL && p->rightsibling == NULL)
		{
			return 1;
		}
		else
		{
			return leafnumber(p->rightsibling) + leafnumber(p->leftsibling);
		}
		
	}
}

template<class T>
int binarytree<T>::nodenumber(BinarytreeNode<T>* p) {
	if (p==NULL)
	{
		return 0;
	}
	else
	{
		return 1 + nodenumber(p->rightsibling) + nodenumber(p->leftsibling);
	}
}

template<class T>
int binarytree<T>::getdepth(BinarytreeNode<T>* p) {
	int depth1, depth2;
	if (p==NULL)
	{
		depth1 = depth2 = 0;
		return 0;
	}
	else
	{
		depth1 = getdepth(p->leftsibling);
		depth2 = getdepth(p->rightsibling);
	}
	if (depth1>depth2)
	{
		return1 (1+depth1);
	}
	else
	{
		return (1+depth2);
	}
}

template<class T>
void binarytree<T>::exchangeleftrightsibiling(BinarytreeNode<T>* parent) {
	BinarytreeNode<T>* current;
	if (parent!=NULL)
	{
		current = parent->leftsibling;
		parent->leftsibling = parent->rightsibling;
		parent->rightsibling = current;;
		exchangeleftrightsibiling(parent->leftsibling);
		exchangeleftrightsibiling(parent->rightsibling);
	}

}

template<class T>
bool binarytree<T>::findX(BinarytreeNode<T>* root,T X) {
	BinarytreeNode* current;
	stack<BinarytreeNode<T>*>s;
	if (root==NULL)
	{
		return false;
	}
	current = root;
	while (!s.empty()||current!=NULL){
		while (current != NULL && current->data == X) {
			s.push(current);
			current = current->leftsibling;
		}
		if (current!=NULL&&current->data==X)
		{
			return true;
		}
		if (!s.empty())
		{
			current = s.top;
			s.pop;
			if (current->rightsibling!=NULL)
			{
				current = current->rightsibling;
			else
			{
				current = NULL;
			}
			}
		}
	}
	return false;
}

template<class T>
bool binarytree<T>::isEmpty(BinarytreeNode<T>* root) {
	if (root==NULL)
	{
		return true;
	}
	return false;

}

template<class T>
void binarytree<T>::printflistnode(Node<T>* root) {
	Node<T>* current;
	current = root;
	while (current!=NULL)
	{
		cout << current->data << "";
		current = current->next;
	}
}

template<class T>
BinarytreeNode<T>* binarytree<T>::copyBinaryTree(BinarytreeNode<T>* p) {
	BinarytreeNode<T>* current;
	if (p==NULL)
	{
		return NULL;
	}
	current = new BinarytreeNode<T>();
	if (current==NULL)
	{
		cerr << "申请空间失败!\n"; \
			exit(1);
	}
	current->data = p->data;
	current->leftsibling = copyBinaryTree(p->leftsibling);
	current->rightsibling = opyBinaryTree(p->rightsibling);
	return current;
}

template<class T>
bool binarytree<T>::isEqual(BinarytreeNode<T>* p1, BinarytreeNode<T>* p2) {
	bool isflag;
	
	isflag = false;
	if (p1==NULL&&p2==NULL)
	{
		isflag = true;
	}
	else if (p1!==NULL&&p2!==NULL&&(p1->data==p2->data))
	{
		if (isEqual(p1->leftsibling, p2->leftsibling)) {
			if (isEqual(p1->rightsibling,p2->rightsibling)
			{
				isflag=ture
			}
		}
	}

	return isflag;
}

template<class T>
void binarytree<T>::LinkToSequence(BinarytreeNode<T>* p, T arr[], int i) {
	if (p!==NULL)
	{
		arr[i] = p->data;
		LinkToSequence(p->leftsibling, arr, 2 * i + 1);
		LinkToSequence(p->rightsibling, arr, 2 * i + 2);
	}
}

template<class T>
void binarytree<T>::MakeEmpty(BinarytreeNode<T>* root) {
	if (p!=NULL)
	{
		MakeEmpty(p->leftsibling);
		MakeEmpty(p->rightsibling);
		delete p;
		p = NULL;
	}
}


int main(){
  switch case ...;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值