Page 240, Exercise 10 (a)
Note: InsertNode() is called EnterNode in this implementation.
void EnterNode(TreePointer *node, int num)
{/* Enter an element into the tree */
/* pass in the address of the pointer to the current node */
TreePointer ptr;
ptr = *node;
if (!ptr) {
ptr = (TreePointer)malloc(sizeof(struct TreeNode));
ptr->data = num;
ptr->count = 0;
ptr->LeftChild = ptr->RightChild = NULL;
*node = ptr;
}
else if (num == ptr->data) ptr->count++;
else if (num < ptr->data)
EnterNode(&ptr->LeftChild, num);
else
EnterNode(&ptr->RightChild, num);
}
|