Essential c++ 第六章二叉树的完整实现

初步深入学习C++,还很菜,自己整理的,有些不完整,但也不例外愿拿来供大家来参考,不完整的地方还望大家来补充!

 

 

#include < iostream >

#include < string >

 

using namespace std;


// class BTnode declare


template < typename elemType >
class BinaryTree;

template < typename valType >
class BTnode{
 public:
  friend class BinaryTree< valType >;
  BTnode( const valType &val );
  void insert_value( const valType &val );  
  void remove_value( const valType &val, BTnode *& prev );
  void preorder ( BTnode *pt, ostream &os ) const; 
  void display_val( BTnode *pt, ostream &os ) const;
  static void lchild_leaf( BTnode *leaf,BTnode *subtree );


 private:
  valType _val;
  int _cnt;
  BTnode *_lchild;
  BTnode *_rchild;
};
 


// class BinaryTree declare

template < typename elemType >
class BinaryTree {
 public:
  BinaryTree();
  BinaryTree( const BinaryTree& );
  ~BinaryTree();
  BinaryTree& operator=( const BinaryTree&);
  void insert( const elemType &elem );
  bool empty()
  {
   return _root==0;
  } 
        void remove( const elemType &elem );
  
        void preorder() const
  {
       _root->preorder( _root, cout );
  }
     void remove_root();

     void clear()
  {
      if( _root )
   {
         clear( _root );
      _root = 0;
   }
  } //ostream &os = _current_os

       friend ostream & operator<<( ostream &, const BinaryTree< elemType >);
   
 private:
  BTnode< elemType > *_root;
  void copy( BTnode< elemType > * &, BTnode< elemType >* );
  void clear( BTnode<elemType> * ); // static ostream   *_current_os;
};

//BTnode define

template < typename valType >
inline BTnode< valType >::
BTnode( const valType &val ) : _val( val )
{
 _cnt = 1;
 _lchild = _rchild = 0;
}

template < typename valType >
void BTnode< valType >::
preorder ( BTnode *pt, ostream &os ) const
{
 if ( pt )
 {
  display_val( pt ,os );
  if ( pt->_lchild ) preorder( pt->_lchild, os );
  if( pt->_rchild ) preorder( pt->_rchild, os );
 }
}
template < typename valType >
void BTnode< valType >::
insert_value( const valType &val )
{
 if( val == _val)
 {
  _cnt++;
  return;
 }
  if( val < _val )
 {
  if( ! _lchild )
    _lchild = new BTnode( val );
  else
    _lchild->insert_value( val );
 }
 else
 {
  if( ! _rchild )
   _rchild = new BTnode( val );
  else
   _rchild->insert_value( val );
 } 
}

template < typename valType >
void BTnode< valType >::
remove_value( const valType &val, BTnode *& prev )
{
 if( val < _val)
 {
  if( ! _lchild )
   return;

  else
   _lchild->remove_value( val ,_lchild );
 }
 else
  if( val > _val )
  {
   if( ! _rchild )
    return;
   else
    _rchild->remove_value( val,_rchild );
  }
  else
  {
   if( _rchild )
   {
    prev = _rchild;
    if( _lchild )
     if( ! prev->_lchild )
      prev->_lchild = _lchild;
     else
      BTnode< valType >::lchild_leaf( _lchild,prev->_lchild );
   }
   else prev = _lchild;
   delete this;
  }
  
}
template <typename valType>
inline void BTnode<valType>::
display_val( BTnode *pt, ostream &os ) const
{
       os << pt->_val;
       if ( pt->_cnt > 1 )
            os << "( " << pt->_cnt << " ) ";
       else os << ' ';
}
template < typename valType >
void BTnode< valType >::
lchild_leaf( BTnode *leaf, BTnode *subtree )
{
 while ( subtree->_lchild )
  subtree = subtree->_lchild;
 subtree->_lchild = leaf;
}

// BinaryTree define
template < typename elemType >
inline BinaryTree< elemType >::BinaryTree() : _root( 0 )
{}

template < typename elemType >
inline BinaryTree< elemType >::BinaryTree( const BinaryTree &rhs )
{
 copy( _root,rhs._root);
}

template < typename elemType >
inline BinaryTree< elemType >:: ~BinaryTree()
{
 clear();
}

template < typename elemType >
inline BinaryTree < elemType >&
BinaryTree< elemType >::
operator=( const BinaryTree &rhs )
{
 if ( this != &rhs )
 
 
 {
  clear();
  copy(_root,rhs._root );
 }

 return *this;
}

template < typename elemType >
inline void BinaryTree< elemType >::
insert( const elemType &elem )
{
 if( !_root )
  _root = new BTnode< elemType >( elem );
 else
  _root->insert_value( elem );
}
 
template < typename elemType >
void BinaryTree< elemType >::
remove_root()
{
 if( ! _root )
  return ;
 BTnode< elemType > *tmp = _root;
 if( _root->_rchild )
 {

     _root = _root->_rchild;

    if( tmp->_rchild )
    {
  
   BTnode< elemType > *lc = tmp->_lchild;
   BTnode< elemType > *newlc = _root->_lchild;
   if( !newlc )
      _root->_lchild = lc;
   else
   BTnode <elemType>::lchild_leaf( lc,newlc );
    }
 }
 else
  _root = _root->_lchild;
 delete tmp;
}

template < typename elemType >
void BinaryTree< elemType >::
remove( const elemType &elem )
{
   if( _root )
 {
    if( _root->_val == elem )
    remove_root();
  else
    _root->remove_value( elem, _root );
 }
}

   
template < typename elemType >
void BinaryTree< elemType >::
clear( BTnode< elemType > *pt )
{
 if( pt )
 {
  clear( pt->_lchild );
  clear( pt->_rchild );
  delete pt;
 }
}
template < typename elemType >  //BinaryTree< elemType >::
 ostream & operator<<( ostream &os, const BinaryTree< elemType > &bt )
{
  os << "Tree : "<< endl;
  bt.print( os );
 
  return os;
 }

template <typename elemType>
void BinaryTree<elemType>::
copy( BTnode<elemType> * &tar, BTnode<elemType> *src )
{
    if ( src )
 {//( )
   tar = new BTnode<elemType>( src->_val );
   if ( src->_lchild ) copy( tar->_lchild, src->_lchild );
   if ( src->_rchild ) copy( tar->_rchild, src->_rchild );
 }
}

int main()
{
 BinaryTree< string > bt;


 bt.insert( " Piglet " );
 bt.insert( "Eeyore" );
 bt.insert( "Roo" );
 bt.insert( "Tigger" );
 bt.insert( "Chris" );
 bt.insert( "Pooh" );
 bt.insert( "Kanga" );

 cout<< "Preorder traversal: /n";

 bt.preorder();

 bt.remove( "Piglet" );
 cout<< " /n/n Preorder traversal after Piglet removal: /n";
 bt.preorder();

 bt.remove( "Eeyore" );
 cout<< "/n/n Preorder traversal after Eeyore removal : /n";
 bt.preorder();

 return 0;
}

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值