将二叉树排序为排序数组

Sorting Binary tree into a sorted array

我的代码有问题,请帮忙?:D

完整代码:

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

SRD是左、根、右交叉,adaugare是插入函数。所以如果我选择 cout << rad -> info << " ";在 SRD 函数中,它可以工作,但在数组中不起作用:(。 我认为问题出在 SRD 函数中,所以请帮忙?(当它应该在二叉树中打印 2 3 4 6 7 8 时它只打印 7 ,如下所示: 6(根) 3 8 2 4 7 9

像这样...

for(i = 0; i < k; i++)
 cout<<a[i]<<" ";

或者像这样

i=0;
while(i<k)
{
    cout<<a[i]<<" ";
    i++;
}

您的代码中的问题在于

while(a[k])