*This preview shows
page 1. Sign up
to
view the full content.*

**Unformatted text preview: **.
So what happens if we do keep a parent pointer,
as well as left and right child pointers, in each
node. The first node in inorder traversal is the left-most
leftnode. struct node
{
void* data; curr = root;
while (curr->left != 0)
(currcurr = curr->left;
curr- nodePtr parent, left, right;
parent left right;
}; All the insertion and deletion code would now be
rewritten to update the parent pointer as well as
parent
the child pointers.
But traversal is now very simple. if (curr->right != 0) // into right subtree
(curr{
curr = curr->right;
currwhile (curr->left != 0)
(currcurr = curr->left;
curr} If it
If it has no right subtree and no parent, we're
at the root of a tree with only a left subtree so
we're finished.
else if (curr->parent == 0)
(currcurr = 0; Now let's suppose that at some time during the
traversal we are at node curr.
curr
If it has a right child, we find the left-most node in
leftthe right subtree. Otherwise the current node is the rightmost node
of subtree.
We look for an ancestor who has t...

View
Full
Document