Binary tree

Help me plz, why can't I enter and print out database of tree??

[code]
#include
#include
struct TFlat //declaration of flat
{
int Id; //
TFlat * Left; //left pointer
TFlat * Right; //right pointer
} * Flat=NULL; //the TOP of the tree
/*--------------------------------------------------*/
/*---------Insert a ID into database--------------*/
int InsFlat(TFlat * Flat, int Id)
{
if (Flat==NULL)
{
TFlat * NewRecord = new TFlat;
NewRecord->Left=NULL;
NewRecord->Right=NULL;
NewRecord->Id=Id;
Flat=NewRecord;
}
else
if (IdId) InsFlat(Flat->Left,Id);
else
InsFlat(Flat->Right,Id);
return 0;
}
/*--------------Print data about flat----------------*/
int PrintTree(TFlat * AFlat)
{
if (AFlat!=NULL)
{
cout << AFlat->Id << " ";
PrintTree(AFlat->Left);
PrintTree(AFlat->Right);
}
return 0;
}
/*----------------------Main-----------------------*/
int main()
{
int Digit;
clrscr();
cout << "Enter number: ";
cin >> Digit;
while (Digit!=0)
{
InsFlat(Flat, Digit);
cout << "Enter number: ";
cin >> Digit;
}
cout << "
Tree:
";
PrintTree(Flat);
getch();
return 0;
}
[/code]

Comments

  • : Help me plz, why can't I enter and print out database of tree??
    :

    [blue]Since you are going to be modifying the pointer you pass into your insert function, you cannot just pass the pointer itself; you must pass the address of the pointer (a pointer to a poiinter):[/blue]

    [code]
    #include
    #include
    struct TFlat //declaration of flat
    {
    int Id; //
    TFlat * Left; //left pointer
    TFlat * Right; //right pointer
    } * Flat=NULL; //the TOP of the tree
    /*--------------------------------------------------*/
    /*---------Insert a ID into database--------------*/
    int InsFlat(TFlat [blue]*[/blue]* Flat, int Id)
    {
    if ([blue]*[/blue]Flat==NULL)
    {
    TFlat * NewRecord = new TFlat;
    NewRecord->Left=NULL;
    NewRecord->Right=NULL;
    NewRecord->Id=Id;
    [blue]*[/blue]Flat=NewRecord;
    }
    else
    if (Id<[blue](*[/blue]Flat[blue])[/blue]->Id) InsFlat([blue]&((*[/blue]Flat[blue])[/blue]->Left[blue])[/blue],Id);
    else
    InsFlat([blue]&((*[/blue]Flat[blue])[/blue]->Right[blue])[/blue],Id);
    return 0;
    }
    /*--------------Print data about flat----------------*/
    int PrintTree(TFlat * AFlat)
    {
    if (AFlat!=NULL)
    {
    cout << AFlat->Id << " ";
    PrintTree(AFlat->Left);
    PrintTree(AFlat->Right);
    }
    return 0;
    }
    /*----------------------Main-----------------------*/
    int main()
    {
    int Digit;
    clrscr();
    cout << "Enter number: ";
    cin >> Digit;
    while (Digit!=0)
    {
    InsFlat([blue]&[/blue]Flat, Digit);
    cout << "Enter number: ";
    cin >> Digit;
    }
    cout << "
    Tree:
    ";
    PrintTree(Flat);
    getch();
    return 0;
    }
    [/code]
  • : : Help me plz, why can't I enter and print out database of tree??
    : :
    :
    : [blue]Since you are going to be modifying the pointer you pass into your insert function, you cannot just pass the pointer itself; you must pass the address of the pointer (a pointer to a poiinter):[/blue]
    :
    : [code]
    : #include
    : #include
    : struct TFlat //declaration of flat
    : {
    : int Id; //
    : TFlat * Left; //left pointer
    : TFlat * Right; //right pointer
    : } * Flat=NULL; //the TOP of the tree
    : /*--------------------------------------------------*/
    : /*---------Insert a ID into database--------------*/
    : int InsFlat(TFlat [blue]*[/blue]* Flat, int Id)
    : {
    : if ([blue]*[/blue]Flat==NULL)
    : {
    : TFlat * NewRecord = new TFlat;
    : NewRecord->Left=NULL;
    : NewRecord->Right=NULL;
    : NewRecord->Id=Id;
    : [blue]*[/blue]Flat=NewRecord;
    : }
    : else
    : if (Id<[blue](*[/blue]Flat[blue])[/blue]->Id) InsFlat([blue]&((*[/blue]Flat[blue])[/blue]->Left[blue])[/blue],Id);
    : else
    : InsFlat([blue]&((*[/blue]Flat[blue])[/blue]->Right[blue])[/blue],Id);
    : return 0;
    : }
    : /*--------------Print data about flat----------------*/
    : int PrintTree(TFlat * AFlat)
    : {
    : if (AFlat!=NULL)
    : {
    : cout << AFlat->Id << " ";
    : PrintTree(AFlat->Left);
    : PrintTree(AFlat->Right);
    : }
    : return 0;
    : }
    : /*----------------------Main-----------------------*/
    : int main()
    : {
    : int Digit;
    : clrscr();
    : cout << "Enter number: ";
    : cin >> Digit;
    : while (Digit!=0)
    : {
    : InsFlat([blue]&[/blue]Flat, Digit);
    : cout << "Enter number: ";
    : cin >> Digit;
    : }
    : cout << "
    Tree:
    ";
    : PrintTree(Flat);
    : getch();
    : return 0;
    : }
    : [/code]
    :
    Thks a lot :)
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories