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: 30

Answer

See expalantion

Work Step by Step

Tree Structure for Binary Search on a List of 16 Elements 1. Root Level (0 comparisons): o Midpoint: index=7\text{index} = 7index=7 (value at index 7) 2. First Level (1 comparison): Left subtree (elements 0-6): Midpoint is at index 333 o Right subtree (elements 8-15): Midpoint is at index 111111 3. Second Level (2 comparisons): o Left subtree (0-3): Midpoint is at index 111 o Right subtree (4-6): Midpoint is at index 555 o Right subtree (8-11): Midpoint is at index 999 o Left subtree (12-15): Midpoint is at index 131313 4. Third Level (3 comparisons): o Left subtree (0-1): Midpoint is at index 000 o Right subtree (2-3): Midpoint is at index 222 o Right subtree (4-5): Midpoint is at index 444 o Right subtree (6): Only one element o Right subtree (8-9): Midpoint is at index 888 o Right subtree (10-11): Midpoint is at index 101010 o Right subtree (12-13): Midpoint is at index 121212 o Right subtree (14-15): Midpoint is at index 141414 5. Fourth Level (4 comparisons): o Each leaf node will represent a single element in the array (0, 1, 2, 3, etc.) Summary of Comparisons • The depth of the tree gives the number of comparisons in the worst case. • For a list of nnn elements, the maximum depth (or height) of a binary search tree is ⌈log⁡2(n)⌉\lceil \log_2(n) \rceil⌈log2(n)⌉. • For n=16n = 16n=16: ⌈log⁡2(16)⌉=4\lceil \log_2(16) \rceil = 4⌈log2(16)⌉=4
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.