Read the following recursive implementation for Fibonacci numbers and answer the below questions.
UPDATE:You should show all the complete execution tree, including duplicate calls.
int f(int n)
{
if (n <= 1)
return n;
return f(n - 1) + f(n - 2);
}
Answer the following questions regarding the following data structures:
i. 9 ii. 5 iii. 4 iv. 4 v. 10
/ \ / /|\ / \ / \
7 8 0 3 4 5 1 -2 3 12
/ / \ / / \
6 -5 2 1 4
class BinaryTreenode<T> {
private T data;
private BinaryTreenode<T> leftChild;
private BinaryTreenode<T> rightChild;
public BinaryTreenode(T item) {
data = item;
leftChild = null;
rightChild = null;
}
public T getData() {
return data;
}
public BinaryTreenode<T> getLeft() {
return leftChild;
}
public BinaryTreenode<T> getRight() {
return rightChild;
}
...
}
Implement the following countLeaves function that determines the number of leaves of the binary tree. Assume that first call to this function will be with root node.
For full credit, your code:
class BinaryTree<T> {
BinaryTreenode<T> root;
public int countLeaves() { return countLeaves(root); }
private int countLeaves(BinaryTreenode<T> node) {
...
}
}
34, 32, 35, 37, 31, 36, 33, 38
9, 23, 12, 6, 10, 5, 14, 3, 1, 4
Answer the following questions about a binary tree with N nodes. Recall that a binary tree is height-balanced if, for every node, the height of the left or right subtree is at most one more than the height of the other subtree. Equivalently, the height of the tree is as small as possible.
Hint: You will need the following mathematical functions: floor and ceiling. Try out different example cases before finializing your answer.
class BinaryTree<T> {
BinaryTreenode<T> root;
public int height(BinaryTreenode<T> node) {
//Assume this method is implemented for you
}
boolean isBalanced(BinaryTreenode<T> node) {
...
//COMPLETE THIS METHOD
}
}
Complete the implementation of isBalanced method that determines if the binary tree is height-balanced or not.
boolean isBalanced(BinaryTreenode<T> node) {