I have a BST defined as follows:
typedef struct trnode {
Item item;
struct trnode *left;
struct trnode *right;
} Trnode;
typedef struct tree {
Trnode *root;
size_t size;
} Tree;
The issue I'm having is often I want to know what the parent of a particular tree node is. Is it common at all to define a tree node which includes the parent, such as:
typedef struct trnode {
Item item;
struct trnode *parent;
struct trnode *left;
struct trnode *right;
} Trnode;
Or is including the parent something that should not be done, and if so: why not?
Update: someone has requested to see my deletion code. This is untested and was pretty difficult for me to write (I'm a beginniner in C and also just learning the BST data structure). Anyways, here it is:
Node * SeekInsertionParent(const Node *root, const Node *pnode)
{
// cmp returns -1 (less than), +1 (greater than), or 0 (equal)
int cmp = CmpNodes(pnode, root);
if (cmp == 0) return NULL; // ignore this for now
Node *child = (cmp < 0)? root->left : root->right;
if (child == NULL)
return root;
else
SeekInsertionParent(child, pnode);
}
Bool DeleteItem(Tree *ptree, const Item *pi)
{
Node *parent;
Node *node = SeekItem(pi, ptree);
if (node == NULL)
return false;
// (1) if no children, then it's a leaf, just delete it
if (!node->left && !node->right) {
free(node);
return true;
}
// (2) if it has one child, link that child to the parent then free the node
if (!node->left || !node->right) {
Node *descendant = (node->left)? node->left : node->right;
descendant->parent = parent;
if (parent->left == node)
parent->left = descendant;
else
parent->right = descendant;
free(node);
}
// (3) if it has two children, then:
// (a) attach the child same-side child to the parent node;
// (b) using the root of the attachment, find the place to insert the other-side child
else {
Node *insert_at, *other_side;
if (parent->left == node) {
node->left->parent = parent;
parent->left = node->left;
other_side = node->right;
} else {
node->right->parent = parent;
parent->right = node->right;
other_side = node->left;
}
free(node);
insert_at = SeekInsertionParent(parent, other_node);
if (insert_at->left == NULL) {
insert_at->left=node;
node->parent=insert_at;
} else {
insert_at->right=node;
node->parent=insert_at;
}
}
return true;
}