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:
#include <iostream.h>
#include <conio.h>
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\n";
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 << "\nTree that you have enter: \n";
PrintTree(Flat);
getch();
break;
case 3: cout << "\nEnter Id that you want to delete: ";
cin >> Floor;
DeleteFlat(Floor, &Flat);
break;
case 4: return 0;
}
}
return 0;
}