一、说明
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3e66019c62adc3d033cdba627e102694.png)
二、代码
main.cpp
#include <iostream>
#include "BinaryTree.h"
using namespace std;
int main()
{
cout << "二叉树:" << endl;
BinaryTree<char> tree;
TreeNode<char> A,B,C,D,a,b,c,d,e;
A.data = '+';
B.data = '-';
C.data = '*';
D.data = '/';
a.data = 'A';
b.data = 'B';
c.data = 'C';
d.data = 'D';
e.data = 'E';
tree.root = &A;
A.leftChild = &B;
A.rightChild = &e;
B.leftChild = &C;
B.rightChild = &d;
C.leftChild = &D;
C.rightChild = &c;
D.leftChild = &a;
D.rightChild = &b;
cout<<"前序遍历:"<<endl;
tree.PreOrder();
cout<<endl;
cout<<"中序遍历:"<<endl;
tree.InOrder();//中序遍历
cout<<endl;
cout<<"后序遍历:"<<endl;
tree.PostOrder();
cout<<endl;
cout<<"层序遍历:"<<endl;
tree.LevelOrder();
return 0;
}
BinaryTree.h
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include<iostream>
#include<queue>//C++标准模版库队列
using namespace std;
template <class Type> class BinaryTree;
/*二叉树节点类*/
template <class Type>
class TreeNode
{
public:
TreeNode()
{
leftChild = NULL;
rightChild = NULL;
}
Type data;//数据域
TreeNode<Type> *leftChild;//左孩子
TreeNode<Type> *rightChild;//右孩子
};
/*二叉树类*/
template <class Type>
class BinaryTree
{
public:
TreeNode<Type> *root;//用一个指针指向树根节点
public:
void PreOrder();//前序遍历
void PreOrder(TreeNode<Type> *currentNode);
void InOrder();//中序遍历
void InOrder(TreeNode<Type> *currentNode);
void PostOrder();//后序遍历
void PostOrder(TreeNode<Type> *currentNode);
void LevelOrder();//层序遍历
void Visit(TreeNode<Type> *currnetNode);//显示当前节点
};
/*二叉树类:前序遍历函数*/
template<class Type>
void BinaryTree<Type>::PreOrder()
{
PreOrder(root);
}
/*二叉树类:前序遍历重载函数*/
template <class Type>
void BinaryTree<Type>::PreOrder(TreeNode<Type> *currentNode)
{
if(currentNode)
{
Visit(currentNode);
PreOrder(currentNode->leftChild);
PreOrder(currentNode->rightChild);
}
}
/*二叉树类:中序遍历函数*/
template<class Type>
void BinaryTree<Type>::InOrder()
{
InOrder(root);
}
/*二叉树类:中序遍历重载函数*/
template<class Type>
void BinaryTree<Type>::InOrder(TreeNode<Type> *currentNode)
{
if(currentNode)
{
InOrder(currentNode->leftChild);
Visit(currentNode);
InOrder(currentNode->rightChild);
}
}
/*二叉树类:后续遍历函数*/
template<class Type>
void BinaryTree<Type>::PostOrder()
{
PostOrder(root);
}
/*二叉树类:后序遍历重载函数*/
template<class Type>
void BinaryTree<Type>::PostOrder(TreeNode<Type> *currentNode)
{
if(currentNode)
{
PostOrder(currentNode->leftChild);
PostOrder(currentNode->rightChild);
Visit(currentNode);
}
}
/*二叉树类:层序遍历*/
template<class Type>
void BinaryTree<Type>::LevelOrder()
{
queue<TreeNode<Type>*> q;//声明一个节点结构的队列
TreeNode<Type> *currentNode = root;
while(currentNode)
{
Visit(currentNode);
if(currentNode->leftChild)
{
q.push(currentNode->leftChild);
}
if(currentNode->rightChild)
{
q.push(currentNode->rightChild);
}
if(q.empty())
{
return ;
}
currentNode = q.front();
q.pop();
}
}
/*二叉树类:当前节点输出函数*/
template<class Type>
void BinaryTree<Type>::Visit(TreeNode<Type> *currentNode)
{
cout<<currentNode->data;
}
#endif // BINARYTREE_H
输出结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e21d2431eb354a68a98f174d40e834e3.png)