

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


1 Exam 4 and Final Exam 1
2 Complexity 1
3 Design Fast Algorithms 3
4 Parallel Computing 3
Exam 4 will be available in Blackboard on April 24 (Friday) after class and due at 2PM
allowed to discuss with anybody. Every question in the exam will be related to recursion. You are encouraged to submit your answers on-line.
We will have a lecture on April 24 (discussion of shortest and longest paths). The lecture on May 1 is canceled so you have more time preparing for final exams. The last question in exam 4 asks you whether you prefer to take the final exam in class (2 hours 3:20-5:20PM on May 6) or at home (10 hours 8AM-6PM on May 6).
If you want to take the final exam before May 6, please talk to the instructor.
In a binary search tree, how many nodes do we need to check in order to determine whether a value is currently stored in the tree (the BTree search function)? Let’s ask a different question first. How many nodes does a tree have if the tree has n levels? In the first level (the root), there is only one node. The next level has at most two nodes. The third level can have up to four nodes.
level node 1 1 2 2 3 4 4 8 ... ... n 2 n−^1 total 2 n^ − 1
If a binary search tree is complete and balanced , searching a tree of 2 n^ − 1 nodes requires visiting at most n levels. We can eliminate half of the remaining nodes in each step. To put it in another way, searching a tree of n nodes requires visiting only ≈ lg n nodes; here lg is the logarithm function using 2 as the base, namely lg n = log 2 n. We usually write log using 10 as the base, log n = log 10 n.
If we use a binary search tree to sort a sequence of numbers, what is the complexity? What do we mean by using a tree to sort? We can insert these numbers one by one and then use in-order to print the numbers. If the tree is balanced after inserting i − 1 numbers, inserting the ith^ number has to visit only ≈ lg(i − 1) nodes. Therefore, the complexity is
∑^ n
i=
lg(i − 1). (1)
Let’s simplify it by finding an upper bound. We know lg x < lg y if 1 < x < y. Thus,
∑^ n
i=
lg(i − 1) <
∑^ n
i=
lg(n) < n lg n. (2)
This is much better than n^2 using a linked list. This assumes that the tree is always bal- anced. In reality, we may not be that lucky. Let’s consider the situation when the sequence is already sorted in descending order. When we insert the numbers, only the left subtrees are used and inserting the ith^ number has to visit i − 1 nodes. As a result, it takes ≈ n^2 to sort the numbers. Similarly, if the numbers are sorted in ascending order, only the right subtress are used and the complexity is also n^2. For the average cases, we assume the numbers are not ordered and the tree is “somewhat” balanced, the complexity is n lg n. The detailed analysis is beyond the scope of ECE 264.