二叉树的前序,后序,中序,层次遍历

#ifndef BINTREE_H
#define BINTREE_H
#include<iostream>
#include<stack>
#include<queue>
using namespace std;
template<class Type>
class BinTree;
template<class Type>
class BinTreeNode
{
friend class BinTree<Type>;
public:BinTreeNode():data(Type()),LeftChild(NULL),RighChild(NULL)
  {}
  BinTreeNode(Type d,BinTreeNode<Type> *Left = NULL,BinTreeNode<Type>*Right = NULL):data(d),LeftChild(Left),RightChild(Right)
  {}
  ~BinTreeNode()
  {}
private:
Type data;
BinTreeNode<Type>* LeftChild;
BinTreeNode<Type>* RightChild;
};
template<class Type>
class BinTree
{
 public:
BinTree():root(NULL)
{}
BinTree(const BinTree<Type> &bt);
BinTree<Type>& operator=(const BinTree<Type> &bt);
BinTree(Type ref):root(NULL),refval(ref)
{}
public:
bool operator==(const BinTree<Type> &bt)const
{
return Equal(bt);
}
public:
void CreateBinTree()
{CreateBinTree(root);}
void CreateBinTree(char *&str)
{CreateBinTree(root,str);}
public:
int Size()const;
int Height()const;
BinTreeNode<Type> Find(const Type &key)const;
BinTreeNode<Type> Parent(const Type &key)const;
BinTreeNode<Type>* Copy(BinTree<Type> &bt);
bool Equal(const BinTree<Type> &bt)const //bt1 == bt2;  true
{return Equal(root,bt.root);}
public:
void PreOrder()const
{
PreOrder(root);
}
void InOrder()const
{
InOrder(root);
}
void PostOrder()const
{
PostOrder(root);
}
void LevelOrder()const
{
LevelOrder(root);
}
protected:
void PreOrder(BinTreeNode<Type> *t)const;
void InOrder(BinTreeNode<Type> *t)const;
void PostOrder(BinTreeNode<Type> *t)const;
    void LevelOrder(BinTreeNode<Type> *t)const;
protected:
void CreateBinTree(BinTreeNode<Type> *&t);
void CreateBinTree(BinTreeNode<Type> *&t,char *&str);
//bool Equal(BinTreeNode<Type> *t1, BinTreeNode<Type> *t2)const;
private:
BinTreeNode<Type> *root;
Type              refval; //#
};
template<class Type>
void BinTree<Type>::CreateBinTree(BinTreeNode<Type> *&t)
{
Type Item;
cin>> Item;
if(Item == refval)
{
t = NULL;
}
else
{
  t = new BinTreeNode<Type> (Item);//开辟空间,调用构造函数;
  CreateBinTree(t->LeftChild );
  CreateBinTree(t->RightChild );
}
}


template<class Type>//这里的 t 就是root;引用传递直接给root创建结点
void BinTree<Type>::CreateBinTree(BinTreeNode<Type> *&t,char *&str)
{

if(*str == refval)
t = NULL;
else
{
t = new BinTreeNode<Type>(*str);
CreateBinTree(t->LeftChild,++str);
CreateBinTree(t->RightChild,++str);
}
}


template<class Type>//先序遍历
void BinTree<Type>::PreOrder(BinTreeNode<Type> *t)const
{
if(t!=NULL)
{
stack<BinTreeNode<Type>*>st;
st.push(t);
while(!st.empty())
{
t = st.top();
st.pop();
cout<<t->data<<" ";
//根据栈的特点先进后出,所以先人让右结点进去,在让左结点进去;就会先得到左结点再得到右结点;
if(t->RightChild != NULL)
{
st.push(t->RightChild);
}
if(t->LeftChild != NULL)
{
st.push(t->LeftChild);
}
}
}
}


template<class Type>//层次遍历---从上往下打印二叉树;
void BinTree<Type>::LevelOrder(BinTreeNode<Type> *t)const
{
if(t!=NULL)
{
queue<BinTreeNode<Type>*>qe;
qe.push(t);
while(!qe.empty())
{
t = qe.front();
qe.pop();
cout<<t->data<<" ";
if(t->LeftChild != NULL)
{
qe.push(t->LeftChild);
}
if(t->RightChild != NULL)
{
qe.push(t->RightChild);
}
}


}
}


template<class Type>//后序遍历
void BinTree<Type>::PostOrder(BinTreeNode<Type> *t)const
{
stack<BinTreeNode<Type>*> st;
BinTreeNode<Type> *have_visted = NULL;
st.push(t);
while(!st.empty())
{
//先将全部的左孩子都放入栈中;
while(t->LeftChild != NULL && have_visted != t)
{
t = t->LeftChild;
st.push(t);
}
//当他是叶子结点时就访问出栈;
//确保t->RightChild没被访问过;如果已经被访问不在被访问;
if(t->RightChild == NULL && have_visted != t)
{
 t = st.top();
 cout << t->data<<" ";
 st.pop();
 have_visted = t;
}
//得到它的父节点,将右孩子放进去;
t = st.top();
if(t->RightChild != NULL && have_visted != t->RightChild)
{
t = t->RightChild ;
st.push(t);
}
else
{
//如果没有右孩子就直接访问该节点;
   cout << t->data<<" ";
   st.pop();
have_visted = t;
}
}
}
template<class Type>//中序遍历
void BinTree<Type>::InOrder(BinTreeNode<Type> *t)const
{
if( t == NULL)
return;
stack<BinTreeNode<Type>*> st;
BinTreeNode<Type>* have_visted = NULL;
st.push(t);
while(!st.empty())
{
while(t->LeftChild != NULL)
{
t = t->LeftChild;
st.push(t);
}
t= st.top();
cout<<t->data<<" ";
have_visted = t;
st.pop();
if(!st.empty())
{
t= st.top();
cout<<t->data<<" ";
have_visted = t;
st.pop();
}
if(t->RightChild != NULL)
{
t= t->RightChild;
st.push(t);
}
}
}


#endif
//测试代码
#include"bintree.h"
void main()
{
char *str="AB##G#H##";
BinTree<char>bt('#');
bt.CreateBinTree(str);
    bt.InOrder();
cout << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值