数据结构实验四

源代码

  1. #include  
  2. using namespace std;  
  3.   
  4. class BiTree      
  5. {      
  6. public:      
  7.     BiTree(){memset(Tree,0,sizeof(Tree));}      
  8.     ~BiTree(){}      
  9.     int createbitree();    
  10.     int visit(int n);    
  11.     int calnode(int i,int &n);      
  12.     int depth(int n);      
  13.     int max(int a,int b);   
  14.     int parent();  
  15.     int children();  
  16.     void leaves();  
  17.     int leavesnode();  
  18. private:  
  19.     int Tree[100];      
  20. };      
  21.   
  22.   
  23. int BiTree::createbitree()      
  24. {     
  25.     int n,i=1;     
  26.     cout<<"请按照从上到下的顺序依次输入二叉树各个节点,空节点用0表示,以-1表示输入结束:"<>n && n!=-1)     
  27.     {    
  28.         Tree[i]=n;    
  29.         i++;    
  30.     }    
  31.     return 0;    
  32. }   
  33.      
  34.   
  35. int BiTree::visit(int n)      
  36. {   
  37.     for(n=1;n<100;n++)  
  38.     {  
  39.     if(Tree[n]!=-1 && Tree[n]!=0)    
  40.         cout<>x;  
  41.     for(n=1;n<100;n++)  
  42.     {  
  43.         if(Tree[n]==x)  
  44.             cout<<"双亲:"<>x;  
  45.     for(n=1;n<100;n++)  
  46.     {  
  47.         if(Tree[n]==x)  
  48.         {  
  49.             if(Tree[2*n]!=0 && Tree[2*n+1]!=0)  
  50.                 cout<<"左孩子:"<b?a:b;  
  51. }  
  52.   
  53.   
  54. int BiTree::depth(int n)  
  55. {  
  56.     int hl,hr;  
  57.     for(n=1;n<100;)  
  58.     {  
  59.         if (Tree[n]!=-1)   
  60.         {  
  61.             hl = depth(2*n);  
  62.             hr = depth(2*n+1);  
  63.         }     
  64.     }  
  65.     return 1+ max(hl, hr);  
  66. }  
  67.   
  68.   
  69. int main()      
  70. {  
  71.   
  72.     BiTree tree;      
  73.     int n=0;   
  74.       
  75.     tree.createbitree();   
  76.     cout<  

运行结果


猜你喜欢

转载自blog.csdn.net/klay_thompson/article/details/80484523