
急!求帮忙解读一个C++版数据结构的程序的头文件,尽可能详细,最好每一句都有注释,拜谢~~
#ifndefH_BinaryExpTree_H#defineH_BinaryExpTree_Husingnamespacestd;structBTnode{intdat...
#ifndef H_BinaryExpTree_H
#define H_BinaryExpTree_H
using namespace std;
struct BTnode
{
int data;
BTnode *Lchild;
BTnode *Rchild;
};
class BinaryExpTree
{
public:
BinaryExpTree(): m_root(NULL){}
~BinaryExpTree ();
void Create(char ch1[],char ch2[],int ); // 以二叉树的先序和中序次序建立二叉树的接口函数
int Evaluate();
private:
BTnode *m_root;
void _DestroyBT(BTnode* &);
int _Evaluate(BTnode* &);
int _Opreate(const int &,const char &,const int &);
void _Create(BTnode * &,char ch1[],char ch2[],int ,int ,int &);//已知二叉树的先序遍历次序及中序遍历次序,建立二叉树T
};
//析构函数, 释放二叉树
BinaryExpTree :: ~BinaryExpTree( )
{
_DestroyBT(m_root);
}
//以二叉树的先序和中序次序建立二叉树的接口函数
void BinaryExpTree ::Create(char ch1[],char ch2[],int n)
{ int i = 0;
_Create( m_root,ch1,ch2,0,n-1,i);
}
// 初始条件: 二叉树p存在。操作结果: 销毁二叉树p
void BinaryExpTree::_DestroyBT(BTnode* &p)
{
if(p){
_DestroyBT(p->Lchild);
_DestroyBT(p->Rchild);
delete p;
}
}
int BinaryExpTree::Evaluate()
{
return(_Evaluate(m_root));
}
//用后序遍历的方式对表达式树求值
int BinaryExpTree ::_Evaluate(BTnode* &T)
{
if (T) {
if(!T->Lchild && !T->Rchild )
return T->data - 48;
return _Opreate(_Evaluate(T->Lchild),T->data,_Evaluate(T->Rchild));
} else
return 0;
}
//字符theta决定a与b 执行何种运算
int BinaryExpTree::_Opreate(const int &a,const char &theta,const int &b)
{
int c;
switch(theta){
case'+' : c = a + b;
break;
case'-' : c = a - b;
break;
case'*' : c= a * b;
break;
case'/' : c = a / b;
}
return c;
}
//已知二叉树的先序遍历次序及中序遍历次序,建立二叉树T
void BinaryExpTree :: _Create (BTnode* &T,char ch1[],char ch2[],int low,int high,int &k)
{
int i;
if(low > high)
T = NULL;
else{
T = new BTnode;
T->data = ch1[k];
for ( i = low;i <= high&&ch2[i] != ch1[k];i++);
if(ch2[i] == ch1[k]){
k++;
_Create (T->Lchild,ch1,ch2,low,i-1,k);
_Create (T->Rchild,ch1,ch2,i+1,high,k);
}
}
}
#endif 展开
#define H_BinaryExpTree_H
using namespace std;
struct BTnode
{
int data;
BTnode *Lchild;
BTnode *Rchild;
};
class BinaryExpTree
{
public:
BinaryExpTree(): m_root(NULL){}
~BinaryExpTree ();
void Create(char ch1[],char ch2[],int ); // 以二叉树的先序和中序次序建立二叉树的接口函数
int Evaluate();
private:
BTnode *m_root;
void _DestroyBT(BTnode* &);
int _Evaluate(BTnode* &);
int _Opreate(const int &,const char &,const int &);
void _Create(BTnode * &,char ch1[],char ch2[],int ,int ,int &);//已知二叉树的先序遍历次序及中序遍历次序,建立二叉树T
};
//析构函数, 释放二叉树
BinaryExpTree :: ~BinaryExpTree( )
{
_DestroyBT(m_root);
}
//以二叉树的先序和中序次序建立二叉树的接口函数
void BinaryExpTree ::Create(char ch1[],char ch2[],int n)
{ int i = 0;
_Create( m_root,ch1,ch2,0,n-1,i);
}
// 初始条件: 二叉树p存在。操作结果: 销毁二叉树p
void BinaryExpTree::_DestroyBT(BTnode* &p)
{
if(p){
_DestroyBT(p->Lchild);
_DestroyBT(p->Rchild);
delete p;
}
}
int BinaryExpTree::Evaluate()
{
return(_Evaluate(m_root));
}
//用后序遍历的方式对表达式树求值
int BinaryExpTree ::_Evaluate(BTnode* &T)
{
if (T) {
if(!T->Lchild && !T->Rchild )
return T->data - 48;
return _Opreate(_Evaluate(T->Lchild),T->data,_Evaluate(T->Rchild));
} else
return 0;
}
//字符theta决定a与b 执行何种运算
int BinaryExpTree::_Opreate(const int &a,const char &theta,const int &b)
{
int c;
switch(theta){
case'+' : c = a + b;
break;
case'-' : c = a - b;
break;
case'*' : c= a * b;
break;
case'/' : c = a / b;
}
return c;
}
//已知二叉树的先序遍历次序及中序遍历次序,建立二叉树T
void BinaryExpTree :: _Create (BTnode* &T,char ch1[],char ch2[],int low,int high,int &k)
{
int i;
if(low > high)
T = NULL;
else{
T = new BTnode;
T->data = ch1[k];
for ( i = low;i <= high&&ch2[i] != ch1[k];i++);
if(ch2[i] == ch1[k]){
k++;
_Create (T->Lchild,ch1,ch2,low,i-1,k);
_Create (T->Rchild,ch1,ch2,i+1,high,k);
}
}
}
#endif 展开
1个回答
展开全部
这个就是二叉排序树吗,这里面都是增删改查的声明。
追问
是的,这个就是二叉树,是一个头文件程序
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询