Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将二叉树排序为已排序数组_C++_Arrays_Binary Tree_Binary Search Tree - Fatal编程技术网

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++;
}