二叉树结构
struct BTnode { char m_val; BTnode *m_pl,*m_pr; }; class BT { BTnode *m_proot; void create(BTnode *father,int k); void pre_travel_helper(BTnode *father); void post_travel_helper(BTnode *father); void mid_travel_helper(BTnode *father); void clear_BT_helper(BTnode *father); public: BT():m_proot(NULL){} void create_BT(); void pre_travel(); void post_travel(); void mid_travel(); void level_travel(); void clear_BT(); };
二叉树函数
void BT::create_BT(){ if(m_proot!=NULL){ return; } m_proot=new BTnode; cout<<"请输入根节点值"<<endl; cin>>m_proot->m_val; create(m_proot,1);//left create(m_proot,2);//right } //k=1表示创建左子节点,k=2为创建右子节点 void BT::create(BTnode *father,int k){ char tmp; if(k==1){ cout<<father->m_val<<"左子节点值"<<endl; cin>>tmp; if(tmp!='0'){ father->m_pl=new BTnode; father->m_pl->m_val=tmp; create(father->m_pl,1); create(father->m_pl,2); }else{ father->m_pl=NULL; } }else{ cout<<father->m_val<<"右子节点值"<<endl; cin>>tmp; if(tmp!='0'){//'0'为空子节点的标志 father->m_pr=new BTnode; father->m_pr->m_val=tmp; create(father->m_pr,1); create(father->m_pr,2); }else{ father->m_pr=NULL; } } } void BT::pre_travel(){ if(m_proot==NULL){ return; } pre_travel_helper(m_proot); cout<<endl; } void BT::post_travel(){ if(m_proot==NULL){ return; } post_travel_helper(m_proot); cout<<endl; } void BT::mid_travel(){ if(m_proot==NULL){ return; } mid_travel_helper(m_proot); cout<<endl; } void BT::pre_travel_helper(BTnode *father){ if(father!=NULL){ cout<<father->m_val<<" "; }else{ return; } pre_travel_helper(father->m_pl); pre_travel_helper(father->m_pr); } void BT::post_travel_helper(BTnode *father){ if(father==NULL){ return; } post_travel_helper(father->m_pl); post_travel_helper(father->m_pr); cout<<father->m_val<<" "; } void BT::mid_travel_helper(BTnode *father){ if(father==NULL){ return; } mid_travel_helper(father->m_pl); cout<<father->m_val<<" "; mid_travel_helper(father->m_pr); } void BT::clear_BT(){ if(m_proot==NULL){ return; } clear_BT_helper(m_proot); cout<<endl; } void BT::clear_BT_helper(BTnode *father){ BTnode *lson=NULL,*rson=NULL; if(father!=NULL){ lson=father->m_pl; rson=father->m_pr; cout<<father->m_val<<"已被释放"<<endl; delete father; father=NULL; }else{ return; } clear_BT_helper(lson); clear_BT_helper(rson); } void BT::level_travel(){ stack<BTnode*> s1,s2; s1.push(m_proot); while(!s1.empty()){ while(!s1.empty()){ s2.push(s1.top()); s1.pop(); } while(!s2.empty()){ BTnode *tmp=s2.top(); cout<<tmp->m_val<<" "; if(tmp->m_pl!=NULL){ s1.push(tmp->m_pl); } if(tmp->m_pr!=NULL){ s1.push(tmp->m_pr); } s2.pop(); } } cout<<endl; }
比较两棵二叉树,相等返回1:
int compare_BT(BTnode *tree1,BTnode *tree2){ if(!tree1 && !tree2){//同时空子节点,则该子树相等 return 1; }else if(!tree1 || !tree2){ return 0; } if(tree1->m_val==tree2->m_val){ if(compare_BT(tree1->m_pl,tree2->m_pl)){ return compare_BT(tree1->m_pr,tree2->m_pr); }else if(compare_BT(tree1->m_pr,tree2->m_pl)){//左右子树对应相等也可 return compare_BT(tree1->m_pl,tree2->m_pr); } } return 0; }
连续存储方式:
class BT_row { char *m_data; int m_maxlen; void pre_travel_helper(int n); public: BT_row():m_data(NULL),m_maxlen(0){} void init_tree(int maxlen,char *data); void clear_tree(); void pre_travel(); void post_travel(); void mid_travel(); void level_travel(); };
连续存储二叉树函数:
void BT_row::init_tree(int maxlen,char *data){ if(m_data!=NULL){ return; } m_data=new char[maxlen]; this->m_maxlen=maxlen; memcpy(m_data,data,maxlen*sizeof(char)); } void BT_row::clear_tree(){ if(m_data!=NULL){ delete []m_data; m_data=NULL; m_maxlen=0; } } void BT_row::pre_travel(){ pre_travel_helper(0); cout<<endl; } void BT_row::pre_travel_helper(int n){ if(n>=m_maxlen){ return; } if(m_data[n]!='0'){ cout<<m_data[n]<<" "; } pre_travel_helper(2*n+1);//访问左子节点 pre_travel_helper(2*n+2);//右子节点 } void BT_row::level_travel(){ int i; for(i=0;i<m_maxlen;++i){//连续存储的顺序就是按层遍历的顺序 if(m_data[i]!='0'){ cout<<m_data[i]<<" "; } } cout<<endl; }