c++ツリー(リーフノードを統計し、2つのツリーが等しいかどうかを判断し、ツリーの左の子供と右の子供を交換し、リーフをルートノードに出力する経路)

5410 ワード

/*               :
        1:           .
        2:         
        3:                 
    4:          (                 ,       
        ,           ,         ,             )
    5:          (             )
    6:            ,      1     
    7:              ,           
        8:                    
*/
#include 
using namespace std;

#define MAXSIZE 100              //          
//***********   **************
class Tree;
class Node
{
private:
 int Data;
 Node* Lchild;
 Node* Rchild;
public:
 Node()
 {
  Lchild = NULL;
  Rchild = NULL;
 }
 int Return_Data(){return Data;}               //     
 Node* Return_Lchild(){return Lchild;}         //     
 Node* Return_Rchild(){return Rchild;}         //     
 friend class Tree;                    //  Node  Tree    
};

//**********Tree ***************
class Tree
{
private:
 int Leaf_Numbers;
 Node* Root;
 int Front;            //    
 int Rear;             //    
 Node* NodeStack[MAXSIZE];
public:
 Tree()
 {
  Leaf_Numbers = 0;
  Root = NULL;
  Front = -1;
  Rear = 0;
  for(int i = 0; i < MAXSIZE; i++)
  {
   NodeStack[i] = NULL;
  }
 }
 //************  **************
 void CreatTree(Node* &);                     //        
 void TheCreatTree();
 void TraveFront(Node* );                         //    
 void TheTraveFront();
 void Numbers_Leafs(Node*);                       //       
 void TheNumbers_Leafs();
 int Return_LeafNums();
 Node* Return_Root(){return Root;}             //       
 void ExchangeLR(Node*);                           //          
 void TheExchangeLR();
 void Leaf_Root(Node*);                       //         
 /*  :1.                            
         2.           ,      
         3.      ,                ,          
                    
         4.          ,    ,     ,               
                   
         5.  3 4*/
 void TheLeaf_Root();
};
void Tree::CreatTree(Node* &p)
{
 int x;
 cin>>x;
 if(x != -1)
 {
  p = new Node();
  p->Data = x;
  CreatTree(p->Lchild);
  CreatTree(p->Rchild);
 }
}
void Tree::TheCreatTree()
{
 
 CreatTree(Root);
}
void Tree::TraveFront(Node* p)
{
 if(p != NULL)
 {
  cout<Data<Lchild);
  TraveFront(p->Rchild);
 }
}
void Tree::TheTraveFront()
{
 TraveFront(Root);
}
void Tree::Numbers_Leafs(Node* p)
{
 if(p != NULL)
 {
  if(p->Lchild == NULL && p->Rchild == NULL)    //      
  {
   cout<Data<Lchild);
   Numbers_Leafs(p->Rchild);
  }
 }
 /*if(p != NULL)                                    //    
 {
  Numbers_Leafs(p->Lchild);
  if(p->Lchild == NULL && p->Rchild == NULL)
  {
   Leaf_Numbers = Leaf_Numbers + 1;
   cout<Data<Rchild);
 }*/
 /*if(p->Lchild == NULL && p->Rchild == NULL)    //      
 {
  cout<Data<Lchild != NULL && p->Rchild != NULL)
  {
   Numbers_Leafs(p->Lchild);
   Numbers_Leafs(p->Rchild);
  }
   
  if(p->Lchild != NULL && p->Rchild == NULL)
   Numbers_Leafs(p->Lchild);
  if(p->Lchild == NULL && p->Rchild != NULL)
   Numbers_Leafs(p->Rchild);
 }*/
}
void Tree::TheNumbers_Leafs()
{
 Numbers_Leafs(Root);
}
int Tree::Return_LeafNums()
{
 return Leaf_Numbers;
}
/*
           ,           
        
*/
bool Compare(Node* p1,Node* p2)
{
 if(p1 == NULL && p2 == NULL)
 return true;
 else
 if((p1 == NULL && p2 != NULL) || (p1 != NULL && p2 == NULL))
  return false;
 else
  if(p1->Return_Data() != p2->Return_Data())
   return false;
  else
  /*{
   bool Is_Left = Compare(p1->Return_Lchild(),p2->Return_Lchild());
   bool Is_Right = Compare(p1->Return_Rchild(),p2->Return_Rchild());
   if(Is_Left && Is_Right)
    return true;
   else
    return false;
    
  }*/
   return (Compare(p1->Return_Lchild(),p2->Return_Lchild()) && Compare(p1->Return_Rchild(),p2->Return_Rchild()));
}
bool TheCompare(Tree &tree1,Tree &tree2)
{
 Node* p1 = tree1.Return_Root();
 Node* p2 = tree2.Return_Root();
 return Compare(p1,p2);
}
void Tree::ExchangeLR(Node* p)
{
 if(p->Lchild != NULL && p->Rchild != NULL)
 {
  Node* temp;
  temp = p->Lchild;
  p->Lchild = p->Rchild;
  p->Rchild = temp;
  ExchangeLR(p->Lchild);
  ExchangeLR(p->Rchild);
 }
 else
 {
  return;
 }
}
void Tree::TheExchangeLR()
{
 ExchangeLR(Root);
}
void Tree::Leaf_Root(Node* p)
{
 if(p != NULL)
 {
  Front = Front + 1;                  //     1
  NodeStack[Front] = p;               //  
  
  Leaf_Root(p->Lchild);
  Leaf_Root(p->Rchild);
 }
 if(NodeStack[Front]->Lchild == NULL && NodeStack[Front]->Rchild == NULL)     //       ,    
 {
  while(NodeStack[Front]->Rchild == NULL)                                  //                ,  
  {
   
   cout<Data<";
   Front = Front - 1;
  }
        while(NodeStack[Front]->Rchild == NodeStack[Front+1] && Front != 0)       // Front = -1     
  {                  //        ,       while  ,  
   cout<Data<";            //               ,     
   Front = Front - 1;
  }

  int temp = Front;
  while(temp != Rear - 1)             //             ,      
  {
   
   if(temp == 0)
    cout<Data;
   else
    cout<Data<";
   temp = temp - 1;
  }
  cout<