Fibonacci Trees
This exercise deals with "Fibonacci trees", trees that represents the recursive call structure of the Fibonacci computation. (The Fibonacci sequence is defined as follows: F0 = 0, F1 = 1, and each subsequent number in the sequence is the sum of the previous two.)
The root of a Fibonacci tree should contain the value of the nth Fibonacci number the left subtree should be the tree representing the computation of the n-1st Fibonacci number, and the right subtree should be the tree representing the computation of the n-2nd Fibonacci number. The first few Fibonacci trees appear below.
Function Graph
fibtree(0) Tree
fibtree(1) Tree
fibtree(2) Tree
fibtree(3) Tree
fibtree(4) Tree
fibtree(5) Tree
Supply the fibTreeHelper method to go with the BinaryTree method below.
public static BinaryTree fibTree (int n) {
BinaryTree result = new BinaryTree();
result.myRoot = result.fibTreeHelper (n);
return result;
}
private TreeNode fibTreeHelper (int n) ...
Note: Primitive types like int are not objects. Java provides wrapper classes that allow primitive values to be stored as objects. In some situations, Java will automatically convert one to the other. This is called auto-boxing and auto-unboxing.