二叉树的链表实现

#ifndef _BINARYTREE_H
#define _BINARYTREE_H
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
template <class Type>
class BinaryTree{
private:
	struct Node{
		Node *left;
		Node *right;
		Type data;

		Node():left(NULL),right(NULL){}
		Node(Type item, Node *L=NULL, Node *R=NULL):data(item),left(L),right(R){}
		~Node(){}
	};
	Node *root;
	struct stNode {
		Node *node;
		int timesPop;
		stNode(Node *N=NULL):node(N),timesPop(0){}
	};

public:
	BinaryTree():root(NULL){}
	BinaryTree(const Type& value) {
		root=new Node(value);
	}
	~BinaryTree(){ 
		clear();
	}
	Type getRoot() const { return root->data; }
	Type getLeft() const { return root->left->data; }
	Type getRight() const { return root->right->data; }
	void makeTree(const Type &x, BinaryTree <, BinaryTree &rt){
		root=new Node(x,lt.root,rt.root);
	}
	void delLeft(){
		BinaryTree tmp=root->left;
		root->left=NULL;
		tmp.clear();
	}
	void delRight(){
		BinaryTree tmp=root->right;
		root.right=NULL;
		tmp.clear();
	}
	bool IsEmpty() const { return root==NULL; }
	void clear(){
		if(root!=NULL)
			clear(root);
		root=NULL;
	}
	int size() const {return size(root); }
	int height() const { return height(root); }
	void preOrder() const{
		if(root!=NULL){
			cout << "\n前序遍历:";
			preOrder(root);
		}
	}
	void midOrder() const{
		if(root!=NULL){
			cout << "\n中序遍历:";
			midOrder(root);
		}
	}
	void postOrder() const{
		if(root!=NULL){
			cout << "\n后序遍历:";
			postOrder(root);
		}
	}
	void Exchange(){
		if(root!=NULL){
			Exchange(root);
		}
	}
	void levelTranverse(){
		if(root!=NULL){
			cout << "\n层次遍历:";
			levelTranverse(root);
		}
	}
	void createTree(Type flag){
		queue<Node*> que;
		Node *tmp;
		Type x,ldata,rdata;
		cout << "\n输入根节点:";
		cin >> x;
		root=new Node(x);
		que.push(root);
		while (!que.empty())
		{
			tmp=que.front();
			que.pop();
			cout << "\n输入 " << tmp->data << "的两个儿子(" << flag << "表示空节点:)";
			cin >> ldata >> rdata;
			if(ldata!=flag)
				que.push(tmp->left=new Node(ldata));
			if(rdata!=flag)
				que.push(tmp->right=new Node(rdata));
		}
		cout << "create completed! \n";
	}

private:
	//递归求树高
	int height(Node *t) const{
		if(NULL==t)
			return 0;
		else{
			int hleft=height(t->left);
			int hright=height(t->right);
			return 1+((hleft>hright)?hleft:hright);
		}
	}
	void clear(Node *t){
		if(t->left!=NULL)
			clear(t->left);
		if(t->right!=NULL)
			clear(t->right);
		delete t;
		t=NULL;
		return;
	}
	//递归求树大小
	int size(Node *t) const{
		if(t==NULL)
			return 0;
		return 1+size(t->left)+size(t->right);
	}
	//递归实现
	/*void preOrder(Node *t) const{
		if(t!=NULL){
			cout << t->data << " ";
			preOrder(t->left);
			preOrder(t->right);
		}
		return;
	}*/
	//非递归实现
	void preOrder(Node *t) const{
		stack<Node*> s;
		Node *current;
		s.push(t);
		while (!s.empty())
		{
			current=s.top();
			s.pop();
			cout << current->data << " ";
			if(current->right!=NULL)
				s.push(current->right);
			if (current->left!=NULL)
				s.push(current->left);
		}
	}
	//递归实现
	/*void midOrder(Node *t) const{
		if (t!=NULL){
			midOrder(t->left);
			cout << t->data << " ";
			midOrder(t->right);
		}
	}*/
	//非递归实现
	void midOrder(Node *t) const{
		stack<stNode> s;
		stNode current(t);
		s.push(current);
		while (!s.empty()){
			current=s.top();
			s.pop();
			if (++current.timesPop==2){
				cout << current.node->data << " ";
				if(current.node->right!=NULL)
					s.push(stNode(current.node->right));
			}
			else{
				s.push(current);
				if(current.node->left!=NULL)
					s.push(stNode(current.node->left));
			}
		}
	}
	//递归实现
	/*void postOrder(Node *t) const{
		if (t!=NULL){
			postOrder(t->left);
			postOrder(t->right);
			cout << t->data << " ";
		}
	}*/
	//非递归实现
	void postOrder(Node *t) const{
		stack<stNode> s;
		stNode current(root);
		s.push(current);
		while (!s.empty()){
			current=s.top();
			s.pop();
			if(++current.timesPop==3){
				cout << current.node->data << " ";
				continue;
			}
			s.push(current);
			if (current.timesPop==1)
			{
				if(current.node->left!=NULL)
					s.push(stNode(current.node->left));
			}else{
				if(current.node->right!=NULL)
					s.push(stNode(current.node->right));
			}
		}
	}
	//左右子树交换
	void Exchange(Node *t){
		if(t!=NULL){
			Node *tmp;
			tmp=t->left;
			t->left=t->right;
			t->right=tmp;
			Exchange(t->left);
			Exchange(t->right);
		}
		return;
	}
	//层次遍历
	void levelTranverse(Node *t){
		queue<Node *> que;
		Node *tmp;
		que.push(t);
		while (!que.empty()){
			tmp=que.front();
			que.pop();
			cout << tmp->data << " ";
			if(tmp->left!=NULL)
				que.push(tmp->left);
			if(tmp->right!=NULL)
				que.push(tmp->right);
		}
		cout << endl;
		return;
	}
};

#endif//~_BINARYTREE_H
 
#include "BinaryTree.h"
#include <queue>
#include <iostream>
using namespace std;
int main(){
	BinaryTree<char> tree;
	tree.createTree('@');
	cout << "The height is: " << tree.height() << endl;
	cout << "The size is: " << tree.size() << endl;

	tree.preOrder();
	tree.midOrder();
	tree.postOrder();
	tree.levelTranverse();

	tree.Exchange();

	tree.preOrder();
	tree.midOrder();
	tree.postOrder();
	tree.levelTranverse();

	tree.preOrder();
	tree.midOrder();
	tree.postOrder();
	cout << tree.IsEmpty() << endl;

	tree.clear();
	tree.preOrder();
	cout << tree.IsEmpty() << endl;
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值