template <class Type> class BinaryTree;
template <class Type> class BinTreeNode
{
friend class BinaryTree<Type>;
private:
Type data;
BinTreeNode<Type> * leftChild;
BinTreeNode<Type> * rightChild;
public:
BinTreeNode(): leftChild (NULL),rightChild (NULL){}
BinTreeNode(Type item,BinTreeNode<Type> * left= NULL, BinTreeNode<Type> *right =NULL): data (item),leftChild (left),rightChild (right){}
Type GetData() const {return data;}
BinTreeNode<Type> * GetLeft () const { return leftChild;}
BinTreeNode<Type> * GetRight () const {return rightChild;}
void SetData(const Type&item){data=item;}
void SetLeft (BinTreeNode <Type>* L) {leftChild =L;}
void SetRight (BinTreeNode<Type>* R ){rightChild =R;}
};
template <class Type>class BinaryTree
{
private :
BinTreeNode <Type> * root;
Type RefValue;
void CreateBinTree( ifstream& in, BinTreeNode<Type> * & current);
BinTreeNode<Type >*Parent (BinTreeNode<Type> * subTree, BinTreeNode<Type> * current);
int Insert (BinTreeNode<Type> * & subTree const Type & item);// insert
void Traverse (BinTreeNode<Type> * subTree,ostream &out )const // 遍历
int Find(BinTreeNode <Type>* subTree,constType &item)const// 搜索
void destory (BinTreeNode <Type> * subTree);
public:
virtual BinaryTree(): root(NULL){}
virtual BinaryTree(Type value): RefValue (value),root (NULL){}
virtual ~BinaryTree (){destory (root);}
virtual int IsEmpty() { return root==NULL?1:0;}//判断树是否为空
virtual BinTreeNode <Type>* Parent (BinTreeNode<Type> * current)
{
return root == NULL || root == current?
NULL : Parent (root, current);
}//找 curent结点 的双亲
virtual BinTreeNode<Type>*LeftChild (BinTreeNode<Type> * current)
{return root!=NULL? current-> leftChild:NULL; }//取 current 结点的左子女
virtual BinTreeNode<Type>* RightChild (BinTreeNode<Type> * current)
{return root!=NULL? current-> rightChild:NULL; }//取 current 结点的you子女
virtual int Insert (const Type&item);
virtual BinTreeNode <Type> * Find (const Type & item)const;//搜索
BinTreeNode<Type> * GetRoot() const { return root;}//取根
friend istream & operator >> (istream & in, BinaryTree <Type>&Tree)//重载操作:输入
friend ostream & operator <<(ostream&our, BinaryTree<Type>&Tree)//重载操作:输出
};
//以上定义的二叉树部分成员函数的实现如下:
template <class Type> void BinaryTreee<Type>::destory
(BinTreeNode<Type> * subTree)
{
if (subTree!=NULL){
destory (subTree->leftChild);
destory (subTree->rightChild);
delete subTree;
}
}//若指针subTree 不为空,则删除根为subTree 子树
template <class Type> BinTreeNode <Type> * BinaryTree <Tree>::Parent(BinTreeNode<Type> * subTree ,BinTreeNode <Type> * current)
{
if(subTree==NULL)return NULL;
if(subTree->leftChild == current|| subTree->rightChild==current) return subTree;
BinTreeNode <Type>* p;
if((p=Parent(subTree->leftChild,current))!=NULL) return p;
else return Parent(subTree->rightChild,current);
}//私有函数:从结点subTree开始,搜索结点current的双亲结点。若找到结点current的双亲结点,则函数返回双亲结点地址,否则函数返回NUll
template <class Type> void BinaryTree<Type>::Traverse (BinTreeNode<Type> * subTree ,ostream & out)const
{
if(subTree!=NULL){
out<<subTree->data<<'';
Traverse(subTree->leftChild ,out);
Traverse(subTree->rightChild, out);
}
}//私有函数:搜索并输出根为subTree 的二叉树
template <class Type > istream&operator>>(istream&in,BinaryTree<Type>&Tree)
{
Type item;
in(filename ,ios::in|ios::nocreate);//打开文件
if (! in){
cerr<<"文件没被发现!"<<endl;
exit(1);
}
CreateBinTree(in, Tree.root);
in.close();// 关闭文件
}// 建立一颗二叉树Tree。 in是输入流对象
template<class Type>
ostream&operator<<(ostream& out,BinaryTree<Type>&Tree){
out <<"二叉树的前序遍历\n";
Tree.Traverse (Tree.root,out);
out<<endl;
return out;
}
这个是我现在学的数据结构-里头很多需要自己改动下-因为二叉树不同哈-但是前序中序 后续的 代码都对-缺一个main 函数-使用的是模板
温馨提示:答案为网友推荐,仅供参考