二叉树

************************* binaryTree.h *************************
#include <iostream>
using namespace std;

template <typename T>
class BinaryTree<T>;

template <typename T>
class Node
{
	friend class BinaryTree<T>;
public:
	Node( const T& el )
	{
		value = el;
		left = right = NULL;
	}
	~Node(){};
private:
	T value;
	Node *left, *right;
};

template <typename T>
class BinaryTree
{
public:
	BinaryTree();
	~BinaryTree();
	
	bool isEmpty();
	void insertNode( const T& el );
	void deleteNode( const T& el );
	void balanceTree( T data[], int first, int last );
	
	void preorder();
	void inorder();
	void postorder();
	void levelorder();
	
	bool search( const T& el );
	int  height();
private:
	Node<T> *root;
	void clear( Node<T> *p );
	int height( Node<T> *p );
};

************************* binaryTree.cpp *************************
#include <iostream>
#include "binaryTree.h"
#include <stack>
#include <queue>
using namespace std;

template <typename T>
BinaryTree::BinaryTree()
{
	root = NULL;
}

template <typename T>
BinaryTree::~BinaryTree()
{
	clear(root);
}

template <typename T>
void BinaryTree::clear( Node<T> *p )
{
	if( NULL != p->left )
		clear( p->left );
	if( NULL != p->right )
		clear( p->right );
	delete p;
}

template <typename T>
bool BinaryTree::isEmpty()
{
	return root == NULL;
}

template <typename T>
void BinaryTree::insertNode( const T& el )
{
	Node<T> *pNew = new Node<T>(el);
	if(isEmpty())
	{
		root = pNew;
	}
	else
	{
		Node<T> *pCur = root;
		Node<T> *pPre = NULL;
	    int flag;
		while( pCur != NULL )
		{
			if( pCur->value < el )
			{
				pPre = pCur;
				pCur = pCur->right;
				flag = 0;
			}
			else
			{
				pPre = pCur;
				pCur = pCur->left;
				flag = 1;
			}
		}
		if( 0 == flag )
		{
			pPre->right = pNew;
		}
		else
		{
			pPre->left = pNew;
		}	
	}
}

template <typename T>
void BinaryTree::deleteNode( const T& el )
{
	if( !isEmpty() )
	{
		Node<T> *pCur = root;
		Node<T> *pPre = NULL;
	    int flag;
		
		while( pCur != NULL )
		{
			if( pCur->value == el )
			{
				break;
			}
			else if( pCur->value < el )
			{
				pPre = pCur;
				pCur = pCur->right;
				flag = 0;
			}
			else
			{
				pPre = pCur;
				pCur = pCur->left;
				flag = 1;
			}
		}
		if( NULL == pCur )
		{
			printf("Node Not Found");
			return;
		}
		else
		{
			if( pCur == root )
			{
				if( root->left != NULL && root->right != NULL )
				{
					Node<T> *tmp = root->left;
					while( tmp->right != NULL )
					{
						tmp = tmp->right;
					}
					tmp->right = root->right;
					root = tmp;
				}
				else if( NULL == root->left && NULL != root->right )
				{
					root = root->right;
				}
				else if( NULL != root->left && NULL == root->right )
				{
					root = root->left;
				}
				else
				{
					root = NULL;
				}
			}
			else
			{
				if( pCur->left != NULL && pCur->right != NULL )
				{
					Node<T> *tmp = pCur->left;
					while( tmp->right != NULL )
					{
						tmp = tmp->right;
					}
					tmp->right = pCur->right;
					if( 0 == flag )
					{
						pPre->right = tmp;
					}
					else
					{
						pPre->left = tmp;
					}
				}
				else if( pCur->left == NULL && pCur->right != NULL )
				{
					if( 0 == flag )
					{
						pPre->right = pCur->right;
					}
					else
					{
						pPre->left = pCur->right;
					}
				}
				else if( pCur->left != NULL && pCur->right == NULL )
				{
					if( 0 == flag )
					{
						pPre->right = pCur->left;
					}
					else
					{
						pPre->left = pCur->left;
					}
				}
				else
				{
					if( 0 == flag )
					{
						pPre->right = NULL;
					}
					else
					{
						pPre->left = NULL;
					}
				}
			}
			delete pCur;
		}
	}
}

template <typename T>
void BinaryTree::preorder( Node<T> *p )
{
	if( p != NULL )
	{
		cout << p->value << endl;
		preorder( p->left );
		preorder( p->right );
	}
}

template <typename T>
void BinaryTree::preorder()
{
	if(!isEmpty())
	{
		stack<Node<T> *> nodeStack;
		Node<T> *p = root;
		nodeStack.push(p);
		while(!nodeStack.empty())
		{
			p = nodeStack.pop();
			cout << p->value << endl;
			if( p->right != NULL )
				nodeStack.push( p->right );
			if( p->left != NULL )
				nodeStack.push( p->left );
		}
	}
	else
	{
		printf("Empty Tree");
	}
}

template <typename T>
void BinaryTree::inorder()
{
	if(!isEmpty())
	{
		stack<Node<T> *> nodeStack;
		Node<T> *p = root;
		while( !nodeStack.empty() || p != NULL )
		{
			if( p != NULL )
			{
				nodeStack.push(p);
				p = p->left;
			}
			else
			{
				p = nodeStack.pop();
				cout << p->value << endl;
				p = p->right;
			}
		}
	}
	else
	{
		printf("Empty Tree");
	}
}

template <typename T>
void BinaryTree::postorder()
{
	if(!isEmpty())
	{
		stack<Node<T> *> nodeStack;
		Node<T> *p = root, *q = root;
		while( p != NULL )
		{
			for( ; p->left != NULL; p = p->left )
				nodeStack.push(p);
			while( p != NULL && (p->right == NULL || p->right == q) )
			{
				cout << p->value << endl;
				q = p;
				if(nodeStack.empty())
					return;
				p = nodeStack.pop();
			}
			nodeStack.push(p);
			p = p->right;
		}
	}
	else
	{
		printf("Empty Tree");
	}
}

template <typename T>
void BinaryTree::levelorder()
{
	if(!isEmpty())
	{
		queue<Node<T> *> nodeQueue;
		Node<T> *p = root;
		nodeQueue.enqueue(p);
		while( !nodeQueue.empty() )
		{
			p = nodeQueue.dequeue();
			cout << p->value << endl;
			if( p->left != NULL )
				nodeQueue.enqueue( p->left );
			if( p->right != NULL )
				nodeQueue.enqueue( p->right );
		}
	}
	else
	{
		cout << "Empty Tree" << endl;
	}
}

template <typename T>
bool BinaryTree::search( const T& el )
{
	if(!isEmpty())
	{
		Node<T> *p = root;
		while( p != NULL )
		{
			if( p->value == el )
				return true;
			else if( p->value < el )
				p = p->right;
			else
				p = p->left;
		}
		return false;
	}
	else
	{
		return false;
	}
}

template <typename T>
void BinaryTree::balanceTree( T data[], int first, int last )
{
	if( first <= last )
	{
		int mid = (first + last)/2;
		insert(data[mid]);
		balanceTree(data, first, mid -1);
		balanceTree(data, last, mid +1);
	}
}

template <typename T>
int BinaryTree::height()
{
	return height(root);
}

template <typename T>
int BinaryTree::height( Node<T> *p )
{
	if( p == NULL )
		return 0;
		
	int lh = height( p->left );
	int rh = height( p->right );

	return lh > rh ? (lh+1) : (rh+1);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值