您的位置:首页 > 编程语言 > C语言/C++

C++对二叉树的简单实现。

2010-11-20 02:54 591 查看
二叉树结点类的实现如下:

#ifndef BINARYTREENODE_H
#define BINARYTREENODE_H
#include<iostream>
using namespace std;
template<class Type>
class BinaryTreeNode
{
public:
	BinaryTreeNode(){m_leftChild=m_rightChild=NULL;}
	BinaryTreeNode(const Type &data,BinaryTreeNode *leftChild=NULL,
		BinaryTreeNode *rightChild=NULL)
{
	m_data=data;
	m_leftChild=leftChild;
	m_rightChild=rightChild;
}
Type &GetData()
{
	return m_data;
}
BinaryTreeNode<Type>* GetLeftChild()
{
	return m_leftChild;
}
BinaryTreeNode<Type>* GetRightChild()
{
	return m_rightChild;
}
void SetData(const Type &data)
{
	m_data=data;
}
void SetLeftChild(BinaryTreeNode<Type> *leftChild)
{
	m_leftChild=leftChild;
}
void SetRightChild(BinaryTreeNode<Type> *rightChild)
{
	m_rightChild=rightChild;
}
~BinaryTreeNode(){}
private:
	Type m_data;
	BinaryTreeNode<Type> *m_leftChild;
	BinaryTreeNode<Type> *m_rightChild;
};
#endif


二叉树类的实现如下:(方便起见,用了STL中的queue)

#include "BinaryTreeNode.h"
#include<queue>
using namespace std;
template<class T>

class BinaryTree
{
private:
	BinaryTreeNode<T> * m_root;
public:
	BinaryTree()
	{
		m_root=NULL;
	}
	BinaryTree(T data)
	{
		m_root=new BinaryTreeNode<T>(data);
	}
	~BinaryTree();
	bool IsEmpty()const
	{
		return m_root==NULL?true:false;
	}
	bool IsLeftChild(BinaryTreeNode<T>*p)
	{
		return p==GetParent(p)->GetLeftChild()? true:false;
	}
	bool IsRightChild(BinaryTreeNode<T>*p)
	{
		return p==GetParent(p)->GetLeftChild()? true:false;
	}
	BinaryTreeNode<T>* GetRoot()
	{
		return m_root;
	}
	BinaryTreeNode<T>* GetParent(BinaryTreeNode<T>* p)
	{
		return Parent(m_root,p);
	}
	BinaryTreeNode<T>* LetfChild(BinaryTreeNode<T> *root)const
	{
		return root==NULL? NULL:root->GetLeftChild();
	}
	BinaryTreeNode<T>* RightChild(BinaryTreeNode<T>* root)const
	{
		return root==NULL? NULL:root->GetRightChild();
	}
	BinaryTreeNode<T>* LeftSibling(BinaryTreeNode<T>* leftChild);
	BinaryTreeNode<T>* RightSibling(BinaryTreeNode<T>* rightChild);
	T Retrieve(BinaryTreeNode<T> *p)const
	{
		return p->GetData();
	}
	void Assign(BinaryTreeNode<T> *p,const T &d)const
	{
		p->SetData(d);
	}
	void InsertRightChild(BinaryTreeNode<T> *p,const T &d)const;
	void InsertLeftChild(BinaryTreeNode<T> *p,const T &d)const;
	void DeleteRightChild(BinaryTreeNode<T> *p)
	{
		Destroy(p->GetRightChild());
	}
	void DeleteLeftChild(BinaryTreeNode<T> *p)
	{
		Destroy(p->GetLeftChild());
	}
	virtual void LevelOrderTraverse(BinaryTreeNode<T>* root)const;
protected:
	BinaryTreeNode<T>* Parent(BinaryTreeNode<T>* root,BinaryTreeNode<T>* p);
	void Destroy(BinaryTreeNode<T>* p);

};
template<class T>
BinaryTree<T>::~BinaryTree()
{
	Destroy(m_root);
	m_root=NULL;
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::RightSibling(BinaryTreeNode<T>* p)
{
	BinaryTreeNode<T>*q;
	q=Parent(m_root,p);
	if((NULL==q)||(p==q->GetRightChild()))
		return NULL;
	else
		return q->GetRightChild();
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>::LeftSibling(BinaryTreeNode<T>* p)
{
	BinaryTreeNode<T>*q;
	q=Parent(m_root,p);
	if((NULL==q)||(p==q->GetLeftChild()))
		return NULL;
	else
		return q->GetLeftChild();
}
template<class T>
void BinaryTree<T>::InsertLeftChild(BinaryTreeNode<T>* p,const T&d)const
{
	BinaryTreeNode<T>* q=new BinaryTreeNode<T>(d);
	q->SetLeftChild(p->GetLeftChild());
	p->SetLeftChild(q);
}
template<class T>
void BinaryTree<T>::InsertRightChild(BinaryTreeNode<T>* p,const T&d)const
{
	BinaryTreeNode<T>* q=new BinaryTreeNode<T>(d);
	q->SetRightChild(p->GetRightChild());
	p->SetRightChild(q);
}
template<class T>
void BinaryTree<T>::Destroy(BinaryTreeNode<T>*p)
{
	if(NULL!=p)
	{
		Destroy(p->GetLeftChild());
		Destroy(p->GetRightChild());
		
		delete p;

	}

}

template<class T>
BinaryTreeNode<T>* BinaryTree<T>::Parent(BinaryTreeNode<T>*root,BinaryTreeNode<T> *p)
{
	BinaryTreeNode<T>*q;
	if(NULL==root)
		return NULL;
	if((p==root->GetLeftChild())||(p==root->GetRightChild()))
		return root;
	if(NULL!=(q=Parent(root->GetLeftChild(),p)))
		return q;
	else
		return Parent(root->GetRightChild(),p);
}
template<class T>
void BinaryTree<T>::LevelOrderTraverse(BinaryTreeNode<T>*root)const
{
	queue<BinaryTreeNode<T>*> q;
	if(NULL!=root)
		q.push(root);
	while(!q.empty())
	{
		root=q.front(),q.pop();
		cout<<root->GetData();
		if(root->GetLeftChild())
			q.push(root->GetLeftChild());
		if(root->GetRightChild())
			q.push(root->GetRightChild());
	}
}


测试代码:

#include "BinaryTreeNode.h"
#include "BinaryTree.h"
#include<iostream>
using namespace std;
int main()
{
	BinaryTree<char> myBinaryTree('a');
	myBinaryTree.InsertLeftChild(myBinaryTree.GetRoot(),'D');
	myBinaryTree.InsertRightChild(myBinaryTree.GetRoot(),'G');
	myBinaryTree.InsertLeftChild(myBinaryTree.GetRoot(),'B');
	myBinaryTree.InsertRightChild(myBinaryTree.GetRoot()->GetLeftChild(),'E');
	myBinaryTree.InsertRightChild(myBinaryTree.GetRoot(),'C');
	myBinaryTree.InsertLeftChild(myBinaryTree.GetRoot()->GetRightChild(),'F');
	cout<<"这个二叉树是否为空?:"<<myBinaryTree.IsEmpty()<<endl;
	cout<<"这个二叉树的根节点中存储的数据是:"
		<<myBinaryTree.Retrieve(myBinaryTree.GetRoot());
	cout<<endl<<"将二叉树的根节点中的数据赋值为A!";
	myBinaryTree.Assign(myBinaryTree.GetRoot(),'A');
	cout<<"当前二叉树的根节点中存储的数据是:"<<myBinaryTree.Retrieve(myBinaryTree.GetRoot())<<endl;
	cout<<"层次遍历结果如下:"<<endl;
	myBinaryTree.LevelOrderTraverse(myBinaryTree.GetRoot());
	cout<<endl;
	return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: