Complete the following coding problems. Start by coding all of them up in your group’s Google Doc, without using a Java compiler or IDE. This is intended to help you practice writing code as you will be asked to on quizzes and exams in this and future CS classes, and likely also in internship and job interviews.
Once you’ve written out code for all the problems and convinced yourself as thoroughly as possible that they will work, go back to each solution, testing and debugging with the help of a Java compiler. Unless someone in your group has something already set up locally, I recommend using an online IDE such as repl.it to get an easily-accessible Java development environment.
Some of these functions are not trivial to test - for example, you will need to create the BinaryTree
class and build a number of example trees to run your isFull
method on to make sure that it handles all possible cases.
The computation (i.e., raising to the power) can be described iteratively as multiplying by itself times. The obvious iterative algorithm for this is , assuming a single multiplication takes constant time. Implement that algorithm below:
It turns out that an (i.e., much faster!) algorithm exists for computing . It makes use of a recursive definition that leverages the following two mathematical facts in a recursive definition of exponentiation:
Come up with such a recursive definition and implement the following method to compute it. Don’t worry too much about the runtime analysis - if the approach uses more than a bit less than multiplications to compute , you’re probably on the right track.
We will use the following binary tree class for this problem:
Implement the following method:
Implement the following method. You should not assume you have access to a HashMap class - take advantage of the precondition.