Invitation to Computer Science 8th Edition

Published by Cengage Learning
ISBN 10: 1337561916
ISBN 13: 978-1-33756-191-4

Chapter 3 - Exercises - Page 145: 29

Answer

Tree diagram - Jose / \ JoAnn Lee / \ / \ Arturo John Snyder Tracy The number of comparisons in the worst case for a binary tree search algorithm in this list is 3. Searching for the name "Snyder" would take 3 comparisons.

Work Step by Step

Starting from the root node (Jose), the algorithm would compare the target item (e.g. Arturo) with the node. If the target item is less than the node, it would move to the left child node (JoAnn). If the target item is greater than the node, it would move to the right child node (Lee). The algorithm would repeat this process until it finds the target item or it becomes clear that the item is not in the tree. Tree diagram - Jose / \ JoAnn Lee / \ / \ Arturo John Snyder Tracy The number of comparisons in the worst case for a binary tree search algorithm is equal to the height of the tree. In the case of this list, the height of the tree would be log2(8) = 3, meaning that in the worst case, it would take 3 comparisons to find an item in the tree. Searching for the name "Snyder" would take 3 comparisons: Start with Jose Compare Snyder with Jose (Snyder > Jose), move to the right child node (Lee) Compare Snyder with Lee (Snyder > Lee), move to the right child node (Snyder) At this point, the target item has been found, so the algorithm would stop.
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.