二叉树的先中后序遍历,递归遍历
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<stack>
#include<queue>
#include<vector>
using namespace std;
template<class Type>
class BinaryTree
{
private:
struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
Type data;
};
public:
typedef BtNode * PBtNode;
private:
BtNode *root;
Type RefValue;
typedef BtNode NodeType;
static BtNode * _Buynode()
{
BtNode *s = (BtNode *)malloc(sizeof(BtNode));
if(NULL == s) exit(1);
memset(s,0,sizeof(BtNode));
return s;
}
static void _Freenode(BtNode *p)
{
free(p);
}
/*根据字符串创建一棵树*/
BtNode * Create(Type *&str)
{
BtNode * s = NULL;
if(*str != RefValue)
{
s = _Buynode();
s->data = *str;
s->leftchild = Create(++str);
s->rightchild = Create(++str);
}
return s;
}
/*递归先序遍历二叉树*/
static void PreOrder(BtNode *ptr)
{
if(ptr != NULL)
{
cout<<ptr->data<<" ";
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
/*递归中序遍历二叉树*/
static void InOrder(BtNode *ptr)
{
if(ptr != NULL)
{
InOrder(ptr->leftchild);
cout<<ptr->data<<" ";
InOrder(ptr->rightchild);
}
}
/*递归后序遍历二叉树*/
static void PastOrder(BtNode *ptr)
{
if(ptr != NULL)
{
PastOrder(ptr->leftchild);
PastOrder(ptr->rightchild);
cout<<ptr->data<<" ";
}
}
/*二叉树的摧毁*/
static void Destroy(BtNode *ptr)
{
if(ptr != NULL)
{
Destroy(ptr->leftchild);
Destroy(ptr->rightchild);
_Freenode(ptr);
}
}
/*二叉树的查找*/
static BtNode * FindValue(BtNode *broot,const Type &x)
{
BtNode *s = NULL;
if(broot != NULL)
{
if(broot->data == x) return broot;
s = FindValue(broot->leftchild,x );
if(s == NULL)
{
s = FindValue(broot->rightchild ,x);
}
else
{
return s;
}
}
return s;
}
/*查找双亲*/
static BtNode *FindParent(BtNode *broot,const BtNode* child)
{
BtNode *s = NULL;
if(broot != NULL)
{
if(broot->leftchild == child ||broot->rightchild == child) return broot;
s = FindParent(broot->leftchild ,child);
if(s == NULL)
{
return FindParent(broot->rightchild ,child);
}
else
{
return s;
}
}
return s;
}
/*二叉树的节点数目*/
static int Size(BtNode *const broot )
{
if(broot == NULL) return 0;
return Size(broot->leftchild)+Size(broot->rightchild)+1;
}
static int MaxDepth(int a,int b)
{
return a>b?a:b;
}
/*二叉树的深度*/
static int Depth(BtNode *const broot)
{
if(broot == NULL)return 0;
return MaxDepth(Depth(broot->leftchild ),Depth(broot->rightchild ))+1;
}
static int findps(Type value,Type *is,int n)
{
int count = 0;
while(is != NULL)
{
if(*is == value) break;
count++;
is++;
}
if(count == n) return -1;
else return count;
}
/*根据先序遍历和中序遍历创建二叉树*/
static BtNode *CreateTreepi(Type *ps,Type *is,int n,Type refvalue)
{
BtNode *s = NULL;
if(n != 0 && *ps != refvalue && ps != NULL)
{
s = _Buynode();
s->data = *ps;
int index = findps(*ps,is,n);
if(index == -1)exit(1);
s->leftchild = CreateTreepi(ps+1,is,index,refvalue);
s->rightchild = CreateTreepi(ps+index+1,is+index+1,n-index-1,refvalue);
}
return s;
}
/*判断二叉树是否相同*/
static bool equality_tree(BtNode const *aroot,BtNode const *broot)
{
if(aroot != NULL && broot != NULL)
{
if(aroot->data != broot->data)return false;
else
{
return equality_tree(aroot->leftchild,broot->leftchild)
&&equality_tree(aroot->rightchild ,broot->rightchild);
}
}
else if(aroot == NULL && broot == NULL)
{
return true;
}
else
{
return false;
}
}
bool DoseTree1HaveTree2(PBtNode p,PBtNode s)
{
bool result = true;
if(s != NULL && p == NULL || s != NULL && s->data != p->data)
{
result = false;
}
else if(s != NULL && s->data == p->data)
{
result = DoseTree1HaveTree2(p->leftchild,s->leftchild);
if(result)result=DoseTree1HaveTree2(p->rightchild ,s->rightchild);
}
return result;
}
/*判断是否是子树*/
bool HasSubtree(PBtNode p,PBtNode s)
{
bool result = false;
if(p != NULL && s != NULL)
{
if(p->data == s->data)
result = DoseTree1HaveTree2(p,s);
if(!result)
{
result = HasSubtree(p->leftchild,s);
}
if(!result)
{
result = HasSubtree(p->rightchild,s);
}
}
return result;
}
void mirrorRrcursively(PBtNode p)
{
if(p == NULL ||p->leftchild == NULL && p->rightchild == NULL)return;
PBtNode tmp = p->leftchild;
p->leftchild = p->rightchild;
p->rightchild = tmp;
mirrorRrcursively(p->leftchild);
mirrorRrcursively(p->rightchild);
}
void Find_Path(PBtNode proot,Type k1,Type k2)
{
if(proot != NULL)
{
if(proot->data>=k1)
cout<<proot->data<<endl;
if(proot->leftchild != NULL
&& proot->leftchild->data>=k1 &&proot->leftchild->data<=k2)
Find_Path(proot->leftchild,k1,k2);
if(proot->rightchild != NULL
&& proot->rightchild->data>=k1 &&proot->rightchild->data<=k2)
Find_Path(proot->rightchild,k1,k2);
}
}
public:
BinaryTree(const Type &x):root(NULL),RefValue(x) {}
/********************************************************************
/*函数名称:BinaryTree(const BinaryTree<Type> &bt)
/*函数功能:已经有的对象初始化新的对象
/*调用参数:BinaryTree<Type> &bt
/*返回类型:BinaryTree
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str0);
BinaryTree<char> yout(myt);
yout.PreOrder();
*********************************************************************/
PBtNode Copy(const PBtNode broot)
{
PBtNode s = NULL;
if(broot != NULL)
{
s = _Buynode();
s->data = broot->data ;
s->leftchild = Copy(broot->leftchild );
s->rightchild = Copy(broot->rightchild );
}
return s;
}
BinaryTree(const BinaryTree<Type> &bt)
{
if(&bt == NULL) return ;
RefValue = bt.RefValue ;
root = copy(bt.root);
}
/********************************************************************
/*函数名称:operator=(const BinaryTree<Type> &bt)
/*函数功能:已经有的对象赋值已有的的对象
/*调用参数:const BinaryTree<Type> &bt
/*返回类型:BinaryTree<Type> &
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
BinaryTree<char> yout('#');
yout = myt;
yout.PreOrder();
*********************************************************************/
BinaryTree<Type> & operator=(const BinaryTree<Type> &bt)
{
if(this != &bt)
{
Destroy(root);
root = Copy(bt.root);
}
return *this;
}
/********************************************************************
/*函数名称:~BinaryTree()
/*函数功能:释放对象所占的内存
/*调用参数:this
/*返回类型:BinaryTree
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
~BinaryTree()
{
Clear();
}
/********************************************************************
/*函数名称:GetRoot() const
/*函数功能:得到数的根节点地址
/*调用参数:BtNode const *this
/*返回类型:BinaryTree
/*测试用例:
*********************************************************************/
BtNode * GetRoot() const { return root;}
void Clear()
{
Destroy(root);
root = NULL;
}
void Find_Path(Type k1,Type k2)
{
Find_Path(root,k1,k2);
}
/********************************************************************
/*函数名称:CreateTree(Type *str)
/*函数功能:用字符串创建一棵树
/*调用参数:this,Type *str
/*返回类型:void
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
void CreateTree(Type *str)
{
if(str != NULL)
{
root = Create(str);
}
}
/********************************************************************
/*函数名称:CreateTreePI(Type *str)
/*函数功能:用字符串创建一棵树
/*调用参数:this,Type *pi,Type *is
/*返回类型:void
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
*********************************************************************/
void CreateTreePI(Type *pi,Type *is)
{
int n = strlen(pi);
root = CreateTreepi(pi,is,n,RefValue);
}
void PreOrder() const
{
PreOrder(root);
cout<<endl;
}
void InOrder() const
{
InOrder(root);
cout<<endl;
}
void PastOrder() const
{
PastOrder(root);
cout<<endl;
}
/********************************************************************
/*函数名称:FindValue(const Type &x) const
/*函数功能:寻找节点在树上的地址
/*调用参数:const Type &x,BinaryTree<Type> * const this
/*返回类型:BtNode *
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
char x = 'q';
BinaryTree<char> myt('#');
myt.CreateTree(str0);
myt.PreOrder();
BinaryTree<char>::PBtNode p =NULL;
while(x != '#')
{
cin>>x;
p = myt.FindValue(x);
cout<<p<<endl;
}
*********************************************************************/
BtNode * FindValue(const Type &x) const
{
if(this == NULL) return NULL;
return findvalue(root,x);
}
/********************************************************************
/*函数名称:FindParent(const BtNode *child) const
/*函数功能:已知节点地址,找父亲节点地址
/*调用参数:const BtNode *child,BinaryTree<Type> * const this
/*返回类型:BtNode *
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF###G#H##";
char *str2="ABDE####C#FG##H##";
char x = 'q';
BinaryTree<char> myt('#');
myt.CreateTree(str0);
myt.PreOrder();
BinaryTree<char>::PBtNode p =NULL,pa = NULL;
while(x != '#')
{
cin>>x;
p = myt.FindValue(x);
cout<<p<<endl;
pa = myt.FindParent(p);
cout<<pa<<endl;
cout<<endl;
}
*********************************************************************/
BtNode * FindParent(const BtNode *child) const
{
if(this == NULL ||child == NULL) return NULL;
return findparent(root,child);
}
/********************************************************************
/*函数名称:Size() const
/*函数功能:求树节点的大小
/*调用参数:BtNode const *this
/*返回类型:int
/*测试用例:char *str0="ABC##DE##F##G#H##";
char *str1="ABCE###DF####";
char *str2="ABDE####C##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
myt.PreOrder();
cout<<myt.Size()<<endl;
*********************************************************************/
int Size() const
{
if(this == NULL) return -1;
return Size(root);
}
int Depth() const
{
if(this == NULL) return -1;
return Depth(root);
}
/********************************************************************
/*函数名称:operator == (const BinaryTree<Type> &bt) const
/*函数功能:判断二叉树是否相等
/*调用参数:BtNode const *this,const BinaryTree<Type> &bt
/*返回类型:boll
/*测试用例:char *ps1="ABCDEFGH";
char *is1="CBEDFAGH";
char *ps2="ABDEHCFIG";
char *is2="DBHEAFICG";
char *ps3="ABDECFG";
char *is3="DBEAFCG";
BinaryTree<char> myt('#');
myt.CreateTreePI(ps2,is2);
BinaryTree<char> yout('#');
yout.CreateTreePI(ps3,is3);
cout<<(myt != yout)<<endl;
cout<<(myt == yout)<<endl;
*********************************************************************/
bool operator==(const BinaryTree<Type> &bt) const
{
if(this == NULL ||&bt == NULL) return false;
if(this == &bt) return true;
if(RefValue != bt.RefValue) return false;
return equality_tree(root,bt.root);
}
/********************************************************************
/*函数名称:operator != (const BinaryTree<Type> &bt) const
/*函数功能:判断二叉树是否不等
/*调用参数:BtNode const *this,const BinaryTree<Type> &bt
/*返回类型:boll
/*测试用例:
*********************************************************************/
bool operator != (const BinaryTree<Type> &bt) const
{
return !(*this == bt);
}
/*判断是否为子结构*/
bool HasSubtree(const BinaryTree<Type> &bt)
{
return HasSubtree(this->GetRoot(),bt.GetRoot());
}
/*求二叉树的镜像*/
void mirrorRrcursively()
{
mirrorRrcursively(this->GetRoot());
}
};
/*二叉树遍历迭代器抽象类*/
template<class Type>
class TreeIterator
{
protected:
BinaryTree<Type> &tree;
typename BinaryTree<Type>::PBtNode _Ptr;
public:
TreeIterator(BinaryTree<Type> &bt):tree(bt),_Ptr(NULL)
{}
Type &operator*() { return _Ptr->data;}
const Type & operator*() const { return _Ptr->data;}
bool IsDone() const { return _Ptr == NULL;}
virtual void First() = 0;
virtual void operator++() = 0;
};
template<typename Type>
void Printf_Iterator(TreeIterator<Type> &Tree)
{
Tree.First();
while(!Tree.IsDone())
{
cout<<*Tree<<" ";
++Tree;
}
cout<<endl;
}
先序非递归遍历二叉树
template<class Type>
class PreIterator:public TreeIterator<Type>
{
stack<typename BinaryTree<Type>::PBtNode> st;
public:
PreIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = NULL;
if(tree.GetRoot() != NULL)
{
_Ptr = tree.GetRoot();
if(_Ptr->rightchild != NULL)
st.push(_Ptr->rightchild);
if(_Ptr->leftchild != NULL)
st.push(_Ptr->leftchild);
}
}
virtual void operator++()
{
if(st.empty())
{
_Ptr = NULL;
return ;
}
else
{
_Ptr = st.top();st.pop();
if(_Ptr->rightchild != NULL)st.push(_Ptr->rightchild);
if(_Ptr->leftchild != NULL)st.push(_Ptr->leftchild);
}
}
};
中序非递归遍历二叉树
template<typename Type>
class StkNode
{
public:
typename BinaryTree<Type>::PBtNode pnode;
int popnum;
public:
StkNode(typename BinaryTree<Type>::PBtNode p = NULL):pnode(p),popnum(0){}
};
template<class Type>
class InIterator:public TreeIterator<Type>
{
private:
stack<StkNode<Type> > st;
public:
InIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = tree.GetRoot();
while( _Ptr != NULL)
{
StkNode<Type> node(_Ptr);node.popnum = 1;
st.push(node);
_Ptr = _Ptr->leftchild;
}
typename StkNode<Type> node;
node = st.top();st.pop();
if(node.pnode ->rightchild != NULL)
st.push(StkNode<Type>(node.pnode->rightchild) );
_Ptr = node.pnode;
}
virtual void operator++()
{
if(st.empty())
{
_Ptr = NULL;
return;
}
else
{
_Ptr = NULL;
while(_Ptr == NULL)
{
typename StkNode<Type> node;
node = st.top();st.pop();
if(++node.popnum == 2)
{
_Ptr = node.pnode;
if(node.pnode ->rightchild != NULL)
st.push(StkNode<Type>(node.pnode->rightchild) );
}
else if(node.popnum == 1)
{
st.push(node);
if(node.pnode ->leftchild != NULL)
st.push(StkNode<Type>(node.pnode->leftchild));
}
}
}
}
};
后序非递归遍历二叉树
template<class Type>
class PastIterator : public TreeIterator<Type>
{
protected:
stack<typename StkNode<Type> > st;
public:
PastIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = tree.GetRoot();
while( _Ptr != NULL)
{
StkNode<Type> node(_Ptr);
node.popnum = 1;
st.push(node);
_Ptr = _Ptr->leftchild;
}
typename StkNode<Type> node;
node = st.top();st.pop();
if(node.pnode ->rightchild != NULL)
st.push(StkNode<Type>(node.pnode->rightchild) );
_Ptr = node.pnode;
}
virtual void operator++()
{
if(st.empty())
{
_Ptr = NULL;
return;
}
StkNode<Type> node;
for(;;)
{
node = st.top(); st.pop();
if(++node.popnum == 3)
{
_Ptr = node.pnode;
return ;
}
st.push(node);
if(node.popnum == 1 && node.pnode->leftchild != NULL)
{
st.push(StkNode<Type>(node.pnode->leftchild));
}else if(node.popnum == 2 && node.pnode->rightchild != NULL)
{
st.push(StkNode<Type>(node.pnode->rightchild));
}
}
}
};
测试用例
int main()
{
char *str1="ABCE###DF###G#H##";
char *str2="321###4#865##7##9#A##";
BinaryTree<char> myt('#');
myt.CreateTree(str2);
PreIterator<char> preIt(myt);
Printf_Iterator(preIt);
InIterator<char> InIt(myt);
Printf_Iterator(InIt);
PastIterator<char> PastIt(myt);
Printf_Iterator(PastIt);
}
运行结果
3 2 1 4 8 6 5 7 9 A
1 2 3 4 5 6 7 8 9 A
1 2 5 7 6 A 9 8 4 3
请按任意键继续. . .