10b - TreesSlides

# We look for an ancestor who has that node in its left

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

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: hat node in its left subtree. else // find ancestor { while (curr->parent != 0 && (curr(curr->parent)(curr->parent)->left != curr) curr = curr->parent; currcurr = curr->parent; curr} We either find it, or we are at the rightmost node of the whole tree (finished). 13 What was all this for? We can now find the next node in inorder traversal from any node in the tree. Suppose we use a iterator to travel through the tree tree structure nodePtr curr; void SetIterator(nodePtr root);//find leftmost node void* Next(nodePtr root); //return next item bool More(nodePtr root); //any more nodes? Here's the function to see if there is any more of the tree to iterate through. bool More(nodePtr root) { return (curr != 0); (curr 0); } And finally there is Next, the iterator itself. Next OK. So just how efficient are these trees? A tree contains at most 1, 2, 4, 8, ... , 2n nodes at each level. Or, we need level for node, levels for Or, we need 1 level for 1 node, 2 levels for 3 nodes, nodes, 3 levels for 7 nodes, . . ., n levels...
View Full Document

## This document was uploaded on 04/07/2014.

Ask a homework question - tutors are online