64,649
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class BTnode;
template <typename Type>
class BinaryTree
{
public:
BinaryTree();
BinaryTree(const BinaryTree &a);
~BinaryTree();
BinaryTree& operator=(const BinaryTree &a);
bool empty() const{return _root==0;}
bool clear();
void insert(const BTnode<Type> &a);
void remove(const Type &a);//删除 叶子 a
void remove_root();//删除 根节点
void clear()//删除全部
{
if (_root)
{
clear(_root);//private版的clear()
_root = 0;//防止这家伙乱来
}
return;
}
protected:
private:
BTnode<Type> *_root;
void clear(BTnode<Type> *bt); //删除整棵树
//把src所指的值复制到tar上去
void copy(BinaryTree<Type> *tar,BinaryTree<Type> *src);
};
template <typename Type>
inline BinaryTree<Type>::BinaryTree():_root(0){}
template <typename Type>
inline BinaryTree<Type>::BinaryTree(const BinaryTree<Type> &a)
{
copy(_root,a._root);
}
template <typename Type>
inline BinaryTree<Type>::~BinaryTree()
{
clear();
}
template <typename Type>
inline BinaryTree& BinaryTree<Type>::operator =(const BinaryTree &a)
{
if (this != &a)
{
clear();
copy(_root,a._root);
}
return *this;
}
template <typename Type>
inline void BinaryTree::insert(const BTnode<Type> &a)
{
if ( ! _root)
{
_root = new BTnode<Type>(a);
}
else
{
_root->insert_val(a);
}
return;
}
template <typename Type>
inline void BinaryTree::remove(const Type &a)
{
if (_root)
{
if (_root->_val == a)
{
remove_root();//根节点 特殊 处理
}
else
{
_root->remove_val(a,_root);
}
}
return;
}
template <typename Type>
inline void BinaryTree::remove_root()
{
if ( ! _root)
{
return;
}
BTnode<Type> *temp = _root;
if (_root->_rchild)
{
_root = _root->_rchild;
//如果 根节点(右子树来的) 没有左子树就直接复制过去,
//如果有 就app在左子树的底部
if (temp->_lchild)
{
BTnode<Type> *lc = temp->_lchild;
BTnode<Type> *newlc = _root->_lchild;
if (newlc)//左子树存在
{
BTnode<Type>::lchild_left(lc, newlc);
}
else
{
newlc = lc;//不存在 直接复制
}
}
}
else
{
_root = _root->_lchild;
}
delete temp;//最后 释放
}
template <typename Type>//这个也要单步一次..
void BinaryTree::clear(BTnode<Type> *bt)
{
if (bt)
{
clear(bt->_lchild);
clear(bt->_rchild);
delete bt;
}
return;
}
template <typename valtype>
class BTnode //节点
{
friend class BinaryTree<valtype>;
public:
BTnode(const BTnode &a);
void insert_val(const valtype &a);//插入
static void lchild_left(BTnode *left,BTnode *subtree);
void remove_val(const valtype &val, BTnode *&ptr);//从prt节点开始找val这个数值 在删除
protected:
private:
valtype _val;
int _cnt;
BTnode *_lchild;
BTnode *_rchild;
};
template <typename valtype>
inline BTnode::BTnode(const valtype &a):_val(a)
{
_cnt = 1;
_lchild = _rchild = 0;
};
template <typename valtype>
inline void BTnode::insert_val(const valtype &a)
{
if (a == _val)
{
_cnt++;
return;
}
if (a < _val)
{
if (! _lchild)
{
_lchild = new BTnode<Type>(a);
}
else
{
_lchild->insert_val(a);
}
}
else
{
if (! _rchild)
{
_rchild = new BTnode<Type>(a);
}
else
{
_rchild->insert_val(a);
}
}
return;
}
template <typename valtype>
inline void lchild_left(BTnode<valtype> *left, BTnode<valtype> *subtree)
{
while(subtree->_lchild)
{
subtree = subtree->_lchild;
}
subtree->_lchild = left;
return;
}
template <typename valtype>//这个很难多理解
inline void BTnode::remove_val(const valtype &val, BTnode *&ptr)
{
if (val < _val)
{
if ( ! _lchild)
{
return;//左子树没有这值
}
else
{
_lchild->remove_val(val,_lchild);
}
}
else
{
if (val > _val)
{
if ( ! _rchild)
{
return;//右子树没有这值
}
else
{
_rchild->remove_val(val,_lchild);
}
}
else
{
//不负重望,终于找到了..
if (_rchild)
{
ptr = _rchild;//节点变成 节点的右子树
if ( ! ptr->_lchild)
{
ptr->_lchild = _lchild;//这句很深要多理解
}
else
{
BTnode<valtype>::lchild_left(_lchild,ptr->_lchild);//把左子树add在右子树的左节点的底部
}
}
else
{
ptr = _lchild;//节点变成 节点的左子树
}
delete this;//最后释放
}
}
return;
}
#include <iostream>
#include <string>
#include <vector>
using namespace std;
template<typename valtype>
class BTnode;
template <typename Type>
class BinaryTree
{
public:
BinaryTree();
BinaryTree(const BinaryTree &a);
~BinaryTree();
BinaryTree& operator=(const BinaryTree &a);
bool empty() const{return _root==0;}
bool clear();
void insert(const BTnode<Type> &a);
void remove(const Type &a);//删除 叶子 a
void remove_root();//删除 根节点
void clear()//删除全部
{
if (_root)
{
clear(_root);//private版的clear()
_root = 0;//防止这家伙乱来
}
return;
}
protected:
private:
BTnode<Type> *_root;
void clear(BTnode<Type> *bt); //删除整棵树
//把src所指的值复制到tar上去
void copy(BinaryTree<Type> *tar,BinaryTree<Type> *src);
};
template <typename Type>
inline BinaryTree<Type>::BinaryTree():_root(0){}
template <typename Type>
inline BinaryTree<Type>::BinaryTree(const BinaryTree<Type> &a)
{
copy(_root,a._root);
}
template <typename Type>
inline BinaryTree<Type>::~BinaryTree()
{
clear();
}
template <typename Type>
inline BinaryTree<Type>& BinaryTree<Type>::operator =(const BinaryTree &a)
{
if (this != &a)
{
clear();
copy(_root,a._root);
}
return *this;
}
template <typename Type>
inline void BinaryTree<Type>::insert(const BTnode<Type> &a)
{
if ( ! _root)
{
_root = new BTnode<Type>(a);
}
else
{
_root->insert_val(a);
}
return;
}
template <typename Type>
inline void BinaryTree<Type>::remove(const Type &a)
{
if (_root)
{
if (_root->_val == a)
{
remove_root();//根节点 特殊 处理
}
else
{
_root->remove_val(a,_root);
}
}
return;
}
template <typename Type>
inline void BinaryTree<Type>::remove_root()
{
if ( ! _root)
{
return;
}
BTnode<Type> *temp = _root;
if (_root->_rchild)
{
_root = _root->_rchild;
//如果 根节点(右子树来的) 没有左子树就直接复制过去,
//如果有 就app在左子树的底部
if (temp->_lchild)
{
BTnode<Type> *lc = temp->_lchild;
BTnode<Type> *newlc = _root->_lchild;
if (newlc)//左子树存在
{
BTnode<Type>::lchild_left(lc, newlc);
}
else
{
newlc = lc;//不存在 直接复制
}
}
}
else
{
_root = _root->_lchild;
}
delete temp;//最后 释放
}
template <typename Type>//这个也要单步一次..
void BinaryTree<Type>::clear(BTnode<Type> *bt)
{
if (bt)
{
clear(bt->_lchild);
clear(bt->_rchild);
delete bt;
}
return;
}
template <typename valtype>
class BTnode //节点
{
friend class BinaryTree<valtype>;
public:
BTnode(const valtype &a);
void insert_val(const valtype &a);//插入
static void lchild_left(BTnode *left,BTnode *subtree);
void remove_val(const valtype &val, BTnode *&ptr);//从prt节点开始找val这个数值 在删除
protected:
private:
valtype _val;
int _cnt;
BTnode *_lchild;
BTnode *_rchild;
};
template <typename valtype>
inline BTnode<valtype>::BTnode(const valtype &a):_val(a)
{
_cnt = 1;
_lchild = _rchild = 0;
};
template <class valtype>
inline void BTnode<valtype>::insert_val(const valtype &a)
{
if (a == _val)
{
_cnt++;
return;
}
if (a < _val)
{
if (! _lchild)
{
_lchild = new BTnode<Type>(a);
}
else
{
_lchild->insert_val(a);
}
}
else
{
if (! _rchild)
{
_rchild = new BTnode<Type>(a);
}
else
{
_rchild->insert_val(a);
}
}
return;
}
template <typename valtype>
inline void BTnode<valtype>::lchild_left(BTnode<valtype> *left, BTnode<valtype> *subtree)
{
while(subtree->_lchild)
{
subtree = subtree->_lchild;
}
subtree->_lchild = left;
return;
}
template <typename valtype>//这个很难多理解
inline void BTnode<valtype>::remove_val(const valtype &val, BTnode *&ptr)
{
if (val < _val)
{
if ( ! _lchild)
{
return;//左子树没有这值
}
else
{
_lchild->remove_val(val,_lchild);
}
}
else
{
if (val > _val)
{
if ( ! _rchild)
{
return;//右子树没有这值
}
else
{
_rchild->remove_val(val,_lchild);
}
}
else
{
//不负重望,终于找到了..
if (_rchild)
{
ptr = _rchild;//节点变成 节点的右子树
if ( ! ptr->_lchild)
{
ptr->_lchild = _lchild;//这句很深要多理解
}
else
{
BTnode<valtype>::lchild_left(_lchild,ptr->_lchild);//把左子树add在右子树的左节点的底部
}
}
else
{
ptr = _lchild;//节点变成 节点的左子树
}
delete this;//最后释放
}
}
return;
}