二叉树的C++实现

关于二叉树的实现直接贴代码吧:

// BinaryTree_Class.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include "stdafx.h"
#include <iostream>
#include<string.h>
#include<stack> 
using namespace std;

template<class T>
struct _BinNode
{
	T data;
	struct _BinNode *lchild,*rchild;
};

template<class T>
class BinTree
{
public:
	typedef struct _BinNode<T> BinNode;
	//声明根结点成员
	BinNode *root;
public:
	BinTree();
	~BinTree();

	void createTree( char *s);
	void printInorder(BinNode *pRoot) const;
	void _printInorder(BinNode *pRoot) const;
	void printPreOrder(BinNode *pRoot) const;
	void _printPreOrder(BinNode *pRoot) const;
	void printPostOrder(BinNode *pRoot) const;
	void _printPostOrder(BinNode *pRoot) const;
	int size(BinNode *T) const;
	int Height(BinNode *T) const;
	BinNode * _allocate();		//产生一个新的结点
};

template<class T>
typename BinTree<T>::BinNode * BinTree<T>::_allocate()
{
	BinNode *newNode = new struct _BinNode<T>;
	newNode->lchild = NULL;
	newNode->rchild = NULL;
	newNode->data = NULL;
	return newNode;
}

template<class T>
BinTree<T>::BinTree()
{
	root = _allocate();
}

template<class T>
BinTree<T>::~BinTree()
{
	delete root;
	root = NULL; //记住在用delete释放root的内存后,root成为了悬空指针,需要让它指为空,否则很危险
}

template<class T> 
void BinTree<T>::createTree( char *s)  //形如A(B,C(D,E))
{
	stack<T> s1;
	stack<typename BinTree<T>::BinNode *> s2;  //用于存放结点
	root->data = s[0];
	s2.push(root);
	bool isRight = false;  //
	int i=1;
	while(i<strlen(s))
	{
		if(s[i] == '(')
		{
			isRight = false;
			s1.push(s[i]);
		}
		else if(s[i] == ')')
		{
			s1.pop();
			s2.pop();
		}
		else if(s[i] == ',')
			isRight = true;
		else
		{
			//结点
			typename BinTree<T>::BinNode *newNode = _allocate();
			newNode->data = s[i];
			typename BinTree<T>::BinNode *temp = s2.top();
			if(isRight)
				temp->rchild = newNode;
			else
				temp->lchild = newNode;

			if(s[i+1] == '(')
				s2.push(newNode);
		}
		i++;
	}
}

//中序递归遍历
template<class T>
void BinTree<T>::printInorder(BinNode *pRoot) const
{
	if(pRoot!=NULL)
	{
		printInorder(pRoot->lchild);
		cout<<pRoot->data<<',';
		printInorder(pRoot->rchild);

	}
}

//中序非递归遍历
template<class T>
void BinTree<T>::_printInorder(BinNode *pRoot) const
{
	stack<BinNode *> s;
	BinNode *p = pRoot;
	while(!s.empty() ||p!=NULL)
	{
		while(p!=NULL)
		{
			s.push(p);
			p = p->lchild;
		}

		if(!s.empty())
		{
			BinNode *temp = s.top();
			s.pop();
			cout<<temp->data<<',';
			p = temp->rchild;
		}
	}
}

template<class T>
void BinTree<T>::printPreOrder(typename BinTree<T>::BinNode *pRoot) const
{
	if(pRoot!=NULL)
	{
		cout<<pRoot->data<<',';
		printPreOrder(pRoot->lchild);
		
		printPreOrder(pRoot->rchild);
	}
}

template<class T>
void BinTree<T>::_printPreOrder(BinNode *pRoot) const
{
	stack<BinNode *> s;
	BinNode *p = pRoot;
	while(!s.empty() || p!=NULL)
	{
		while(p!=NULL)
		{
			cout<<p->data<<',';
			s.push(p);
			p = p->lchild;
		}
		if(!s.empty())
		{
			BinNode *temp = s.top();
			s.pop();
			p = temp->rchild;
		}
	}
}
template<class T>
void BinTree<T>::printPostOrder(typename BinTree<T>::BinNode *pRoot) const
{
	if(pRoot!=NULL)
	{
		printPostOrder(pRoot->lchild);
		printPostOrder(pRoot->rchild);
		cout<<pRoot->data<<',';
	}
}
int _tmain(int argc, _TCHAR* argv[])
{
	 char *s = "A(B,C(D,E))";
	BinTree<char> *bTree = new BinTree<char>;
	bTree->createTree(s);
	cout<<"中序遍历:";
	bTree->printInorder(bTree->root);
	bTree->_printInorder(bTree->root);
	cout<<"前序遍历:";
	bTree->printPreOrder(bTree->root);
	bTree->_printPreOrder(bTree->root);
	cout<<"后序遍历:";
	bTree->printPostOrder(bTree->root);
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值