(Towards the Reading grade) In our textbook (link at top of Moodle page), read 6.3-6.8.
The authors do a couple things differently than we do.
First, they use while loops to traverse a tree path. It's a valuable technique to know about, but you'll notice that we present recursion in lecture.
Why? Recursion is more general, from algorithms that only need to visit one path up to those that need to visit the entire tree.
And recursive code is simpler, especially when you use you use the NULL dummy node and approaches that avoid parent pointers.
So I'll require you to use recursion whenever possible, recognizing that things like iterators still require loops.
Second, instead of static functions, we implement our algorithms in methods that have access to this
because that more closely follows
the object-oriented paradigm used in practice.
They also present general BSTs which allow duplicates while we'll focus on using trees to implement the Set interface (so no duplicates).
To receive full credit, complete the self-quizzes as you go. (You can re-take any you don't get correct the first time; you are graded on your participation.) The reading is a zybooks "assignment" called "Reading for HW4".
(20 points) Calculate the exact average-case time complexity of the binary search algorithm shown in Figure 2.2.1 of our zybook text Only consider successful searches (where the item we are looking for is actually in the array) and assume all elements are equally likely to be the one we are searching for.
Hint: For each i, count how many of the array elements will be found after binary search examines exactly i array elements. (For example, the middle element is the only one to be found after examining one array element, and there are two elements that can be found after examining two array elements.) Then sum over all and divide to find the average. For the generalization, you'll use a summation. You need to convince me somehow that your summation is correct, perhaps using a table or words. Once you have done that, then you may find the following formula useful:
After you have done that, you'll get a messy expression with lots of terms in it. Take the limit as n gets large so some terms go to zero. You'll end up with a much simpler expression that should be clearly between 1 and log(n), like we said in class.
(Hint: that section of the textbook gives an answer for the worst-case runtime of binary search, so you know that your answer should be smaller than that. But how much smaller is the interesting question we are trying to answer here.