J
j_depp_99
My program is required to build a binary search tree using integers
inputted from a file. It also searches for items and counts the nodes
accessed in each search. Also it needs to calculate the average number
of comparisons per search. I am not sure where to insert the counts
because it seems I am getting the wrong count. Here is my retrieve
function:
Thanks in advance.
inputted from a file. It also searches for items and counts the nodes
accessed in each search. Also it needs to calculate the average number
of comparisons per search. I am not sure where to insert the counts
because it seems I am getting the wrong count. Here is my retrieve
function:
Code:
int Retrieve(TreeNode* tree,
ItemType& item, bool& found, int& num)
// Recursively searches tree for item.
// Post: If there is an element someItem whose key matches item's,
// found is true and item is set to a copy of someItem;
// otherwise found is false and item is unchanged.
{
//int counter=0;
if (tree == NULL)
{
found = false; // item is not found.
return num=0;
}
else if (item < tree->info)
{
Retrieve(tree->left, item, found, num); // Search left subtree.
num=num+1;
}
else if (item > tree->info)
{
Retrieve(tree->right, item, found, num);// Search right subtree.
num = num+1;
}
else
{
item = tree->info; // item is found.
found = true;
}
}
Thanks in advance.