/*********************************
          WZ  ASUST  2016
1.网上拼图游戏测试(错误处已注释)(产生的数组正好是不可解拼图,游戏无退出)
2.二叉树  网页上的再添加函数  层次遍历建立等 树状打印,缺竖状打印(一说横向打印,如下链接)
//http://www.aemiot.com/lanqiao-001.html//
*********************************/
    //bug    last line can not swap with n-1
//http://www.zhihu.com/question/22547591/
 #include<iostream>
 #include<queue>
#include <queue>  
#include <vector>  
#include <list>  
#include <math.h>
#include <cstdio>
using namespace std;
template<class T>  
struct Binary_node  
{   T data;  
    Binary_node<T>* left;    Binary_node<T>* right;  
    Binary_node(){ left = right = NULL; }
    Binary_node(const T &x){left = right = NULL;  data = x; }
};  
template<class T>  
class Binary_tree  
{  
public:  
    Binary_tree():root(NULL){};
    Binary_tree(const Binary_tree<T>&original){ root = original.get_root();  }  ;  
    Binary_tree & operator=(const Binary_tree<T>&original){equal(root,original.get_root());      return *this;}
    ~Binary_tree(){      clear();  }  
    bool empty() const{  return root == NULL;  }  
    void preorder(void (*visit)(T &)) { recursive_preorder(root, visit);  } /*/ DLR   /*/
    void inorder(void  (*visit)(T &)) { recursive_inorder(root, visit);   } /*/ LDR  /*/
    void postorder(void (*visit)(T &)){ recursive_postorder(root, visit); } /*/ LRD  /*/
    int size() const{ return recursive_size(root);  }         /*/ Binary_tree大小/*/      
    int height() const;  
    void clear(){  recursive_clear(root);  }    
    void insert(const T& x) {    recursive_insert(root, x);  }  
    void reverse(){      recursive_reverse(root);  }  
    const Binary_node<T>* get_root() const{      return root;  }  
 
    void leaf()
   {
    Binary_node<T> *b;
   cout<<"leaf number:"<<_leaf(root)+1<<endl;
   cout<<"and the leaf element :"<<endl;
while(!q.empty())
{
    b=q.front(); cout<<b->data<<" "<<endl;q.pop();
 }  
   }
 void createlay()
   {
    Binary_node<T>*cur=root;
    Binary_node<T>*p=root;
     queue<Binary_node<T>* > q;  
     Binary_node<T> *b;
    int f=1,r=0;
    T c;
    cin>>c;
    root=new Binary_node<T>; q.push(root);  root->data=c;
   while(c!=-1)
    {
       if(r%2==0)
              {root->left=new Binary_node<T>;  q.push(root->left);}
       else
              {root->right=new Binary_node<T>;  q.push(root->right);}
       if(r%2==1)f++;if(f%2==0)root=q.front(); q.pop();
      r++;
    cin>>c;
    }
   }  
    void createBiTree()   { _createBiTree(root);}  
    void lay(int n)                         /*/ 层次打印 /*/
   {  //assert(root)
      int k=1; int m=1;int i=0;
      Binary_node<T> *b=root;
      queue<Binary_node<T>* > q;  
          q.push(b);  
    while(!q.empty())
  {
     b=q.front();  if(b->left) q.push(b->left); if(b->right) q.push(b->right);
     m=pow(2,k-1); for(i=0;i<n/2;i++) cout<<" ";cout<<b->data; q.pop();
     while(--m)
       {
        for(i=0;i<2*(n/2)+1;i++) cout<<" "; if(!q.empty()){ b=q.front();  if(b->left) q.push(b->left); if(b->right) q.push(b->right);cout<<b->data; q.pop();}
else cout<<"#";
       }
      cout<<endl;
      n=n/2;
      k++;
   }
}
   
private:      
    int recursive_size(const Binary_node<T>*root) const{  
    if(root == NULL)  
        return 0;  
    else  
        return 1 + recursive_size(root->left) + recursive_size(root->right) ;  
}  
    int recursive_height(const Binary_node<T>*root) const;  
    void equal(Binary_node<T>*&sub_root,const Binary_node<T>*orig_node)
 {   if(empty())  
        sub_root = new Binary_node<T>(orig_node->data);  
    if(orig_node->left!=NULL)  
    {  
        sub_root->left = new Binary_node<T>(orig_node->left->data);  
        equal(root->left, orig_node->left);  
    }  
    if(orig_node->right!=NULL)  
    {  
        sub_root->right = new Binary_node<T>(orig_node->right->data);  
        equal(root->right, orig_node->right);  
    }  
}
     
   
    void recursive_reverse(Binary_node<T> * & sub_root)
{  
    if(sub_root!=NULL)  
    {  
        Binary_node<T>* temp = NULL;  
        temp = sub_root->left;  
        sub_root->left = sub_root->right;  
        sub_root->right = temp;  
        recursive_reverse(sub_root->left);  
        recursive_reverse(sub_root->right);  
    }  
}        
    void recursive_clear(Binary_node<T> * & sub_root){  
    if(sub_root != NULL)  
    {  
        recursive_clear(sub_root->left);  
        recursive_clear(sub_root->right);  
        delete sub_root;  
        sub_root = NULL;  
    }  
 
}    
    void recursive_insert(Binary_node<T> * & sub_root, const T& x)
{  
    if(sub_root == NULL)  
    {  
        Binary_node<T>* ins_data = new Binary_node<T>(x);  
        sub_root = ins_data;  
        return;  
    }  
    else  
    {  
        if(recursive_height(sub_root->left) > recursive_height(sub_root->right))  
            recursive_insert(sub_root->right, x);  
        else  
            recursive_insert(sub_root->left, x);  
    }  
}    
 
   
    void recursive_preorder(Binary_node<T> * sub_root, void (*visit)(T &))
	{   if (sub_root!=NULL)  
    	 {  
         (*visit)(sub_root->data);  
         recursive_preorder(sub_root->left, visit);  
         recursive_preorder(sub_root->right, visit);  
        }  
      }  
 
    void recursive_inorder(Binary_node<T> * sub_root, void (*visit)(T &))
   {   if(sub_root!=NULL)  
    	{   recursive_inorder(sub_root->left, visit);  
       	  (*visit)(sub_root->data);  
       	 recursive_inorder(sub_root->right, visit);  
    	}  
     }
    void recursive_postorder(Binary_node<T> * sub_root, void (*visit)(T &))
 {   if (sub_root!=NULL)  
    {    recursive_postorder(sub_root->left, visit);  
        recursive_postorder(sub_root->right, visit);  
        (*visit)(sub_root->data);  
    }  
}
    int _leaf(Binary_node<T> * sub_root)
{
    if(NULL == sub_root) return 0;  
    if(NULL == sub_root->left&& NULL == sub_root->right)  
       { return 1;  q.push(sub_root);}
    return _leaf(sub_root->left) + _leaf(sub_root->right);
}
 void _createBiTree(Binary_node<T> *&sub_root)
 { T c;   cin >> c;  
  if(-1== c)  sub_root = NULL;  
    else  
    {    sub_root = new Binary_node<T>;
        sub_root->data = c;  
        _createBiTree(sub_root->left);  
        _createBiTree(sub_root->right);  
    }  
}
protected:  
    Binary_node<T>* root;  
    queue<Binary_node<T>* > q;
 
};
#ifndef BINARY_TREE_CPP_X  
#define BINARY_TREE_CPP_X  
 
template<class T>  
int Binary_tree<T>::height() const  
{  
    return recursive_height(root);  
}  
#define max MAX  
template<class Comparable>  
Comparable MAX(const Comparable& a, const Comparable& b)  
{  
    return a > b ? a : b;  
}  
template<class T>  
int Binary_tree<T>::recursive_height(const Binary_node<T>*root) const  
{  
    if(root == NULL)  
        return 0;  
    else  
        return 1 + max(recursive_height(root->left) , recursive_height(root->right)) ;  
}  
#undef max  
 
#endif  
 
 void test10()
{
Binary_tree<int> dd;  
//dd.createBiTree();
dd.createlay();
dd.lay(9);
}
 
void print( int& x)  {   cout<<x<<" ";  }
 int main()
{
Binary_tree<int> dd;  
    dd.insert(1);  
    dd.insert(2);  
    dd.insert(3);  
    dd.insert(4);  
    dd.insert(5);  
    dd.insert(6);  
    dd.insert(7);
    Binary_tree<int> ww;  
    ww = dd;  
    ww.insert(10);  
     ww.insert(7);  
     cout<<"preorder:";  
    dd.preorder(print);  
     cout<<endl;  
    cout<<"preorder:";  
    ww.preorder(print);  
     cout<<endl;
     dd.lay(9);  
    cout<<endl;
    dd.reverse();  
    cout<<"preorder:";  
     dd.preorder(print);  
      cout<<endl;
    dd.lay(9);  
    dd.leaf();
    cout<<endl;
    return 0;  
}

viewfile?f=A2BCBF0427C89DDFE9B81DF1BF635//bug