二叉树的建立,三个遍历,深度,节点数,叶节点数,度为一的节点数,输出二叉树中从每个叶节点到根节点的路径

数据结构上机作业,对二叉树的操作考察还是比较全面的

  1 //写完作业咯 
  2 #include<bits/stdc++.h>
  3 using namespace std;
  4 const int N=1010;
  5 int n;
  6 int pre[N];//
  7 int in[N];//
  8 int post[N];//
  9 int k;//记录节点个数
 10 int cnt;//统计叶节点个数 
 11 int one;//统计一个度的节点个数 
 12 int ans;//统计叶节点到根节点的路径条数
 13 int path[N];//记录从叶节点到根节点的路径 
 14 struct node
 15 {
 16     int value;//节点数据
 17     node *l;
 18     node *r;
 19     node(int value=0,node *l=NULL,node *r=NULL):value(value),l(l),r(r){} 
 20     
 21 };
 22 void buildtree(int l,int r,int &t,node * &root)//建树 
 23 {
 24     int flag=-1;
 25     for(int i=l;i<=r;i++)//先序第一个数是根,找到对应的中序位置 
 26     {
 27         if(in[i]==pre[t])
 28         {
 29             flag=i;
 30             break;
 31         }
 32     }
 33     if(flag==-1)
 34     return ;//结束 
 35     root=new node(in[flag]);//新建节点 
 36     t++;
 37     if(flag>l)
 38     {
 39         buildtree(l,flag-1,t,root->l);
 40         
 41     }
 42     if(flag<r)
 43     buildtree(flag+1,r,t,root->r);
 44 }
 45 void preorder(node *root)//
 46 {
 47     if(root!=NULL)
 48     {
 49         post[k++]=root->value;//输出 
 50          preorder(root->l);
 51          preorder(root->r); 
 52     }
 53     
 54 }
 55 void inorder(node *root)//
 56 {
 57     if(root!=NULL)
 58     {
 59         inorder(root->l);
 60         post[k++]=root->value;
 61         inorder(root->r);
 62      } 
 63 }
 64 void postorder(node *root)//
 65 {
 66     if(root!=NULL)
 67     {
 68         postorder(root->l);
 69         postorder(root->r);
 70         post[k++]=root->value;
 71     }
 72 }
 73 void leaves(node *root)//统计叶节点个数 
 74 {
 75     if(root)
 76     {
 77         if(!root->l&&!root->r)
 78         {
 79             //printf("%d ",root->value);
 80             cnt++;
 81         }
 82         leaves(root->l);
 83         leaves(root->r); 
 84     }
 85 }
 86 void oneleaves(node *root)//统计度为一节点个数 
 87 {
 88     if(root)
 89     {
 90         if((root->l&&!root->r)||(!root->l&&root->r))
 91         {
 92             //printf("%d ",root->value);
 93             one++;
 94         }
 95         oneleaves(root->l);
 96         oneleaves(root->r); 
 97     }
 98 }
 99 int getheight(node *root)//求树的深度 
100 {
101     int hl;
102     int hr;
103     int maxh;
104     if(root)
105     {
106         hl=getheight(root->l);
107         hr=getheight(root->r);
108         maxh=hl>hr?hl:hr;
109         return (maxh+1); 
110     }
111     else
112     return 0;
113  } 
114 void route(node *root,int path[],int len)//输出从叶节点到根节点的路径 
115 {
116     if(root==NULL)
117     return ;
118     if(root->l==NULL&&root->r==NULL)
119     {
120         cout<<root->value<<' ';
121         for(int i=len;i>=0;i--)
122         {
123             cout<<path[i]<<" ";
124             cout<<endl; 
125          } 
126     }
127     else
128     {
129         path[len++]=root->value;
130         route(root->l,path,len);
131         route(root->r,path,len);
132     }
133 }
134 void remove_tree(node *root)//用完就打扫一下啦 
135 {
136     if(root==NULL)
137     return ;
138     remove_tree(root->l);
139     remove_tree(root->r);
140     delete root;
141 }
142 int main()
143 {
144     std::ios::sync_with_stdio(false);
145     cout<<"请输入节点个数:"<<endl;
146     cin>>n;
147     cout<<"请输入pre数组:"<<endl;
148     for(int i=1;i<=n;i++)
149     cin>>pre[i];
150     cout<<"请输入in数组:"<<endl;
151     for(int j=1;j<=n;j++)
152     cin>>in[j];
153     node *root;
154     int t=1;
155     buildtree(1,n,t,root);
156     k=0;
157     postorder(root);
158     cout<<"输出后序序列:"<<endl; 
159     for(register int i=0;i<k;i++)
160     {
161         printf("%d%c",post[i],i==k-1?'\n':' ');
162     }
163     cout<<endl;
164     preorder(root);
165     cout<<"输出先序序列:"<<endl; 
166     for(register int i=1;i<=n;i++)
167     {
168         printf("%d%c",pre[i],i==k-1?'\n':' ');
169     }
170     cout<<endl;
171     inorder(root); 
172     cout<<"输出中序序列:"<<endl;
173      for(register int i=1;i<=n;i++)
174     {
175         printf("%d%c",in[i],i==k-1?'\n':' ');
176     }
177     cout<<endl;
178     cout<<"节点个数:"<<endl; 
179     cout<<k<<endl;
180     leaves(root);
181     cout<<"叶节点个数"<<endl; 
182     cout<<cnt<<endl;
183     int height=getheight(root);
184     cout<<"树的深度是:"<<endl;
185     cout<<height<<endl; 
186     oneleaves(root);
187     cout<<"度为1的节点有"<<one<<""<<endl;
188     int len=0;
189     cout<<"输出从叶节点到根节点的路径:"<<endl;
190     route(root,path,len);
191     remove_tree(root);//释放内存 
192     return 0;
193     
194 }

 

posted @ 2022-04-14 10:11  江上舟摇  阅读(40)  评论(0编辑  收藏  举报