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^2Explanation / 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).
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.