C++ 将二叉树排序为已排序数组
我的代码有问题,请给我一点帮助 完整代码:C++ 将二叉树排序为已排序数组,c++,arrays,binary-tree,binary-search-tree,C++,Arrays,Binary Tree,Binary Search Tree,我的代码有问题,请给我一点帮助 完整代码: #include <iostream> using namespace std; int a[100],k; struct nod { int info; nod *st,*dr; } *rad,*p; void adaug(nod *&rad, int x) { if(!rad)
#include <iostream>
using namespace std;
int a[100],k;
struct nod
{
int info;
nod *st,*dr;
} *rad,*p;
void adaug(nod *&rad, int x)
{
if(!rad)
{
nod *p = new nod;
p -> info = x;
p -> st = 0;
p -> dr = 0;
rad = p;
}
else if(x < rad -> info) adaug(rad->st,x);
else adaug(rad->dr,x);
}
void SRD(nod *rad,int &k)
{
if(rad)
{
SRD(rad -> st,k);
a[k] = rad -> info;
k++;
SRD(rad -> dr,k);
}
}
int main()
{
nod *rad = NULL;
int n,x,i;
cout << "n="; cin >> n;
for(i = 1; i <= n; i++)
{
cin >> x;
adaug(rad,x);
}
SRD(rad,k);
while (a[k]){
cout << a[k] << " ";
k++;
}
cout << endl << k;
return 0;
}
#包括
使用名称空间std;
int a[100],k;
结构节点
{
国际信息;
nod*st、*dr;
}*rad,*p;
无效adaug(节点*&rad,int x)
{
如果(!rad)
{
nod*p=新节点;
p->info=x;
p->st=0;
p->dr=0;
rad=p;
}
否则如果(xinfo)adaug(rad->st,x);
else-adaug(rad->dr,x);
}
无效SRD(节点*rad、int&k)
{
中频(rad)
{
SRD(rad->st,k);
a[k]=rad->info;
k++;
SRD(rad->dr,k);
}
}
int main()
{
nod*rad=NULL;
int n,x,i;
cout>n;
对于(i=1;i>x;
adaug(rad,x);
}
SRD(rad,k);
while(a[k]){
不能像这样做
for(i = 0; i < k; i++)
cout<<a[i]<<" ";
(i=0;i
库特
i=0;
while(i<k)
{
cout<<a[i]<<" ";
i++;
}