c语言可不可以不要.cpp文件,[求助]一个头文件和CPP文件的问题

我把我的BSTTree.cpp和BSTTree.h的源程序贴出来,还有一个BSTNode.h,请大家帮我看看。

以下是BSTTree.h

#if!defined _BSTTree_h

#define _BSTTree_h

#include "BSTNode.h"

#include

using namespace std;

template

class BSTTree {

public:

BSTTree(){LastFind=NULL;Root=NULL;}

~BSTTree(){ FreeTree(Root)}

int Insert(const Type &x) { return Insert(x,Root); }//插入x到以root为根的二叉排序树

const Type &Find(const Type &x) {

return (LasrFind=FindFromRoot(x,Root)) ? LastFind->data:ItemNotFind;

}//若查找x成功,则返回二叉排序树的相应的数据项。否则,返回ItemNotFound.

const Type &FindMin() const{

const BSTNode *p=FindMin(Root);

return p?p->data:ItemNotFound;

}//返回二叉排序树中的最小的数据项。若树为空,返回ItemNotFound

const Type &FindMax() const{

const BSTNode *p=FindMax(Root);

return p?p->data:ItemNotFound;

}//返回二叉排序树中的最大的数据项。若树为空,返回ItemNotFound

int IsFound(const Type &x){return Find(x,root)!=NULL;}//若x找到,则返回True

int WasFound() const {return LastFind!=NULL;}//最近一次查找成功,则返回True

int Remove(const Type &x) {return Remove(x,Root);}

//从二叉排序树中删除值为x的结点,删除成功返回True

int RemoveMin(){return RemoveMin(Root);}

//从二叉排序树中删除最小的结点,删除成功

int IsEmpty() const { return Root=NULL;}

//二叉排序树为空,返回True,否则为False

void MakeEmpty() {FreeTree(Root);Root=NULL;}

//清除二叉树中的所有结点

protected:

BSTNode *Root;

BSTNode *LastFind;

Type ItemNotFound;//用于查找失败时返回

const BSTNode *FindFromRoot(const Type &x,const BSTNode *T) const;

const BSTNode *FindMin(const BSTNode *T) const;

const BSTNode *FindMax(const BSTNode *T) const;

int Insert(const Type &x,BSTNode *&T);

int RemoveMin(const BSTNode *&T);

int Remove(const Type &x,const BSTNode *&T);

};

#endif

以下是BSTTree.cpp

#include "BSTTree.h"

#include

using namespace std;

template

const BSTNode * BSTTree::FindFromRoot(const Type & x, const BSTNode * T) const {

while ( T != NULL )

if ( X < T->data ) T = T->left else if ( X > T->data ) T = T->right; else return T;

return NULL;// 查找失败,返回空。查找成功,返回指向相应结点的指针。

}

template

int BSTTree :: Insert (const Type & x, BSTNode *&T ) {

if ( T == NULL ) { T = new BSTNode (x); return 1; } // 新结点插入。

else if ( x < T->data ) return Insert( x, T->left ); // 转向左子树。

else if ( x > T->data ) return Insert( x, T->right ); // 转向右子树。

return 0;// 若结点已经存在,返回不必重复插入标志。

} // 插入结点到以 T 为根的二叉排序树中去。插入成功返回 1,插入失败返回 0。

template

int BSTTree:: Remove( const Type & x, const BSTNode * &T ) {

BSTNode * p;

if ( T == NULL ) return 0; // 删除失败,返回 0。

else if ( x < T->data ) return Remove( x, T->left ); // 转向左子树。

else if ( x > T->data ) return Remove( x, T->right ); // 转向右子树。

else if ( T->left != NULL && T->right != NULL ) { // 被删结点的左右儿子非空。

p=( BSTNode * )FindMin(T->right);

T->data=p->data; //复制右子树最小结点数据值。

return RemoveMin( T->right ); //删除被删结点的右子树的最小结点。

}

p = T; // 处理被删结点的至少一个儿子结点为空的情况。

T = ( T->left == NULL ) ? T->right : T->left;

delete p;

return 1;

} // 删除成功,返回1。删除失败,返回 0。

template

const BSTNode *BSTTree::FindMin(const BSTNode *T) const{

if(T!=NULL)

while(T->left!=NULL) T=T->left;

return T;

}

template

const BSTNode *BSTTree::FindMax(const BSTNode *T) const{

if(T!=NULL)

while(T->right!=NULL) T=T->right;

return T;

}

template

int BSTTree::RemoveMin(const BSTNode *&T){

if(T==NULL) return 0;

else if(T->left!=NULL) return RemoveMin(T->left);

BSTNode *p;

p=T;

T=T->right;

delete p;

return 1;

}

还有一个BSTNode.h

#if!defined _BSTNode_h

#define _BSTNode_h

#include

using namespace std;

template

class BSTNode { // 二叉排序树的结点的表示。

public:

Type data; // 结点的数据场。

BSTNode * left; // 给出结点的左儿子的地址。

BSTNode * right; // 给出结点的右儿子的地址。

int BalanceFactor; // 结点的平衡度,用于 AVL 树。

int Size; // 以本结点为根的子树的所有结点的个数,用于顺序统计。

BSTNode ( ): left(NULL), right(NULL), Size(1), BalanceFactor(1) { }

BSTNode ( const Type x ) : data(x), left(NULL), right(NULL), Size(1),

BalanceFactor(1) { }

BSTNode ( const Type x, BSTNode * L, BSTNode * R ): data(x), left(L), right(R), Size(1), BalanceFactor(1) { }

~BSTNode( ) { }

};

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值