递归创建二叉树,前序,中序,后序遍历二叉树,广义表创建二叉树,非递归前序,中序,后序遍历二叉树

来源:互联网 发布:mac桌面图标隐藏 编辑:程序博客网 时间:2024/04/30 11:49

 #ifndef TERRMSGLIST_H
#define TERRMSGLIST_H

#include<string>
using std::string;
#include<iostream>
using std::cout;
using std::endl;

class CErrMsgList
{     struct ErrMsgRecord
    {  int iNumber;
       // vector<string> msg;
       char msg[30];
 };
       ErrMsgRecord MsgList[100];
       public:
           int iLen;
          CErrMsgList( int icase)
          { // int i = 0;
          switch( icase )
            { case 0:
                   { MsgList[0].iNumber = 0;/*0*/
                  strcpy(MsgList[0].msg,"Unknown error") ;
            // i++;
                   break;
                   }
              case 1:
                   {    
              MsgList[1].iNumber = 1;
             strcpy(MsgList[1].msg ,"pramete is illegal");/*1*/
            // i++;
                   break;
                   }
              case 2:    
            {       MsgList[2].iNumber = 2;
                    strcpy(MsgList[2].msg , "Out Of Range");/*2*/
                    // i++;
             break;
             }
             case 3:
                  {
                   MsgList[3].iNumber = 3;
                   strcpy(MsgList[3].msg,"Memory space low");/*3*/
                    //i++;
             break;
             }
            case 4:
                 {
                 MsgList[4].iNumber = 4;
                 strcpy(MsgList[4].msg , "Not Found");/*4*/
                  // i++;
            break;
                }
            default:
            break;  
              }//switch
             
               iLen = 4;
                    }
                      
     char * GetMessage(int num )
     {  if(num < iLen || num > 100)return NULL;
       cout<<MsgList[num].msg<<endl;
        return MsgList[num].msg;
          }    
         
      };
#endif

#ifndef CBTREE_H
#define CBTREE_H
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

#include<new>
#include<iomanip>
using std::setw;
using std::left;
#include <assert.h>

 

#include "CErrMsgList.h"
//#include "CTreeObject.h"
template<class T>
   struct BTreeNode
   { T data;
    BTreeNode* left;
      BTreeNode * right;
   BTreeNode( const T &d):data( d ),left( NULL ),right( NULL )
   {};
   };   

template<class T>
 //class BTree : public CTreeObject<T>
 class BTree
 
{ //protected:
public:
 BTreeNode<T>* root;
 
 
  public:
    BTree();
   virtual ~BTree();
    //virtual ObjectNode<T>* GetParent_( ObjectNode<T>* pNode ) = 0;
 //virtual ObjectNode<T>* GetLevel( ObjectNode<T>* pNode ) = 0;
 virtual void Insert( BTreeNode<T>* pNode,T &element );
 virtual BTreeNode<T>* GetLeftChild( BTreeNode<T>* pNode )const ;
 virtual BTreeNode<T>* GetRightChild( BTreeNode<T>* pNode )const ;
 BTreeNode<T>* GetRoot()const;
 virtual /*BTreeNode<T>**/void SetLeftChild( BTreeNode<T>* pNode,T &element ) ;
 virtual /*BTreeNode<T>**/void SetRightChild( BTreeNode<T>* pNode,T &element );
 //递归创建二叉树
    virtual void CreateBTree_recursion( BTreeNode<T>**ptr, const T &element );
    
    virtual void Create( const T &element );
 virtual void PutOut( BTreeNode<T>* pNode )const;
    virtual void PreOrder( BTreeNode<T>* pNode )const;
    virtual void InOrder( BTreeNode<T>* pNode )const;
    virtual void ClusterPoster( BTreeNode<T>* pNode )const;
 //BTreeNode<T>* LevelOrder( BTreeNode<T>* pNode );
 virtual void Visiter()const;
 virtual void Release( BTreeNode<T>*  pNode );
 };  

template<class T>
BTree<T>::BTree()
{  
   root = NULL;
 
 
}

template<class T>
BTree<T>::~BTree()
{ Release( GetRoot() );
}

template<class T>
void BTree<T>::Create( const T &element )
{   CreateBTree_recursion( &root,element);
}

template<class T>
void BTree<T>::Insert( BTreeNode<T>* pNode,T &element )

}

template<class T>
 void BTree<T>::CreateBTree_recursion( BTreeNode<T>**ptr ,const T &element )
 { 
 if(*ptr == NULL )
   {
   (*ptr) = new BTreeNode<T> (element);
   assert( *ptr!= NULL );
   }
else if(element < (*ptr)->data)
     CreateBTree_recursion(&(*ptr)->left, element);
else if(element > (*ptr)->data)
     CreateBTree_recursion(&(*ptr)->right, element);
else
     cout<<element<<" dup"<<endl;
   
 }
 


template<class T>
BTreeNode<T>* BTree<T>::GetRoot()const
{ return root;
}
template<class T>
/*BTreeNode<T>**/void BTree<T>::SetLeftChild( BTreeNode<T>* pNode ,T &element)
{ BTreeNode<T>* p = new (BTreeNode<T>)( element);
 
  //if( p == NULL )throw CErrMsgList( 3 );
 
 // p->left = NULL;
  //p->right = NULL;
 // p->data = element;
  pNode->left = p;
 // node++;
 // return p;
}

template<class T>
/*BTreeNode<T>**/void BTree<T>::SetRightChild( BTreeNode<T>* pNode,T &element )
{ BTreeNode<T>* p = new (BTreeNode<T>)(element);
 
  //if( p == NULL )throw CErrMsgList( 3 );
 
 // p->left = NULL;
 // p->right = NULL;
 // p->data = element;
  pNode->left = p;
 // node++;
 // return p;
}

template<class T>
BTreeNode<T>* BTree<T>::GetLeftChild( BTreeNode<T>* pNode )const
{// if( pNode->left != NULL )
 //return pNode->left;
 return pNode->left != NULL ? pNode->left : NULL;
}

template<class T>
BTreeNode<T> * BTree<T>::GetRightChild( BTreeNode<T>* pNode )const
{ return pNode->right != NULL ? pNode->right : NULL;
}

template<class T>
void BTree<T>::InOrder( BTreeNode<T>* pNode )const
{ if( pNode != NULL )
{
 InOrder( GetLeftChild( pNode ) );
 PutOut( pNode );
 InOrder( GetRightChild( pNode ) );
}
}

template<class T>
void BTree<T>::PreOrder( BTreeNode<T>* pNode )const 
{ if( pNode != NULL )
 { PutOut( pNode );
  // PreOrder( GetLeftChild( pNode ) );
  // PreOrder( GetRightChild( pNode ) );
  PreOrder( pNode->left );
  PreOrder( pNode->right );
 }
}

template<class T>
void BTree<T>::ClusterPoster( BTreeNode<T>* pNode )const 
{ if( pNode != NULL )
 {  ClusterPoster( GetLeftChild( pNode ) );
    ClusterPoster( GetRightChild( pNode ) );
 PutOut( pNode );
 }
}

template<class T>
void BTree<T>::Release( BTreeNode<T>* pNode )
{  if( pNode != NULL )
{  Release( pNode->left );
   Release( pNode->right );
   delete pNode;
}
}

/*
template<class T>
BTreeNode<T>* BTree<T>::LevelOrder( BTreeNode<T>* pNode )
{
}
*/
template<class T>
void BTree<T>::PutOut( BTreeNode<T>* pNode )const
{  if( pNode != NULL )
 cout<<" "<<left<<( pNode->data )<<setw( 2 );
}

template<class T>
void BTree<T>::Visiter()const
{ int selected;
  cout<<"please input 1 to putout in preorder,2 to putout in inorder,"
  <<"3 to putout in clusterorder,4 to putout in levelorder"
  <<"0 to exit/n";
  cin>>selected;
  do{
  switch( selected )
  { case 1:
   PreOrder( GetRoot() );
   cout<<endl;
   cin>>selected;
   break;
    case 2: 
   InOrder( GetRoot() );
   cout<<endl;
   cin>>selected;
      break;
    case 3:
   ClusterPoster( GetRoot() );
   cout<<endl;
   cin>>selected;
   break;
    case 4:
  // LevelOrder( root );
   break;
    default :
      break; 
  }
  }while(selected != 0);
}

#endif

#ifndef BTREE2_H
#define BTREE2_H
#include<iostream>
using std::cin;
using std::cout;
using std::endl;

#include<new>
#include<iomanip>
using std::setw;
using std::left;
#include <assert.h>
#include <cmath>
#include <stdlib.h>

#include "CBTree.h"
#include "CErrMsgList.h"

template<class T>
class BTree2 : public BTree<T>
{ protected:
     // BTreeNode<T>* root; 
 public:
 BTree2();
   virtual ~BTree2();
   virtual void Create2( const char *, const int  );//广义表创建二叉树
 BTreeNode<T>* GListToTree( const char *pArray, const int length );
    virtual void PreOrder( BTreeNode<T>* pNode )const;
    virtual void InOrder( BTreeNode<T>* pNode )const;
   virtual void ClusterPoster( BTreeNode<T>* pNode )const;
   virtual void Release( BTreeNode<T>*  pNode );
   virtual int GetHeight( BTreeNode<T>* pNode )const;
 
   virtual void Visiter()const;
}; 

 

template<class T>
BTree2<T>::BTree2()//:root( NULL )
{ //root = NULL;
}

template<class T>
 BTree2<T>::~BTree2()
{  Release( root );
}

template<class T>
void BTree2<T>::Release( BTreeNode<T>* pNode )
{  if( pNode != NULL )
{  Release( pNode->left );
   Release( pNode->right );
   delete pNode;
}
}

template<class T>
void BTree2<T>::PreOrder( BTreeNode<T>* pNode )const
{ BTreeNode<T> **pStack;
  int top = 0;
  int size = GetHeight( pNode );
   if(size <= 0 )return;
  pStack = new (BTreeNode<T>*)[size + 1];
  assert( (pStack) != NULL );
  while( pNode != NULL || top != 0)
  { if( pNode != NULL )
    { BTree<T>::PutOut( pNode );
      pStack[++top] = pNode;//push pNode into stack
   pNode = pNode->left;//visit left child
    }
   else
   {  pNode = pStack[top--];
      pNode = pNode->right;
   }   
  }
  delete [] pStack;
}

template<class T>
void BTree2<T>::InOrder( BTreeNode<T>* pNode )const
{ BTreeNode<T> **pStack;
  int top = 0;
  int size = GetHeight( pNode );
   if(size <= 0 )return;
  pStack = new (BTreeNode<T>*)[size + 1];
  assert( (pStack) != NULL );
  while( pNode != NULL || top != 0)
  { if( pNode != NULL )
    { //BTree<T>::PutOut( pNode );
      pStack[++top] = pNode;//push pNode into stack
   pNode = pNode->left;//visit left child
    }
   else
   {  pNode = pStack[top];
      top--;
      BTree<T>::PutOut( pNode );
      pNode = pNode->right;
   }   
  }
  delete [] pStack;
}

template<class T>
void BTree2<T>::ClusterPoster( BTreeNode<T>* pNode )const
{
  BTreeNode<T>* p;
  int top = 0;
  int size = GetHeight( pNode );
  if(size <= 0 )return;
 
  struct ClusterNode
  {
    int IsFirst;
 BTreeNode<T>* pNode;
  };
  ClusterNode  *pStack;
  pStack = new (ClusterNode )[size + 1];
  assert( pStack != NULL );
  p = pNode;
  while( p!= NULL || top != 0 )
  { if(p != NULL )
    {   top++;
  pStack[top].pNode = p;
  pStack[top].IsFirst = 1;
        p = p->left;
    }
    else
 { if(pStack[top].IsFirst)
      { pStack[top].IsFirst = 0;
        p = (pStack[top].pNode)->right;
      }
   else
   { p = pStack[top].pNode;
     top--;
     BTree<T>::PutOut( p );
     p = NULL;
   }
 }
  }
  delete []pStack;
}

template<class T>
void BTree2<T>::Create2( const char chArray[],const int size ) 
{ //read information from chArray[]
 GListToTree( chArray,size);
}

template<class T>
BTreeNode<T>* BTree2<T>::GListToTree( const char *pArray, const int length )
{ BTreeNode<T> **pStack;
  BTreeNode<T>* rt;
  BTreeNode<T>* p;
  int top = 0;//point to the top of stack
  int i = 1;//index of pArray
 
  if( length < 2 )return NULL;
 
  pStack = new (BTreeNode<T>*)[length + 1 ];
  assert( pStack != NULL );
  p = new BTreeNode<T>(pArray[ i ] );//apply a new node as the root of binary tree
  if( p == NULL )
  { delete [] pStack ;
    throw CErrMsgList( 3 );
  }
  rt = p;
  pStack[++top] = p;//push p to stack
 
  do
  {
   i++;//get the next char
   if( pArray[ i ] == '(')
   { continue;
   }
   if( pArray[ i ] == ',' || pArray[ i ] == ')' )
   { top--;
   }
   else
   {   if( pArray[ i ] == '*' )
   { p = NULL;
   }
       else
       {      p = new (BTreeNode<T>)(pArray[ i ]);
              if( p == NULL )
                 {     delete [] pStack ;
                        throw CErrMsgList( 3 );
                 }
           if( pArray[ i - 1 ] == '(')
           {   pStack[top]->left = p;//pStack[top] store current root node
        }
           else
       { pStack[top]->right = p;//problem
    }
      
          }//else
  pStack[++top] = p; 
   }//else
  }while( top != 0 );
  delete  [] pStack ;
  return root = rt;
}

template<class T>
int BTree2<T>::GetHeight( BTreeNode<T>* pNode )const
{ if(pNode == NULL)
 return 1;
  else
   return int(fmax( GetHeight( pNode->left ),GetHeight( pNode->right ) ) + 1);
}

template<class T>
void BTree2<T>::Visiter()const
{ int selected;
  cout<<"please input 1 to putout in preorder,2 to putout in inorder,"
  <<"3 to putout in clusterorder,4 to putout in levelorder"
  <<"0 to exit/n";
  cin>>selected;
  do{
  switch( selected )
  { case 1:
   PreOrder( root );
   cout<<endl;
   cin>>selected;
   break;
    case 2: 
   InOrder( root );
   cout<<endl;
   cin>>selected;
      break;
    case 3:
   ClusterPoster( root );
   cout<<endl;
   cin>>selected;
   break;
    case 4:
  // LevelOrder( root );
   break;
    default :
      break; 
  }
  }while(selected != 0);
}
#endif

#include <iostream>
using std::cout;
using std::endl;

#include <cmath>
#include "CBTree.h"
#include "BTree2.h"

int main(int argc, char* argv[])
{  BTree<int> bt;
   //int number;  
   //cout<<"input 5 numbers:/n";
   static const int NodeSize = 10;
  
   for( int i =0;i < NodeSize;i++)
   {// cin>>number;
   bt.Create( 1 + rand()%100 );
   }
   bt.Visiter();
  
   BTree2<char> bt2;
   //(2(3(*,7(5,*)),9))
   char GList[18] = { '(' , '2' , '(' , '3' , '(' , '*' , ',' , '7' , '(' , '5' , ',' ,
                     '*' , ')' , ')' , ',' , '9' , ')' , ')' };
   bt2.Create2( GList,18 );     
   bt2.Visiter();
  
   BTree2<float> bt3;
   for( int i =0;i < NodeSize;i++)
   {// cin>>number;
   bt3.Create( (1 + rand()%100)*1.12 ) ;//调用基类版本
   }
   bt3.Visiter();
  system("pause");
 return 0;
}

原创粉丝点击