二叉树的C++源码

 参考着课件,写了一个简单的二叉树,用法很简单,方法也很少,要是谁有兴趣的话,加上一个线索二叉树的方法,就更好了。

KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))");//用广义表,表示法输入
 btree.Preorder();//前序遍历
 btree.Inorder();//中序遍历
 btree.Postorder();//后序遍历
 cout<<btree.Depth();//深度

KaKaBTree.h

 

#pragma  once
#include 
< string >
#include 
< vector >
#include 
< iostream >
using   std:: string ;


typedef 
struct  Note{
   
char  Date;
   Note 
* Left;
   Note 
* Right;
   Note(
char  char_v){
       Date
= char_v;
       Left
= 0 ;
       Right
= 0 ;
   }

}
* PNote;
class  KaKaBTree
{
public :
    KaKaBTree(
void );
    KaKaBTree(
string  s); // 用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")    
private :
    KaKaBTree(
const  KaKaBTree &  bt);
    KaKaBTree 
operator = ( const  KaKaBTree &  bt);
public :
    
void  Init( string  s); // 用广义表,表示法输入,KaKaBTree btree("-(+(a,*(b,-(c,d))),/(e,f))")    
     void  Preorder( void ); // 前序遍历
     void  Inorder( void ); // 中序遍历
     void  Postorder( void ); // 后序遍历
     int  Depth( void ); // 深度
public :
    
~ KaKaBTree( void );    
private :
    PNote BT;
    
void  Clear( void );
private :
    
void  Create( string  s);
    
void  Iner_Clear(PNote PN);
    
void  Iner_Preorder(PNote pn);
    
void  Iner_Inorder(PNote pn);
    
void  Iner_Postorder(PNote pn);    
    
int  Iner_Depth(PNote pn);
};

 

KaKaBTree.cpp

 

#include  " KaKaBTree.h "
#define  MAX(a,b) ((a)>(b)?(a):(b))
using  std::vector;
using  std:: string ;
using  std::cout;
using  std::endl;

KaKaBTree::KaKaBTree(
void ):BT( 0 )
{
}

KaKaBTree::KaKaBTree(
string  s):BT( 0 )
{
    
this -> Create(s);    
}

KaKaBTree::
~ KaKaBTree( void )
{
    
this -> Clear();
}

void  KaKaBTree::Init( string  s)
{
    
this -> Clear();
    
this -> Create(s);
}



void  KaKaBTree::Clear( void )
{
    
this -> Iner_Clear(BT);
    BT
= 0 ;
    
}

void  KaKaBTree::Iner_Clear(PNote PN)
{
    
if (PN){        
        Iner_Clear(PN
-> Left);
        Iner_Clear(PN
-> Right);
        delete PN;
    }
}



void  KaKaBTree::Create( string  s)
{
    
// 用广义表,表示法输入
     if (s.empty())  return ;
    vector
< Note *>  StackNote;
    
enum  LR{L,R};
    LR lr
= L;
    
    BT
= new  Note(s[ 0 ]);
    PNote t_p
= BT;
    
for ( string ::size_type i = 1 ;i != s.size(); ++ i){
         
char  ch = s[i];
         
switch (ch){
             
case   ' ( ' :
                 StackNote.push_back(t_p);
                 lr
= L;
                 
break ;
             
case   ' , ' :
                 lr
= R;
                 
break ;
             
case   ' ) ' :
                 StackNote.pop_back();                
                 
break ;
             
default :
                 
if (lr == L){
                     t_p
=  StackNote[StackNote.size() - 1 ] -> Left = new  Note(ch);
                     
                 }
else {
                      t_p
=  StackNote[StackNote.size() - 1 ] -> Right = new  Note(ch);
                      
                 }
         }

       
    }
    

}

void  KaKaBTree::Preorder( void )
{
    cout
<< endl << " ===================Preorder=================== " << endl;
    
this -> Iner_Preorder(BT);
    cout
<< endl << " =============================================== " << endl;

}
void  KaKaBTree::Iner_Preorder(PNote pn)
{
    
// 前序遍历
     if (pn){
        cout
<< pn -> Date << "   " ;
        
this -> Iner_Preorder(pn -> Left);        
        
this -> Iner_Preorder(pn -> Right);
    }
    
}

void  KaKaBTree::Inorder( void )
{
    cout
<< endl << " ==================Inorder====================== " << endl;
    
this -> Iner_Inorder(BT);
    cout
<< endl << " =============================================== " << endl;
}
void  KaKaBTree::Iner_Inorder(PNote pn)
{
    
// 中序遍历
     if (pn){
        
this -> Iner_Inorder(pn -> Left);
        cout
<< pn -> Date << "   " ;
        
this -> Iner_Inorder(pn -> Right);

    }
}



void  KaKaBTree::Postorder( void )
{
    cout
<< endl << " =============Postorder========================= " << endl;
    
this -> Iner_Postorder(BT);
    cout
<< endl << " =============================================== " << endl;
}
void  KaKaBTree::Iner_Postorder(PNote pn)
{
    
// 后序遍历
     if (pn){
        
this -> Iner_Postorder(pn -> Left);        
        
this -> Iner_Postorder(pn -> Right);
        cout
<< pn -> Date << "   " ;

    }
}

int  KaKaBTree::Depth( void )
{
    
return  Iner_Depth(BT);
}

int  KaKaBTree::Iner_Depth(PNote pn)
{
    
// 深度
     if ( ! pn){
        
return   0 ;
    }
else {
        
int  dl,dr;
        dl
= Iner_Depth(pn -> Left);
        dr
= Iner_Depth(pn -> Right);
        
return  MAX(dl,dr) + 1 ;

    }

}

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值