求二叉树中每一个节点对应的层次

转载自:http://wenku.baidu.com/view/b9c8c4115f0e7cd1842536da.html
#include "stdafx.h"
#include
using namespace std;
 int i=1;  
typedef struct bnode
{  
   char data; 
   struct bnode*lchild,*rchild;
 }bnode;
 class bitree
 { 
   public:  
    void preorder(bnode*t);
    void inorder(bnode*t); 
    void postorder(bnode*t);  
    bnode * get_root(){return root;} 
    void creat(bnode*&t);  
    void inorder_printjie(bnode*t,int i);
  private:
     bnode * root;
    };
  void bitree::preorder (bnode*t)
   { 
     if(t!=NULL)
       { 
          cout<data<<" ";
         preorder(t->lchild );
          preorder(t->rchild );
        }
    }  
  void bitree::inorder (bnode*t)
{ 
 if(t!=NULL)
   {   inorder(t->lchild ); 
       cout<data<<" " ;
       inorder(t->rchild );
    }
 }

void bitree::creat (bnode*&t)
{  
   char x; 
   cin>>x;
   if(x=='.')t=NULL; 
   else
      {   
		t=new bnode;  
        t->data =x;  
        creat(t->lchild );
        creat(t->rchild );
     } 
}
void bitree::inorder_printjie (bnode*t,int i) 
{  
  if(t!=NULL)
 {  
	 
   inorder_printjie(t->lchild ,i+1);  
   cout<<" ("<data <<","<rchild ,i+1);
  } 
} 
 void main()
{  
  bitree a;
  bnode*t=a.get_root ();
  cout<<"请输入A二叉树,以.表示没有孩子数目:"<
虽然能够求得每个节点对应的层次,但还是不能求得每一层的节点,还需要好好研究才行。


你可能感兴趣的:(数据结构)