|
|
COT 5410-01 Fall 2004 Algorithms Chris Lacher Notes 8: Set < Binary Tree > |
{
Iterator I;
I.Initialize(*this);
return I;
}
{
// start at root
N = B.Root();
// then slide left to lowest lchild
while (N.HasLeftChild())
++N;
}
{
if (!N.Valid())
{
return *this;
}
// now we have a valid navigator
if (N.HasRightChild())
// slide down the left subtree of right child
{
N++;
while (N.HasLeftChild())
++N;
}
else
// back up to first ancestor not already visited
// as long as we are parent's right child, then parent has been visited
{
int NwasRightChild;
do
{
NwasRightChild = N.IsRightChild();
--N;
}
while (NwasRightChild);
}
return *this;
}
for (I = B.Begin(); I != B.End(); ++I){}
has exactly 2n iterations, where n is the number of nodes in
the tree.Includes(t)
{
Navigator N = B.Root();
while (N.Valid())
{
if (t.key < N.key)
++N;
else if (N.key < t.key)
N++;
else // t.key == N.key
return N;
}
return N; // null navigator
}
Runtime: O(depth)