BinaryTreeNode.h
/* BinaryTreeNode.h */
template <class T> class BinaryTree;
template <class T>
class BinaryTreeNode {
friend class BinaryTree<T>; //声明二叉树为结点类的友元类,便于访问私有数据成员
private:
T info; //二叉树结点数据域
BinaryTreeNode<T>* left; //二叉树结点指向左子树的指针
BinaryTreeNode<T>* right; //二叉树结点指向左子树的指针
public:
BinaryTreeNode(); //缺省构造函数
BinaryTreeNode(const T& inf); //给定数据的构造函数
BinaryTreeNode(const T& inf,BinaryTreeNode<T>* l, BinaryTreeNode<T>* r);//给定了结点值和左右子树的构造函数
T value() const; //返回当前结点的数据
BinaryTreeNode<T>* leftchild() const; //返回当前结点左子树
BinaryTreeNode<T>* rightchild() const; //返回当前结点右子树
void setLeftchild(BinaryTreeNode<T>*) ; //设置当前结点的左子树
void setRightchild(BinaryTreeNode<T>*) ; //设置当前结点的右子树
void setValue(const T& val); //设置当前结点的数据域
bool isLeaf() const; //判定当前结点是否为叶结点,若是返回true
BinaryTreeNode<T>& operator = (const BinaryTreeNode<T>& Node){this = Node;};//重载赋值操作符
};
//****** BinaryTreeNode Implementation *******//
template<class T>
BinaryTreeNode<T>::BinaryTreeNode() {
left = right = NULL;
}
template<class T>
BinaryTreeNode<T>::BinaryTreeNode(const T& inf) { //给定数据的构造函数
info = inf;
left = right = NULL;
}
template<class T>
BinaryTreeNode<T>::BinaryTreeNode(const T& inf,BinaryTreeNode* l, BinaryTreeNode* r) {//给定数据的左右指针的构造函数
info = inf;
left = l;
right = r;
}
template<class T>
T BinaryTreeNode<T>::value() const {
return info;
}
template<class T>
BinaryTreeNode<T>* BinaryTreeNode<T>::leftchild() const { //返回当前结点指向左子树的指针
return left;
}
template<class T>
BinaryTreeNode<T>* BinaryTreeNode<T>::rightchild() const { //返回当前结点指向右子树的指针
return right;
}
template<class T>
void BinaryTreeNode<T>::setLeftchild(BinaryTreeNode<T>* subroot) { //设置当前结点的左子树
left = subroot;
}
template<class T>
void BinaryTreeNode<T>::setRightchild(BinaryTreeNode<T>* subroot) { //设置当前结点的右子树
right = subroot;
}
template<class T>
void BinaryTreeNode<T>::setValue(const T& val) { //设置当前结点的数据域
info = val;
}
template<class T>
bool BinaryTreeNode<T>::isLeaf() const { //判定当前结点是否为叶结点,若是返回true
return (left == NULL) && (right == NULL);
}
BinaryTree.h
//************BinaryTree.h****************//
#include <stack>
#include <queue>
#include "BinaryTreeNode.h"
enum Tags{Left,Right}; //枚举类型
template <class T>
class StackElement { //StackElement,用于非递归方式遍历二叉树
public:
BinaryTreeNode<T>* pointer;
Tags tag;
};
using namespace std;
template <class T>
class BinaryTree {
private:
BinaryTreeNode<T>* root; //二叉树根结点
public:
BinaryTree(){root = NULL;}; //构造函数
~BinaryTree() {DeleteBinaryTree(root);}; //析构函数
bool isEmpty() const{return ( root? false : true); };//判定二叉树是否为空树
BinaryTreeNode<T>* Root(){return root;}; //返回二叉树根结点
BinaryTreeNode<T>* CreateBinaryTree(); //创建二叉树,返回指向所创建的二叉树的根结点的指针
void PreOrder(BinaryTreeNode<T>* root); //前序周游二叉树或其子树
void InOrder(BinaryTreeNode<T>* root); //中序周游二叉树或其子树
void PostOrder(BinaryTreeNode<T>* root); //后序周游二叉树或其子树
void LevelOrder(BinaryTreeNode<T>* root); //按层次周游二叉树或其子树
void DeleteBinaryTree(BinaryTreeNode<T>* root); //删除二叉树或其子树
void Visit(T Value) {cout << Value<<" ";}; //访问
};
template<class T>
void BinaryTree<T>:: DeleteBinaryTree(BinaryTreeNode<T>* root) { //以后序周游的方式删除二叉树
if(root) {
DeleteBinaryTree(root->left); //递归删除左子树
DeleteBinaryTree(root->right); //递归删除右子树
delete root; //删除根结点
}
}
template<class T>
BinaryTreeNode<T>* BinaryTree<T>:: CreateBinaryTree() {
//按前序构造二叉链表表示的二叉树root,元素类型为字符型
BinaryTreeNode<T> * N;
T ch;
cin>>ch;
if(ch=='*')N=NULL; //*表示空子树
else{
N = new BinaryTreeNode<T>(ch); //创建新树
N->left =CreateBinaryTree(); //递归创建左子树
N->right=CreateBinaryTree(); //递归创建右子树
}
return root=N;
}
template<class T>
void BinaryTree<T>::PreOrder (BinaryTreeNode<T>* root) { //递归前序周游二叉树
if(root!=NULL)
{
Visit(root->info);
PreOrder(root->leftchild());
PreOrder(root->rightchild());
}
}
template<class T>
void BinaryTree<T>:: InOrder (BinaryTreeNode<T>* root) { //递归中序周游二叉树
if(root!=NULL)
{
InOrder(root->leftchild());
Visit(root->info);
InOrder(root->rightchild());
}
}
template<class T>
void BinaryTree<T>:: PostOrder (BinaryTreeNode<T>* root) { //递归后序周游二叉树
if(root!=NULL)
{
PostOrder(root->leftchild());
PostOrder(root->rightchild());
Visit(root->info);
}
}
template<class T>
void BinaryTree<T>::LevelOrder(BinaryTreeNode<T>* root) {
//按层次周游二叉树或其子树
using std::queue;
queue<BinaryTreeNode<T>*> aQueue;
BinaryTreeNode<T> * pointer=root;
if(pointer) //根不为空
{
aQueue.push(pointer); //根结点入队列
}
while(!aQueue.empty()) //若队列不为空
{
pointer=aQueue.front(); //取出队头
aQueue.pop();
Visit(pointer->info);
if(pointer->leftchild()!=NULL)
{
aQueue.push(pointer->leftchild());
}
if(pointer->rightchild()!=NULL)
{
aQueue.push(pointer->rightchild());
}
}
}
BinaryTree.cpp
//BinaryTree.cpp
//创建一棵如下所示的二叉树 (如图5.5所示)
// A
// / \
// B C
// / \ \
// D E F
// / / \
// G H I
//
#include <iostream>
#include "BinaryTree.h"
int main() {
//建一棵二叉树(如图5.5所示)
//输入前序序列(*表示NULL):ABD**EG***C*FH**I**
BinaryTree<char> a;
cout<<"input node(preorder):";
BinaryTreeNode<char> * root=a.CreateBinaryTree();
cout<<"PreOrderTraversal: ";
a.PreOrder(root); //前序周游
cout<<endl;
cout<<"InOrderTraversal: ";
a.InOrder(root); //中序周游
cout<<endl;
cout<<"PostOrderTraversal: ";
a.PostOrder(root); //后序周游
cout<<endl;
cout<<"LevelOrderTraversal: ";
a.LevelOrder(root); //层次周游
cout<<endl;
return 0;
}