Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The constructor below is very similar to the one you used for the last assignmen

ID: 3827508 • Letter: T

Question

The constructor below is very similar to the one you used for the last assignment. What Is its time complexity in terms of N, where N is the number of lines in the InputStream (the word file)? public Doublets(InputStream in) { try { lexicon = new TreeSet(); Scanner s = new Scanner(new BufferedReader(new InputStreamReader in))) while (s.hasNext()) { String str = s.next(); boolean added = lexicon.add(str); s. nextLine(); } in.close() ; } catch (java.io.IOException e) { System err.println("Error reading from InputStream."); System exit(1); } } A. log N B. N C. N log N D. N^2

Explanation / Answer

Answer is B. As we are adding the elements in treeset. Each addition in treeset has complexity O(1). So N additions will have complexity O(N).

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote