a lot of rearranging has to be done
You got that right (grin). I've rearranged it slightly to make it easier to view and think about (atleast to me).
Code:
typedef struct TreeStruc
{
struct TreeStruc *left;
struct TreeStruc *right;
double number;
}TreeCell,*TreePtr;
// function made.. talloc..!!
TreePtr talloc(void)
{
TreePtr theP;
theP = 0L;
theP = (TreePtr)malloc(sizeof(TreeCell));
if(!theP)
ErrHandler(); /* <-- normally, you'd write this */
return(theP);
}
TreePtr AddCell(TreePtr p, double number)
{
if(!p) /* create root node */
{
p = talloc();
/* check allocation of 'p' here */
p->number = number;
p->right = p->left = NULL;
}
else
{
if(number == p->number)
{
/*****
*
* Found duplicate number. Want to replace it
* How do I delete existing number?
*
*****/
}
else
{
if(number < p->number)
p->left = AddCell(p->left, number);
else
p->right = AddCell(p->right, number);
}
}
return p;
}
---
Okay, I took a look at your algorithm and in your case you don't need to do anything. You don't have to delete the existing node, because the value is in 'number' is already the same. Just discard the duplicate (ignore it and move on).
If you were really sorting a database and this happened, then you would created a hash-table that would let you distinguish between the two, and you'd add another node to hold the duplicate.
If you were really interested in deleting an entire node, then you must delete that node, but KEEP it's child (left & right) branches as well as the parent node's other branch together and resort them. Then rehang them off the parent.
Rather than walking the list to find the parent, add another field to your structure:
Code:
typedef struct TreeStruc
{
struct TreeStruc *parent;
struct TreeStruc *left;
struct TreeStruc *right;
double number;
}TreeCell,*TreePtr;
Also, it's a good idea to get into the habit of putting your link pointers as the FIRST fields in a structure. that way, for future exandability, you always know where the pointers are, even if the rest of the format of the structure changes-- it wouldn't break a program.