二叉树的操作

#pragma once


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


template<class Type>
class BinTree;

template<class Type>
class BinTreeNode;

typedef enum{L,R}Tag_Type;

template<class Type>
struct stkNode
{
	BinTreeNode<Type> *ptr;
	Tag_Type          tag;
};
template<class Type>
class BinTreeNode
{
	friend class BinTree<Type>;
public:
	BinTreeNode() :data(Type()), leftChild(NULL), rightChild(NULL)
	{}
	BinTreeNode(Type d,BinTreeNode<Type> *left = NULL,
		BinTreeNode<Type> *right = NULL)
		:data(d),leftChild(left),rightChild(right)
	{}
	~BinTreeNode()
	{}
private:
	Type data;
	BinTreeNode<Type> *leftChild;
	BinTreeNode<Type> *rightChild;
};

template<class Type>
class BinTree
{
public:
	BinTree():root(NULL)
	{}
	BinTree(Type ref):root(NULL),refvalue(ref)
	{}
	~BinTree()
	{}
	void CreateBinTree()
	{
		CreateBinTree(root);
	}
	void CreateBinTree1()
	{
		root = CreateBinTree_();
	}
	void CreateBinTree(const char* str)
	{
		CreateBinTree(root, str);
	}
	void PreOrder()
	{
		PreOrder(root);
	}
	void InOrder()
	{
		InOrder(root);
	}
	void PostOrder()
	{
		PostOrder(root);
	}
	void PreOrder_() const
	{
		PreOrder_(root);
	}
	void InOrder_() const
	{
		InOrder_(root);
	}
	void PostOrder_() const
	{
		PostOrder_(root);
	}
	int Size()const
	{
		return Size(root);
	}
	int Height()const
	{
		return Height(root);
	}
	BinTreeNode<Type> Root()const
	{
		if (root == NULL)
		{
			return NULL;
		}
		return root;
	}
	BinTreeNode<Type>* LeftChild(BinTreeNode<Type> *p)const
	{
		if (p == NULL)
		{
			return NULL;
		}
		return p->leftChild;
	}
	BinTreeNode<Type>* RightChild(BinTreeNode<Type> *p)const
	{
		if (p == NULL)
		{
			return NULL:
		}
		return p->rightChild;
	}
	BinTreeNode<Type>* Search(const Type &key)
	{
	    return Search(root,key);
	}
	BinTreeNode<Type>* Parent(const Type &key)
	{
		return Parent(root, key);
	}
	BinTreeNode<Type>* Parent(BinTreeNode<Type> *p)
	{
		return Parent(root, p);
	}
	bool Empty()const
	{
		return (NULL == root);
	}
	void DestroyBinTree()
	{
		DestroyBinTree(root);	
	}
	bool Equal(BinTree<Type> &bt)
	{		
		return Equal(root, bt.root);
	}
	void Copy(BinTree<Type> &bt)
	{
		root = Copy(bt.root);
	}
	void LevelOrder()
 	{
		LevelOrder(root);
	}
	void CreateBinTree_Pre(char *VLR, char *LVR, int n)
	{
		CreateBinTree_Pre(root, VLR, LVR, n);
	}
	void CreateBinTree_Post(char *LVR, char *LRV, int n)
	{
		CreateBinTree_Post(root, LVR, LRV, n);
	}
protected:
	/*void CreateBinTree_Post(BinTreeNode<Type> *&t, char *LVR, char *LRV, int n)
	{
		if(n == 0)
		{
			return;
		}
		else
		{
			int k = 0;
			while (LRV[n-1] != LVR[k])
			{
				k++;
			}
			t = new BinTreeNode<Type>(LVR[k]);
            CreateBinTree_Post(t->rightChild ,LVR+k+1,LRV+k,n-k-1);
			CreateBinTree_Post(t->leftChild ,LVR,LRV,k);
		}
	}
	void CreateBinTree_Pre(BinTreeNode<Type> *&t, char *VLR, char *LVR, int n)
	{
		if (n == 0)
		{
			return;
		}
		else
		{
			int k = 0;
			while (VLR[0] != LVR[k])
			{
				k++;
			}

			t = new BinTreeNode<Type>(LVR[k]);
			CreateBinTree_Pre(t->leftChild, VLR + 1, LVR, k);
			CreateBinTree_Pre(t->rightChild, VLR + k + 1, LVR + k + 1, n - k - 1);
		}
	}
	*/
	void PreOrder_(BinTreeNode<Type> *t) const
	{
		if (NULL == t)
		{
			return;
		}
		stack<BinTreeNode<Type> *> St;
		BinTreeNode<Type> *p;
		St.push(t);

		while (!St.empty())
		{
			p = St.top();
			St.pop();
			cout << p->data << " ";

			if (NULL != p->rightChild)
			{
				St.push(p->rightChild);
			}

			if (NULL != p->leftChild)
			{
				St.push(p->leftChild);
			}
		}
	}
	void InOrder_(BinTreeNode<Type> *t) const
	{
		if (NULL == t)
		{
			return;
		}
		stack<BinTreeNode<Type> *> St;
		BinTreeNode<Type> *p;
		St.push(t);
		while (!St.empty())
		{
			while (NULL != t->leftChild)
			{
				St.push(t->leftChild);
				t = t->leftChild;
			}
			p = St.top();
			St.pop();
			cout << p->data << " ";
			if (NULL != p->rightChild)
			{
				t = p->rightChild;
				St.push(t);
			}
		}
	}
	void PostOrder_(BinTreeNode<Type> *t) const
	{
		if (NULL == t)
		{
			return;
		}
		stack<stkNode<Type> > St;
		stkNode<Type> nd;
		do 
		{
			while (NULL != t)
			{
				nd.ptr = t;
				nd.tag = L;
				St.push(nd);
				t = t->leftChild;
			}
			bool flag = true;
			while (flag && !St.empty())
			{
				nd = St.top();
				St.pop();
				switch (nd.tag)
				{
				case L:
					nd.tag = R;
					t = nd.ptr;
					flag = false;
					St.push(nd);
					t = t->rightChild;
					break;
				case R:
					cout << nd.ptr->data << " ";
					break;
				default:
					break;
				}
			}
		} while (!St.empty());
 	}
	BinTreeNode<Type> *Copy(BinTreeNode<Type> *t)
	{
		DestroyBinTree();
		if (NULL == t)
		{
			return NULL;
		}
		else
		{
			BinTreeNode<Type> *r = new BinTreeNode<Type>(t->data);
			r->leftChild = Copy(t->leftChild);
			r->rightChild = Copy(t->rightChild);
			return r;
 		}
	}
	bool Equal(BinTreeNode<Type> *t1, BinTreeNode<Type> *t2)
	{
		if (NULL == t1 && NULL == t2)
		{
			return true;
		}
		if (NULL != t1 && NULL != t2 && t1->data == t2->data
			Equal(t1->leftChild,t2->leftChild)
			&& Equal(t1->rightChild,t2->rightChild))
		{
			return true;
		}
		return false;
	}
	void LevelOrder(BinTreeNode<Type> *t)
	{
		if (NULL == t)
		{
			return;
		}
		queue<BinTreeNode<Type>* > Q;
		BinTreeNode<Type> *p;
		Q.push(t);
		while (!Q.empty())
		{
			p = Q.front();
			Q.pop();
			cout << p->data << " ";
			if (NULL != p->leftChild )
			{
				Q.push(p->leftChild);
			}
			if (NULL != p->rightChild)
			{
				Q.push(p->rightChild);
			}
		}
	}
	BinTreeNode<Type>* Parent(BinTreeNode<Type> *t, BinTreeNode<Type> *p)
	{
		if (NULL == t || t == p)
		{
			return NULL;
		}
		if (t->leftChild == p || t->rightChild == p)
		{
			return t;
		}
		BinTreeNode<Type> *parent = Parent(t->leftChild, p);
		if (NULL != parent)
		{
			return parent;
		}
		return Parent(t->rightChild, p);
	}
	BinTreeNode<Type>* Parent(BinTreeNode<Type> *t, const Type &key)
	{
// 		if (NULL == t || t->data == key)
// 		{
// 			return NULL;
// 		}
// 		if ((NULL != t->leftChild && t->leftChild == p )
// 			||(NULL != t->rightChild && t->rightChild == p))
// 		{
// 			return t;
// 		}
// 		BinTreeNode<Type> *s = Parent(t->leftChild, key);
// 		if (NULL != s)
// 		{
// 			return s;
// 		}
// 		return Parent(t->rightChild, key);
		BinTreeNode<Type> *index = Search(root, key);
		if (NULL  == index)
		{
			return NULL;
		}
		return Parent(index);
	}
	BinTreeNode<Type>* Search(BinTreeNode<Type> *t, const Type &key)
	{
		if (NULL == t)
		{
			return NULL;
		}
		if(t->data == key)
		{
			return t;
		}
		BinTreeNode<Type> *p = Search(t->leftChild, key);
		if ( NULL != p)
		{
			return p;
		}
		return Search(t->rightChild, key);
	}
	void DestroyBinTree(BinTreeNode<Type> *t)
	{
		if (NULL != t)
		{
			DestroyBinTree(t->leftChild);
			DestroyBinTree(t->rightChild);
			delete t;
			t = NULL;
		}
	}
	int Height(BinTreeNode<Type> *t) const 
	{
		if (NULL == t)
		{
			return 0;
		}
		else
		{
			return
				(Height(t->leftChild) > Height(t->rightChild))
				? (1 + Height(t->leftChild)) : (1 + Height(t->rightChild));
		}
	}
	int Size(BinTreeNode<Type> *t) const 
	{
		if (NULL == t)
		{
			return 0;
		}
		else
		{
			return 1 + Size(t->leftChild) + Size(t->rightChild);
		}
	}
	void PreOrder(BinTreeNode<Type> *t)
	{
		if (NULL != t)
		{
			cout << t->data << " ";
			PreOrder(t->leftChild);
			PreOrder(t->rightChild);
		}
	}
	void InOrder(BinTreeNode<Type> *t)
	{
		if (NULL != t)
		{
			InOrder(t->leftChild);
			cout << t->data << " ";
			InOrder(t->rightChild);
		}
	}
	void PostOrder(BinTreeNode<Type> *t)
	{
		if (NULL != t)
		{
			PostOrder(t->leftChild);
			PostOrder(t->rightChild);
			cout << t->data << " ";
		}
	}
	BinTreeNode<Type>* CreateBinTree_()
	{
		BinTreeNode<Type> *t;
		Type Item;
		cin >> Item;
		if (Item == refvalue)
		{
			return NULL;
		}
		else
		{
			t = new BinTreeNode<Type>(Item);
			t->leftChild = CreateBinTree_();
			t->rightChild = CreateBinTree_();
			return t;
		}
	}
	void CreateBinTree(BinTreeNode<Type> *&t)
	{
		Type Item;
		cin >> Item;
		if (Item == refvalue)
		{
			t = NULL;
		}
		else
		{
			t = new BinTreeNode<Type>(Item);
			CreateBinTree(t->leftChild);
			CreateBinTree(t->rightChild);
		}
	}
	void CreateBinTree(BinTreeNode<Type> *&t, const char *&str)
	{
		if (*str == refvalue || *str == '\0')
		{
			t = NULL;
		}
		else
		{
			t = new BinTreeNode<Type>(*str);
			CreateBinTree(t->leftChild, ++str);
			CreateBinTree(t->rightChild, ++str);
		}
	}
private:
	BinTreeNode<Type> *root;
	Type               refvalue;
};



#include "BinTree.h"
#include<string>

/*void main()
{
	BinTree<char> mytree;
	char *VLR = "ABCDEFGH";
	char *LVR = "CBEDFAGH";
	char *LRV = "CEFDBHGA";

	int n = strlen(VLR);
	mytree.CreateBinTree_Pre(VLR, LVR, n);
	mytree.PreOrder();
	cout<<endl;
    mytree.CreateBinTree_Post(LVR, LRV, n);
	mytree.PostOrder();
	cout << endl;
 }
 */
 void main()
 {
 	BinTree<char> mytree('#');
 	char *str = "ABC##DE##F##G#H##";
 	mytree.CreateBinTree(str);
 	//mytree.CreateBinTree();
 	//mytree.CreateBinTree1();
 	mytree.PreOrder_();
 	cout << endl;
 	mytree.InOrder_();
 	cout << endl;
 	mytree.PostOrder_();
	cout << endl;
 	mytree.LevelOrder();
	cout << endl << endl;
 
 	cout << "Size :" << mytree.Size() << endl;
 	cout << "Height :" << mytree.Height() << endl;
 	BinTreeNode<char> *p = mytree.Search('B');
	BinTreeNode<char> *parent = mytree.Parent('H');

 	BinTree<char> youtree('#');
 	youtree.Copy(mytree);
 	youtree.PreOrder();
 	cout << endl;
 	youtree.DestroyBinTree();
 	cout << youtree.Empty() << endl;
 }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值