C and C++

Moderators: None (Apply to moderate this forum)
Number of threads: 28691
Number of posts: 94711

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
is it so difficult?? Posted by cuongmits on 2 Apr 2006 at 2:51 PM
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;
}

Report
Re: is it so difficult?? Posted by jambeard on 3 Apr 2006 at 12:30 PM
You've already done it right haven't you?

This part of your code works fine:

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


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

TFlat * q;


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

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


J



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.