比前一版本增加了构建方法
BinaryTree.h
- #pragma once
- #include<iostream>
- #include<queue>
- #include<stack>
- #include"BinaryTreeNote.h"
- #include<string>
- using namespace std;
- template<class T>
- class BinaryTree{
- BinaryTreeNote<T> *rt;
- //2011.11.12 删除num变量。
- public:
- BinaryTree();
- ~BinaryTree();
- bool isEmpty( ) const;
- BinaryTreeNote<T>* getRoot()const; //return root note
- BinaryTreeNote<T>* getParent(BinaryTreeNote<T>* current )const; //return parent note
- BinaryTreeNote<T>* getLeftSibing(BinaryTreeNote<T>* current ) const; //return left brother
- BinaryTreeNote<T>* getRightSibing(BinaryTreeNote<T>* current ) const; //return right brother
- void breadFristOrder(BinaryTreeNote<T>* ); //广度优先遍历
- void preOrder(BinaryTreeNote<T>* ); // 前序
- void inOrder(BinaryTreeNote<T>* ); //中序
- void postOrder(BinaryTreeNote<T>* ); //后序
- void levelOrder(BinaryTreeNote<T>* ); //
- void deleteBinaryTree(BinaryTreeNote<T>* root); //delete the tree which is the charge of root
- void build(BinaryTreeNote<T>** );
- BinaryTreeNote<T>** getDRoot(){
- return &rt;
- }
- void help_build_qz(T*,int,int,T*,int,int,BinaryTreeNote<T> *);
- void build_qz();
- void help_build_zh(T*,int,int,T*,int,int,BinaryTreeNote<T> *);
- void build_zh();
- //5.1 2 3
- int sta_deg(int,BinaryTreeNote<T>* root );
- //5.4
- int high(BinaryTreeNote<T>*);
- //5.5
- int wide( );
- //5.6
- int biggest(BinaryTreeNote<T>*,T);
- //5.7
- void swap(BinaryTreeNote<T>*);
- //5.8
- void del_leaf(BinaryTreeNote<T>*,BinaryTreeNote<T>*);
- //6
- bool isCom();
- //7
- void build_clu(); //线索二叉树构建
- BinaryTreeNote<T>* clu_qx(BinaryTreeNote<T>* root);
- BinaryTreeNote<T>* clu_hj(BinaryTreeNote<T>* root);
- };
- template<class T>
- BinaryTree<T>::BinaryTree( ):rt(new BinaryTreeNote<T>){
- }
- template<class T>
- bool BinaryTree<T>::isEmpty( )const{
- return !rt;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getRoot() const{
- return rt;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getParent(BinaryTreeNote<T>* current ) const {
- queue<BinaryTreeNote<T>*> n;
- BinaryTreeNote<T>* cur;
- n.push(this->getRoot());
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- if(cur->getLeftChild()==current ||cur->getRightChile()==current )
- return cur;
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild);
- else if(cur->getRightChile())
- n.push(cur->getRightChile());
- }
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getLeftSibing(BinaryTreeNote<T>* current ) const{
- if( this->getParent()==NULL)
- return NULL;
- return this->getParent(current)->getLeftChild();
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::getRightSibing(BinaryTreeNote<T>* current ) const{
- if( this->getParent()==NULL)
- return NULL;
- return this->getParent(current)->getRightChile();
- }
- template<class T>
- void BinaryTree<T>::breadFristOrder( BinaryTreeNote<T>* root ){
- queue<BinaryTreeNote<T>*> local;
- local.push(root );
- BinaryTreeNote<T>* index=local.front();
- while( !local.empty() ){
- if( index ){
- local.push(index->getLeftChild());
- local.push(index->getRightChile());
- cout<<index->getValue()<<" ";
- }
- local.pop();
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::preOrder(BinaryTreeNote<T>* root) {
- BinaryTreeNote<T>* cur=root;
- stack<BinaryTreeNote<T>*> n;
- while( cur || !n.empty() ){
- if( cur ){
- cout<<cur->data<<" ";
- if(cur->getRightChile() )
- n.push(cur->getRightChile());
- cur=cur->getLeftChild();
- }
- else{
- cur=n.top();
- n.pop();
- }
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::inOrder(BinaryTreeNote<T>* root){
- //关键部分就是把一个过程抽象成一步一步执行相同的过程!
- BinaryTreeNote<T>* cur=root;
- stack<BinaryTreeNote<T>* > n;
- while( !n.empty() || cur!=0 ){
- if(cur ){
- n.push(cur);
- cur=cur->getLeftChild();
- }
- else{
- cur=n.top();
- cout<<cur->getValue()<<" ";
- cur=cur->getRightChile();
- n.pop();
- }
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::postOrder(BinaryTreeNote<T>* root){
- stack<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur=root;
- BinaryTreeNote<T>* pre=root;
- while( cur ){
- while( cur->getLeftChild() ){
- n.push(cur);
- cur=cur->getLeftChild();
- }
- while( cur &&( cur->getRightChile()==0 || cur->getRightChile()==pre ) ){
- cout<<cur->getValue()<<" ";
- pre=cur;
- if(n.empty())
- return;
- cur=n.top();
- n.pop();
- }
- n.push(cur);
- cur=cur->getRightChile();
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::levelOrder(BinaryTreeNote<T>* root){
- if(!root)
- return ;
- queue<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur;
- n.push(root);
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- cout<<cur->getValue()<<" ";
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild());
- if(cur->getRightChile()!=0)
- n.push(cur->getRightChile());
- }
- cout<<endl;
- }
- template<class T>
- void BinaryTree<T>::deleteBinaryTree(BinaryTreeNote<T>* root){
- if(!root)
- return ;
- queue<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur;
- n.push(rt);
- if(root==rt)
- rt=0;
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- if(cur->getLeftChild()==root ){
- cur->left=0;
- break;
- }
- else if(cur->getRightChile()==root ){
- cur->right=0;
- break;
- }
- if( cur->getLeftChild()!=0)
- n.push(cur->getLeftChild());
- if(cur->getRightChile()!=0)
- n.push(cur->getRightChile());
- }
- }
- template<class T>
- BinaryTree<T>::~BinaryTree(){
- this->deleteBinaryTree (this->rt);
- }
- //下面是一段2重的指针代码.......
- //创建方法1
- template<class T>
- void BinaryTree<T>::build(BinaryTreeNote<T>** root){
- T h;
- static int ll=0;
- if(ll++==0)
- cout<<"root:";
- if(cin>>h){
- *root=new BinaryTreeNote<T>;
- (*root)->data=h;
- cout<<h<<" left:";
- build(&((*root)->left));
- cout<<h<<" right:";
- build(&((*root)->right));
- }
- else{
- cin.clear();
- return ;
- }
- }
- template<class T>
- //创建发放二
- void BinaryTree<T>::build_qz(){
- T n;
- cin>>n;
- int *a=new T [n]; //zhong
- int *b=new T [n];
- cout<<"前序输入:";
- for( int i=0; i<n;i++ )
- cin>>a[i];
- cout<<"中序输入:";
- for( int i=0; i<n;i++ )
- cin>>b[i];
- this->help_build_qz(a,0,n-1,b,0,n-1,this->rt);
- }
- template<class T>
- void BinaryTree<T>::help_build_qz( T* a,int num_af,int num_al,T* b,int num_bf,int num_bl,BinaryTreeNote<T>* root ){
- //a前,b中 root 已经分备好的根节点
- root->data=a[num_af];
- if(num_af==num_al)
- return ;
- int i=num_bf;
- while( b[i]!=a[num_af] ) i++;
- if(a[num_af]!=b[num_bf] ){
- root->left=new BinaryTreeNote<T>;
- help_build_qz(a,num_af+1,num_af+i-num_bf,b,num_bf,i-1,root->getLeftChild() );
- }
- if(a[num_af]!=b[num_bl] ){
- root->right=new BinaryTreeNote<T>;
- help_build_qz(a,num_af+i-num_bf+1,num_al,b,i+1,num_bl,root->getRightChile() );
- }
- }
- template<class T>
- //创建方法3
- void BinaryTree<T>::build_zh(){
- T n;
- cin>>n;
- int *a=new T [n]; //zhong
- int *b=new T [n];
- cout<<"后序输入:";
- for( int i=0; i<n;i++ )
- cin>>a[i];
- cout<<"中序输入:";
- for( int i=0; i<n;i++ )
- cin>>b[i];
- this->rt=new BinaryTreeNote<T>;
- this->help_build_zh(a,0,n-1,b,0,n-1,this->rt);
- int y;
- }
- template<class T>
- void BinaryTree<T>::help_build_zh(T* a,int num_af,int num_al,T* b,int num_bf,int num_bl,BinaryTreeNote<T>* root ){
- //a后,b中 root 已经分备好的根节点
- root->data=a[num_al];
- if(num_af==num_al)
- return ;
- int i=num_bf;
- while( b[i]!=a[num_al] ) i++;
- if(a[num_af]!=b[num_bl] ){
- root->left=new BinaryTreeNote<T>;
- help_build_qz(a,num_af,num_af+i-num_bf-1,b,num_bf,i-1,root->getLeftChild() );
- }
- if(a[num_af]!=b[num_bf] ){
- root->right=new BinaryTreeNote<T>;
- help_build_qz(a,num_af+i-num_bf,num_al-1,b,i+1,num_bl,root->getRightChile() );
- }
- }
- //5.1 2 3
- template<class T>
- int BinaryTree<T>::sta_deg(int deg ,BinaryTreeNote<T>* root ){
- int a=0;
- int p=0;
- if(root->getLeftChild() ){
- a++;
- p+=this->sta_deg(deg,root->getLeftChild());
- }
- if(root->getRightChile()){
- a++;
- p+=this->sta_deg(deg,root->getRightChile());
- }
- if(a==deg)
- return 1+p;
- else
- return p;
- }
- //5.4
- template<class T>
- int BinaryTree<T>::high(BinaryTreeNote<T>* root){
- int i=1,j=1;
- if(root->getLeftChild() )
- i+=this->high(root->getLeftChild() );
- if(root->getRightChile() )
- j+=this->high(root->getRightChile() );
- return i>j?i:j;
- }
- //5.5
- template<class T>
- int BinaryTree<T>::wide(){
- queue<BinaryTreeNote<T>*> k;
- k.push(this->rt);
- BinaryTreeNote<T>* p=this->rt;
- k.push(0);
- int con=0;
- int con_pre=0;
- while( 1){
- if(k.front()==0 ){
- con_pre=con_pre>con?con_pre:con;
- con=0;
- k.pop();
- if(k.empty())
- break;
- k.push(0);
- continue;
- }
- p=k.front();
- if(p->getRightChile() )
- k.push(p->getRightChile());
- if(p->getLeftChild())
- k.push(p->getLeftChild());
- con++;
- k.pop();
- }
- return con_pre;
- }
- template<class T>
- int BinaryTree<T>::biggest(BinaryTreeNote<T>* root,T big){
- big=big>root->data?big:root->data;
- if(root->getLeftChild() ){
- big=big>this->biggest(root->getLeftChild(),big)?big:this->biggest(root->getLeftChild(),big) ;
- }
- if(root->getRightChile() ){
- big=big>this->biggest(root->getRightChile(),big)?big:this->biggest(root->getRightChile(),big);
- }
- return big;
- }
- template<class T>
- void BinaryTree<T>::swap(BinaryTreeNote<T>* root){
- if(! root)
- return ;
- BinaryTreeNote<T>* aw=root->getLeftChild();
- root->left=root->getRightChile();
- root->right=aw;
- this->swap(root->getLeftChild());
- this->swap(root->getRightChile());
- }
- template<class T>
- void BinaryTree<T>::del_leaf(BinaryTreeNote<T>* root,BinaryTreeNote<T>* par){
- if(root->getLeftChild()||root->getRightChile()){
- if(root->getLeftChild() )
- this->del_leaf(root->getLeftChild(),root);
- if(root->getRightChile())
- this->del_leaf(root->getRightChile(),root);
- return ;
- }
- if( root->getLeftChild()==0 && root->getRightChile()==0 ){
- if(par->getLeftChild()==root )
- par->left=0;
- else
- par->right=0;
- delete root;
- }
- }
- template<class T>
- bool BinaryTree<T>::isCom(){
- queue<BinaryTreeNote<T>* > n;
- BinaryTreeNote<T>* cur;
- BinaryTreeNote<T>* o;
- for( o=rt ; o->getLeftChild(); )o=o->getLeftChild();
- o->left=(BinaryTreeNote<T>*)1;
- n.push(rt);
- int p=0;
- while(!n.empty() ){
- cur=n.front();
- n.pop();
- if(cur==0){
- p++;
- continue;
- }
- if(p!=0){
- p=-1;
- break;
- }
- if(cur==(BinaryTreeNote<T>*)1&&p==0){
- p=0;
- break;
- }
- n.push(cur->getLeftChild());
- n.push(cur->getRightChile());
- }
- o->left=0;
- return p!=-1;
- }
- template<class T>
- void BinaryTree<T>::build_clu(){
- BinaryTreeNote<T>* cur=rt;
- BinaryTreeNote<T>* p=0;
- stack<BinaryTreeNote<T>* > n;
- while( !n.empty() || cur!=0 ){
- if(cur ){
- n.push(cur);
- cur=cur->getLeftChild();
- }
- else{
- cur=n.top();
- if(cur->getLeftChild()==0 && cur->getRightChile()==0 && p){
- p->right=cur;
- cur->left=p;
- p=cur;
- }
- if(cur->getLeftChild()==0)
- cur->l=0;
- if(cur->getRightChile()==0)
- cur->r=0;
- cur=cur->getRightChile();
- n.pop();
- }
- }
- cout<<endl;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::clu_hj(BinaryTreeNote<T>* root ){
- BinaryTreeNote<T>* cur=root->getRightChile();
- if(!cur)
- return 0;
- while(cur->l )
- cur=cur->getLeftChild();
- return cur;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTree<T>::cluqx(BinaryTreeNote<T>* root ){
- return root->getLeftChild();
- }
二叉树定义.cpp
- #pragma once
- #include "BinaryTree.h"
- int main(){
- BinaryTree<int> a;
- /*
- a.build( a.getDRoot());
- cout<<"a.levelOrder(a.getRoot() );"<<endl;
- a.levelOrder(a.getRoot() ); //right!
- cout<<"a.postOrder(a.getRoot());"<<endl;
- a.postOrder(a.getRoot());
- cout<<endl;
- cout<<"a.inOrder(a.getRoot());"<<endl;
- a.inOrder(a.getRoot());
- cout<<"a.preOrder(a.getRoot());"<<endl;
- a.preOrder(a.getRoot());
- cout<<"a.deleteBinaryTree(a.getRoot()->getLeftChild());"<<endl;
- a.deleteBinaryTree(a.getRoot()->getLeftChild());
- a.levelOrder(a.getRoot() );
- exit(0);
- */
- cout<<"输入节点数"<<endl;
- a.build_qz();
- cout<<"a.postOrder(a.getRoot());";
- a.postOrder(a.getRoot());
- cout<<endl;
- cout<<"a.preOrder(a.getRoot());";
- a.preOrder(a.getRoot());
- cout<<endl<<"a.inOrder(a.getRoot());";
- a.inOrder(a.getRoot());
- cout<<"a.sta_deg(1,a.getRoot())"<<a.sta_deg(1,a.getRoot())<<endl;
- cout<<"a.wide()"<<a.wide()<<endl;
- cout<<"a.biggest(a.getRoot(),a.getRoot()->getValue())"<<a.biggest(a.getRoot(),a.getRoot()->getValue())<<endl;
- a.swap(a.getRoot());
- cout<<"a.swap(a.getRoot());";
- a.levelOrder(a.getRoot());
- a.del_leaf(a.getRoot(),a.getRoot() );
- cout<<"a.del_leaf(a.getRoot(),a.getRoot() );";
- a.levelOrder(a.getRoot());
- if( a.isCom( ))
- cout<<"完全"<<endl;
- else
- cout<<"不完全"<<endl;
- a.build_clu();
- cout<<"a.clu_hj(a.getRoot())"<<a.clu_hj(a.getRoot())->getValue()<<endl;;
- cout<<"a.clu_qx(a.getRoot())"<<a.clu_qx(a.getRoot())->getValue()<<endl;
- return 0;
- }
BinaryTreeNote.h
- #pragma once
- #include<iostream>
- template<class T>
- class BinaryTree;
- template<class T>
- class BinaryTreeNote{
- friend class BinaryTree<T>;
- BinaryTreeNote<T>* right;
- BinaryTreeNote<T>* left;
- T data;
- int l;
- int r;
- public:
- BinaryTreeNote():right(0),left(0),l(0),r(0) { }
- BinaryTreeNote(const T& ele );
- BinaryTreeNote(const T& ele,BinaryTreeNote<T>* l,BinaryTreeNote<T>*r );
- BinaryTreeNote<T>* getLeftChild( );
- BinaryTreeNote<T>* getRightChile( );
- void setLeftChild(BinaryTreeNote<T>* l );
- void setRightChild(BinaryTreeNote<T>* r );
- T getValue( ) const;
- void setValue(const T& val);
- bool isLeaf() const;
- ~BinaryTreeNote();
- };
- template<class T>
- BinaryTreeNote<T>::BinaryTreeNote(const T& ele):right(0),left(0){
- this->data=ele;
- }
- template<class T>
- BinaryTreeNote<T>::BinaryTreeNote(const T& ele,BinaryTreeNote<T>* l,BinaryTreeNote<T>*r){
- this->data=ele;
- this->left=l;
- this->right=r;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTreeNote<T>::getLeftChild(){
- return this->left;
- }
- template<class T>
- BinaryTreeNote<T>* BinaryTreeNote<T>::getRightChile(){
- return this->right;
- }
- template<class T>
- T BinaryTreeNote<T>::getValue()const{
- return this->data;
- }
- template<class T>
- bool BinaryTreeNote<T>::isLeaf()const {
- return ! (this->left||this->right);
- }
- template<class T>
- void BinaryTreeNote<T>::setLeftChild(BinaryTreeNote* l ){
- this->left=l;
- }
- template<class T>
- void BinaryTreeNote<T>::setRightChild(BinaryTreeNote* r ){
- this->right=r;
- }
- template<class T>
- BinaryTreeNote<T>::~BinaryTreeNote(){
- }