二叉树---封装类
给各位集美们参考,欢迎大佬指出问题,优问题找qq:1808113578
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
//结点类
template<class T>
class TreeNode{
public :
TreeNode(T val) :value(val) {this->left = NULL;this->right = NULL;}
TreeNode() {}
T value;
TreeNode *left;
TreeNode *right;
};
//树类
template<class T>
class Tree {
public :
Tree();
void PreOrderBuildTree(); //先序建树
//三种递归遍历
void Recur_PreOrder (); //递归前序遍历
void Recur_InOrder (); //递归中序遍历
void Recur_PostOrder(); //递归后序遍历
//四种非递归遍历
void LevelOrder(); //层序遍历
void None_Recur_PreOrder (); //非递归前序遍历
void None_Recur_InOrder (); //非递归中序遍历
void None_Recur_PostOrder(); //非递归后序遍历
private:
TreeNode<T>* PBT();
void preOrder (TreeNode<T> *root); //递归前序遍历
void inOrder (TreeNode<T> *root); //递归中序遍历
void postOrder (TreeNode<T> *root); //递归后序遍历
void levelOrder(TreeNode<T> *root); //层序遍历
void none_Recur_PreOrder (TreeNode<T> *root);//非递归前序遍历
void none_Recur_InOrder (TreeNode<T> *root);//非递归中序遍历
void none_Recur_PostOrder(TreeNode<T> *root);//非递归后序遍历
private:
TreeNode<T> *root;
};
template<class T>
inline Tree<T>::Tree()
{
this->root = NULL;
}
template<class T>
inline void Tree<T>::PreOrderBuildTree()
{
std::cout << "请输入一个先序序列" << std::endl;
this->root = this->PBT();
}
template<class T>
inline TreeNode<T>* Tree<T>::PBT()
{
T a;
TreeNode<T> *node = NULL;
std::cin >> a;
if (a != '#' && a != '-')
{
node = new TreeNode<T>(a);
node->left = this->PBT();
node->right = this->PBT();
}
return node;
}
template<class T>
inline void Tree<T>::Recur_PreOrder()
{
std::cout << "使用递归进行前序遍历" << std::endl;
this->preOrder(this->root);
std::cout<<std::endl << std::endl;
}
template<class T>
inline void Tree<T>::Recur_InOrder()
{
std::cout << "使用递归进行中序遍历" << std::endl;
this->inOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::Recur_PostOrder()
{
std::cout << "使用递归进行后序遍历" << std::endl;
this->postOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::LevelOrder()
{
std::cout << "进行层序遍历" << std::endl;
this->levelOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::None_Recur_PreOrder()
{
std::cout << "使用非递归进行前序遍历" << std::endl;
this->none_Recur_PreOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::None_Recur_InOrder()
{
std::cout << "使用非递归进行中序遍历" << std::endl;
this->none_Recur_InOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::None_Recur_PostOrder()
{
std::cout << "使用非递归进行后序遍历" << std::endl;
this->none_Recur_PostOrder(this->root);
std::cout << std::endl << std::endl;
}
template<class T>
inline void Tree<T>::preOrder(TreeNode<T>* root)
{
if (root != NULL)
{
std::cout << root->value << " ";
preOrder(root->left);
preOrder(root->right);
}
return;
}
template<class T>
inline void Tree<T>::inOrder(TreeNode<T>* root)
{
if (root != NULL)
{
inOrder(root->left);
std::cout << root->value << " ";
inOrder(root->right);
}
return;
}
template<class T>
inline void Tree<T>::postOrder(TreeNode<T>* root)
{
if (root != NULL)
{
postOrder(root->left);
postOrder(root->right);
std::cout << root->value << " ";
}
return;
}
template<class T>
inline void Tree<T>::levelOrder(TreeNode<T>* root)
{
std::queue<TreeNode<T>*> q;
q.push(root);
while (!q.empty())
{
TreeNode<T> *now = q.front();
std::cout << now->value << " ";
if (now->left != NULL)q.push(now->left);
if (now->right != NULL)q.push(now->right);
q.pop();
}
return;
}
template<class T>
inline void Tree<T>::none_Recur_PreOrder(TreeNode<T>* root)
{
if (root == NULL)return;
TreeNode<T> *p = root;
std::stack<TreeNode<T>*> s;
while (!s.empty() || p)
{
//边遍历边打印,并存入栈
while (p)
{
std::cout << p->value << " ";
s.push(p);
p = p->left;
}
//当p为空时说明根和左子树都遍历完成,进入右子树
if (!s.empty())
{
p = s.top();
s.pop();
p = p->right;
}
}
}
template<class T>
inline void Tree<T>::none_Recur_InOrder(TreeNode<T>* root)
{
if (root == NULL)return;
TreeNode<T> *p = root;
std::stack<TreeNode<T> *> s;
while (!s.empty() || p)
{
while (p)
{
s.push(p);
p = p->left;
}
//当p为空时说明根和左子树都遍历完成,进入右子树
if (!s.empty())
{
p = s.top();
s.pop();
std::cout << p->value << " ";
p = p->right;
}
}
}
int main()
{
// ABDH##I##E##CF#J##G##
cout << "---------二叉树的创建以及遍历----------" << endl;
Tree<char> tree;
tree.PreOrderBuildTree();
tree.Recur_PreOrder();
tree.Recur_InOrder();
tree.Recur_PostOrder();
tree.LevelOrder();
tree.None_Recur_PreOrder();
tree.None_Recur_InOrder();
system("pause");
return 0;
}