关闭

二叉树的C++源码

标签: c++stringbtvectordateiostream
4741人阅读 评论(1) 收藏 举报
分类:

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

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
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:70665次
    • 积分:947
    • 等级:
    • 排名:千里之外
    • 原创:18篇
    • 转载:10篇
    • 译文:0篇
    • 评论:24条
    最新评论