The two exercises are independent. Be careful to upload .java files (and not .class files).
The tests that are provided are not perfect. A validated program is not necessarily correct.
You have access to the Java documentation.
Upload your file Personne.java (using the French version).
You have access to the documentation of LinkedList and HashSet.
Test your program using Test1.java. Then upload your file UnionIntersection.java (using the French version).
The goal of this exercise is to build and study binary trees whose nodes contain rational numbers and whose properties will be explained in the following. As in lecture/TD 5, the empty tree is implemented with null.
We are given two rational numbers al = pl/ql and ar = pr/qr and an integer limit > ql, qr. The Stern-Brocot tree associated to (al, ar, limit) is built as follows:
In SternBrocot.java, complete the method static SternBrocot build(pl, ql, pr, qr, limit) that returns the Stern-Brocot tree associated to (pl/ql, pr/qr, limit). We assume that pl/ql < pr/qr, that these two fractions are irreducible (numerator and denominator are relatively primed), and that pl, ql, pr, qr are nonnegative.
Figure 1. The Stern-Brocot tree associated to (0/1, 1/1, 7) obtained by calling build(0,1,1,1,7). The values 0/1 and 1/1 and the dotted lines illustrate that all rational numbers in that tree belong to the interval ]0/1,1/1[. To better visualize the tree, click here.
Test your program using Test21.java. Then upload your file SternBrocot.java (using the French version).
Complete the method static int size(SternBrocot t) that returns the number of nodes in tree t.
Test your program using Test22.java. Then upload your file SternBrocot.java (using the French version).
Stern-Brocot trees have, by construction, a structure of binary search tree, with all nodes within the interval ]pl/ql, pr/qr[ (we admit this property). To check it, complete the method static boolean isBST(SternBrocot t, int pl, int ql, int pr, int qr) that returns true if and only if
Test your program using Test23.java. Then upload your file SternBrocot.java (using the French version).
We seek to enumerate all nodes of a given Stern-Brocot tree in increasing order, as a LinkedList<String> l whose elements are strings of the form "p/q".
Complete the method treeToList. Use an auxiliary method static void treeToList(SternBrocot t, LinkedList<String> l) if needed.
Test your program using Test24.java. Then upload your file SternBrocot.java (using the French version).
We consider the Stern-Brocot tree associated to (0/1, 1/1, limit) for a given integer limit. The Stern-Brocot tree being a binary search tree, we can search for a given rational number p/q efficiently, descending either to the left or to the right subtree, and stopping when the node contains p/q (success) or when the tree is empty (failure). One can represent the path in the tree corresponding to such a search as a sequence of characters L (when we descend to the left) or R (when we descend to the right). With the tree in Figure 1, we associate to 1/3 the sequence L (success), to 2/7 the sequence LLR (success) and to 5/14 the sequence LLRR (failure).
Complete the method String path(SternBrocot t, int p, int q) that returns the string corresponding to the search of p/q. The string ends with ! if p/q belongs to t and with ? otherwise. With the tree t in Figure 1, path(t, 2, 7) returns the string "LLR!" and path(t, 5, 14) returns the string "LLRR?" (since 2/7 < 5/14 < 1/3).
Test your program using Test25.java. Then upload your file SternBrocot.java (using the French version).
Complete the method fromPath that performs the reverse operation: given a string of characters L or R, it returns the corresponding rational number in a Stern-Brocot tree associated to (0/1, 1/1, limit) for a value of limit large enough for that path to reach a rational number.
Warning. Your program must not build any tree.
The rational number must be returned as an array of two integers. For instance, fromPath("RLL") returns the array {4,7}. The syntax to create a two-element array containing a and b is new int[]{a,b}.
Test your program using Test26.java Then upload your file SternBrocot.java (using the French version).
Complete the method lowApprox(t, x) that takes a Stern-Brocot tree t and a real number x as input and returns the greatest element in t that is lower than or equal to x, if it exists, and null otherwise. If the result is not null we return a rational number as an array of two integers {numerator, denominator}.
Test your program using Test27.java. Then upload your file SternBrocot.java (using the French version).
In a similar way, implement a method static int[] upApprox(SternBrocot t, double x) that returns the smallest element of t that is greater than or equal to x, if its exists, and null otherwise.
Le x be a real. We say that p/q
is a good rational approximation of x
if p, q are relatively primed and
Upload your file SternBrocot.java (using the French version).