struct node {
int key;
int info;
struct node *left, *right;
};
struct node *head, *dummy_node;
int treesearch(int v) {
struct node *current_node = head->right;
dummy_node->key = v;
while (v != current_node->key)
current_node = (v < current_node->key) ? current_node->left
: current_node->right;
return current_node->info;
}
treeinsert(int v, int info) {
struct node *parent, *current_node;
/* keep track of parent so that the parent can be linked
to the search key when the appropriate insertion point
is found */
parent = head;
current_node = head->right;
while (current_node != dummy_node) {
parent = current_node;
current_node = (v < current_node->key) ? current_node->left
: current_node->right;
}
/* allocate a node for the search key, store the key and
its information, and make the dummy node be the left
and right child of the new node */
current_node = (struct node *) malloc(sizeof(struct node));
current_node->key = v;
current_node->info = info;
current_node->left = dummy_node;
current_node->right = dummy_node;
/* make the parent point to the new node via the appropriate
link */
if (v < parent->key)
parent->left = current_node;
else
parent->right = current_node;
}
treedelete(int v)
{
struct node *parent, *current_node;
struct node *second_parent, *low_node;
struct node *t;
/* make the dummy node's key v so that even if v is not in the
tree the search for v's node will terminate */
dummy_node->key = v;
/* find the node containing v */
parent = head;
current_node = head->right;
while (v != current_node->key) {
parent = current_node;
current_node = (v < current_node->key) ? current_node->left
: current_node->right;
}
/* find the node that will replace v's node */
t = current_node;
/* if v does not have a right child, the left child will
replace v */
if (t->right == dummy_node)
current_node = current_node->left;
/* if v does not have a left child, the right child will
replace v */
if (t->left == dummy_node)
current_node = current_node->right;
/* if v has a right child with no left child, replace v with
the right child. Additionally, v's left subtree becomes
the right child's left subtree. */
else if (t->right->left == dummy_node) {
current_node = current_node->right;
current_node->left = t->left;
}
/* otherwise find the node with the next highest key and
replace v's node with this new "low node" */
else {
second_parent = current_node->right;
low_node = second_parent->left;
/* the low_node will be the leftmost child in the right
subtree */
while (low_node->left != dummy_node) {
second_parent = low_node;
low_node = low_node->left;
}
/* the low_node replaces v's node so we make low_node
point to v's left and right subtrees. Before we
do this, we must promote low_node's right subtree
so that it becomes the left subtree of low_node's
parent (we know that low_node does not have a left
subtree, for if it did, low_node would not be the
lowest node) */
current_node = low_node;
second_parent->left = low_node->right;
current_node->left = t->left;
current_node->right = t->right;
}
/* free v and reset its parent link only if t is not the
dummy node (i.e., there was a node corresponding to v
in the tree */
if (t != dummy_node) {
free(t);
if (v < parent->key)
parent->left = current_node;
else
parent->right = current_node;
}
}
/sunshine/homes/bvz/courses/302/src/bintree.c contains the source code that's just been presented. /sunshine/homes/bvz/courses/302/bin/bintree contains a compiled verison of this code. bintree will not prompt you for input but it will keep reading lines until you type ctrl-D. The three action codes you can enter are:
i 10 100 i 2 200 i -3 400 s 2 s 10 d -3 s -3will produce the output:
key = 2 info = 200 key = 10 info = 100 key = -3 info = -1