is it so difficult?? - Programmers Heaven

Howdy, Stranger!

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

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

is it so difficult??

cuongmitscuongmits Posts: 34Member
I'm writing a program, it can creat a binary tree of flats, add more flat into database (binary tree) and delete all flat with floor entered by user. But I have problem with function Delete flat :( Please help me if you can. Thanks in advance!

This is code of my program:
[code]
#include
#include
struct TFlat //declaration of flat
{
int Id; //ID of Flat
int Floor; //info about Flat
TFlat * Left; //left pointer
TFlat * Right; //right pointer
} * Flat=NULL; //the TOP of the tree

TFlat * q;
int Id;
int Floor; //
/*--------------------------------------------------*/
/*---------Insert a Flat into database--------------*/
/*---------- based on Id --------------*/
/*- smaller Id is on the left, bigger on the right -*/
int InsFlat(TFlat **Flat, int Id, int Floor)
{
if (*Flat==NULL)
{
TFlat * NewRecord = new TFlat;
NewRecord->Left=NULL;
NewRecord->Right=NULL;
NewRecord->Id=Id;
NewRecord->Floor=Floor;
*Flat=NewRecord;
}
else
if (Id<(*Flat)->Id) InsFlat(&((*Flat)->Left),Id,Floor);
else
if (Id>(*Flat)->Id) InsFlat(&((*Flat)->Right),Id,Floor);
else cout << "You can not enter the same Id of flat
";
return 0;
}
/*---------------------------------------------------*/
/*-------------Print data about all flat--------------*/
int PrintTree(TFlat * AFlat)
{
if (AFlat!=NULL)
{
PrintTree(AFlat->Left);
PrintTree(AFlat->Right);
cout << AFlat->Id << " - " << AFlat->Floor << endl;
}
return 0;
}
/*---------------------------------------------------*/
/*THIS IS MY ALGORITHM, IS IT RIGHT FOR DEL-FUNCTION?*/
/*----------Delete a flat with its Id----------------*/
/*----------support for the next---------------------*/
TFlat * q;
void Del(TFlat **AFlat)
{
if ((*AFlat)->Right!=NULL) Del(&((*AFlat)->Right));
else
{
q->Id=(*AFlat)->Id;
q=*AFlat;
*AFlat=(*AFlat)->Left;
delete(q);
}
}
/*--------------Main of delete-function--------------*/
int DeleteFlat(int Id, TFlat ** AFlat)
{
if (*AFlat==NULL) cout << "No Flat with Id " << Id << " in database";
else
if (Id<(*AFlat)->Id) DeleteFlat(Id,&((*AFlat)->Left));
else
if (Id>(*AFlat)->Id) DeleteFlat(Id,&((*AFlat)->Right));
else
{
q=(*AFlat); //q points to deleted node
if (q->Right==NULL)
{
*AFlat=q->Left;
delete(q);
}
else
if (q->Left==NULL)
{
*AFlat=q->Right;
delete(q);
}
else
Del(&(q->Left));
}
return 0;
}
void Del(TFlat **AFlat)
{
if ((*AFlat)->Right!=NULL) Del(&((*AFlat)->Right));
else
{
q->Id=(*AFlat)->Id;
q->Floor=(*AFlat)->Floor;
q=*AFlat;
*AFlat=(*AFlat)->Left;
delete(q);
}
}
/*--------------Main of delete---------------------*/
void DeleteFlat(int Floor, TFlat ** AFlat)
{
if ((*AFlat)->Floor==Floor)
{
q->Id=(*AFlat)->Id;
q=(*AFlat);
if (q->Right==NULL)
{
*AFlat=q->Left;
delete(q);
}
else
if (q->Left==NULL)
{
*AFlat=q->Right;
delete(q);
}
else Del(&(q->Left));
DeleteFlat(Floor,&(*AFlat));
}
else
if ((*AFlat)->Left!=NULL) DeleteFlat(Floor,&((*AFlat)->Left));
else
if ((*AFlat)->Right!=NULL) DeleteFlat(Floor,&((*AFlat)->Right));
}
/*----------------------Main-----------------------*/
int main()
{
int Digit;
int choice;
while (1==1)
{
clrscr();
cout << "1. Insert Flat" << endl
<< "2. Show tree" << endl
<< "3. Delete tree" << endl
<< "4. Exit" << endl
<< "Your choice: ";
cin >> choice;
switch (choice)
{
case 1:
clrscr();
cout << "Enter ID: ";
cin >> Digit;
cout << "Floor: ";
cin >> Floor;
InsFlat(&Flat, Digit, Floor);
break;
case 2: cout << "
Tree that you have enter:
";
PrintTree(Flat);
getch();
break;
case 3: cout << "
Enter Id that you want to delete: ";
cin >> Floor;
DeleteFlat(Floor, &Flat);
break;
case 4: return 0;
}
}
return 0;
}
[/code]

Comments

  • jambeardjambeard Posts: 70Member
    You've already done it right haven't you?

    This part of your code works fine:

    [code]
    void Del(TFlat **AFlat)
    {
    if ((*AFlat)->Right!=NULL) Del(&((*AFlat)->Right));
    else
    {
    q->Id=(*AFlat)->Id;
    q=*AFlat;
    *AFlat=(*AFlat)->Left;
    delete(q);
    }
    }
    /*--------------Main of delete-function--------------*/
    int DeleteFlat(int Id, TFlat ** AFlat)
    {
    if (*AFlat==NULL) cout << "No Flat with Id " << Id << " in database";
    else
    if (Id<(*AFlat)->Id) DeleteFlat(Id,&((*AFlat)->Left));
    else
    if (Id>(*AFlat)->Id) DeleteFlat(Id,&((*AFlat)->Right));
    else
    {
    q=(*AFlat); //q points to deleted node
    if (q->Right==NULL)
    {
    *AFlat=q->Left;
    delete(q);
    }
    else
    if (q->Left==NULL)
    {
    *AFlat=q->Right;
    delete(q);
    }
    else
    Del(&(q->Left));
    }
    return 0;
    }
    [/code]

    Why is there 2 versions of delete? Were you trying something different out? I think if you comment out the other version of delete and just use this one it will work (works for me anyway). You also need to get rid of this line

    [code]
    TFlat * q;
    [/code]

    which comes just after your comment about the delete function (this one:

    [code]
    /*---------------------------------------------------*/
    /*THIS IS MY ALGORITHM, IS IT RIGHT FOR DEL-FUNCTION?*/
    /*----------Delete a flat with its Id----------------*/
    /*----------support for the next---------------------*/
    [/code]

    J
Sign In or Register to comment.