SlideShare a Scribd company logo
1 of 4
Download to read offline
This is to test a balanced tree. I need help testing an unbalanced tree the same way
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.*;
public class Test {
public static final String INPUT = System.getProperty("user.dir") + "/input/";
public static final String OUTPUT = System.getProperty("user.dir") + "/output/";
public static void main(String[] args) throws FileNotFoundException {
TreeMap map = new TreeMap();
long startTime;
long totalTime = 0;
for(int i = 1; i <= 77;i++) {
Scanner scnr = new Scanner(new File("c:/Users/tvazq/IdeaProjects/hw2/resources/" + i +
".okpuncs"));
while(scnr.hasNext()) {
String word = scnr.next();
startTime = System.nanoTime();
if(!map.containsKey(word))map.put(word, 0);
map.put(word, 1 + map.get(word));
totalTime += (System.nanoTime() - startTime);
}
}
PrintWriter out = new PrintWriter("c:/Users/tvazq/IdeaProjects/hw2/part2" +
"frequencies.txt");
startTime = System.nanoTime();
Set allWords = map.keySet();
totalTime += (System.nanoTime() - startTime);
for(String word: allWords) {
startTime = System.nanoTime();
int frequency = map.get(word);
totalTime += (System.nanoTime() - startTime);
out.printf("%st%dn", word, frequency);
out.flush();
}
out.close();
System.out.printf("%.3f ms", totalTime/1e6);
}
}
UnbalancedTree.java
package part2;
import java.util.ArrayList;
import java.util.List;
class BinaryNode {
E element;
BinaryNode left;
BinaryNode right;
BinaryNode(E element) {
this.element = element;
}
}
class OrderedKeyValue implements Comparable {
String key;
int value;
OrderedKeyValue(String key, int value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(OrderedKeyValue other) {
return this.key.compareToIgnoreCase(other.key);
}
}
class UnbalancedTreeMap {
BinaryNode root;
public UnbalancedTreeMap() {
root = null;
}
public int get(String key) {
BinaryNode current = root;
while (current != null) {
int comparison = key.compareToIgnoreCase(current.element.key);
if (comparison < 0) {
current = current.left;
} else if (comparison > 0) {
current = current.right;
} else {
return current.element.value;
}
}
return 0;
}
public int put(String key, int value) {
BinaryNode parent = null;
BinaryNode current = root;
int comparison = 0;
while (current != null) {
comparison = key.compareToIgnoreCase(current.element.key);
if (comparison < 0) {
parent = current;
current = current.left;
} else if (comparison > 0) {
parent = current;
current = current.right;
} else {
int oldValue = current.element.value;
current.element.value = value;
return oldValue;
}
}
OrderedKeyValue newElement = new OrderedKeyValue(key, value);
if (parent == null) {
root = new BinaryNode<>(newElement);
} else {
if (comparison < 0) {
parent.left = new BinaryNode<>(newElement);
} else {
parent.right = new BinaryNode<>(newElement);
}
}
return 0;
}
public String[] keySet() {
List keys = new ArrayList<>();
inOrderTraversal(root, keys);
return keys.toArray(new String[0]);
}
private void inOrderTraversal(BinaryNode node, List keys) {
if (node == null) {
return;
}
inOrderTraversal(node.left, keys);
keys.add(node.element.key);
inOrderTraversal(node.right, keys);
}
}

More Related Content

Similar to This is to test a balanced tree. I need help testing an unbalanced t.pdf

There is something wrong with my program-- (once I do a for view all t.pdf
There is something wrong with my program-- (once I do a for view all t.pdfThere is something wrong with my program-- (once I do a for view all t.pdf
There is something wrong with my program-- (once I do a for view all t.pdfaashienterprisesuk
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfshaktisinhgandhinaga
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. StreamsDEVTYPE
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdffantasiatheoutofthef
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfamazing2001
 
Answer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfAnswer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfakanshanawal
 
Scala in practice
Scala in practiceScala in practice
Scala in practicepatforna
 
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdfHow do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdfpnaran46
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on AndroidTomáš Kypta
 
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
How to fix this error-   Exception in thread -main- q- Exit java-lang-.pdfHow to fix this error-   Exception in thread -main- q- Exit java-lang-.pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdfaarokyaaqua
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldBTI360
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodecamp Romania
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdfAKVIGFOEU
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxwhitneyleman54422
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfsauravmanwanicp
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfsiennatimbok52331
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfkostikjaylonshaewe47
 

Similar to This is to test a balanced tree. I need help testing an unbalanced t.pdf (20)

There is something wrong with my program-- (once I do a for view all t.pdf
There is something wrong with my program-- (once I do a for view all t.pdfThere is something wrong with my program-- (once I do a for view all t.pdf
There is something wrong with my program-- (once I do a for view all t.pdf
 
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdfimport java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
import java.util.Scanner;class BinaryNode{     BinaryNode left.pdf
 
6. Generics. Collections. Streams
6. Generics. Collections. Streams6. Generics. Collections. Streams
6. Generics. Collections. Streams
 
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdfJAVA OOP project; desperately need help asap im begging.Been stuck.pdf
JAVA OOP project; desperately need help asap im begging.Been stuck.pdf
 
package singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdfpackage singlylinkedlist; public class Node { public String valu.pdf
package singlylinkedlist; public class Node { public String valu.pdf
 
Answer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdfAnswer this question for quality assurance. Include a final applicat.pdf
Answer this question for quality assurance. Include a final applicat.pdf
 
Scala in practice
Scala in practiceScala in practice
Scala in practice
 
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdfHow do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
How do I fix this error - Exception in thread -main- java-lang-NullPoi.pdf
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Reactive programming on Android
Reactive programming on AndroidReactive programming on Android
Reactive programming on Android
 
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
How to fix this error-   Exception in thread -main- q- Exit java-lang-.pdfHow to fix this error-   Exception in thread -main- q- Exit java-lang-.pdf
How to fix this error- Exception in thread -main- q- Exit java-lang-.pdf
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
fix the error - class Node{ int data- Node next-.pdf
fix the error -   class Node{           int data-           Node next-.pdffix the error -   class Node{           int data-           Node next-.pdf
fix the error - class Node{ int data- Node next-.pdf
 
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docxsrcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
srcArtifact.javasrcArtifact.javaclassArtifactextendsCave{pub.docx
 
I need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdfI need help implementing a Stack with this java programming assignme.pdf
I need help implementing a Stack with this java programming assignme.pdf
 
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdfUsing NetBeansImplement a queue named QueueLL using a Linked List .pdf
Using NetBeansImplement a queue named QueueLL using a Linked List .pdf
 
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdfImplement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
Implement a queue using a linkedlist (java)SolutionLinkedQueue.pdf
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 

More from akaluza07

Suppose a division of Washington Instruments Incorporated that sells.pdf
Suppose a division of Washington Instruments Incorporated that sells.pdfSuppose a division of Washington Instruments Incorporated that sells.pdf
Suppose a division of Washington Instruments Incorporated that sells.pdfakaluza07
 
Use the ER Diagram that you created in your Homework Assignment ER .pdf
Use the ER Diagram that you created in your Homework Assignment ER .pdfUse the ER Diagram that you created in your Homework Assignment ER .pdf
Use the ER Diagram that you created in your Homework Assignment ER .pdfakaluza07
 
Use the ER Diagram that you created in your Homework Assignment ER.pdf
Use the ER Diagram that you created in your Homework Assignment  ER.pdfUse the ER Diagram that you created in your Homework Assignment  ER.pdf
Use the ER Diagram that you created in your Homework Assignment ER.pdfakaluza07
 
This includes the following � Setup MongoDB in the cloud � Gen.pdf
This includes the following � Setup MongoDB in the cloud � Gen.pdfThis includes the following � Setup MongoDB in the cloud � Gen.pdf
This includes the following � Setup MongoDB in the cloud � Gen.pdfakaluza07
 
The Xerox Alto42 Imagine the value of cornering the technological ma.pdf
The Xerox Alto42 Imagine the value of cornering the technological ma.pdfThe Xerox Alto42 Imagine the value of cornering the technological ma.pdf
The Xerox Alto42 Imagine the value of cornering the technological ma.pdfakaluza07
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfakaluza07
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfakaluza07
 
The first assignment is about assessing the feasibility of the proje.pdf
The first assignment is about assessing the feasibility of the proje.pdfThe first assignment is about assessing the feasibility of the proje.pdf
The first assignment is about assessing the feasibility of the proje.pdfakaluza07
 
Starter code provided below answer should be in C code please.Star.pdf
Starter code provided below answer should be in C code please.Star.pdfStarter code provided below answer should be in C code please.Star.pdf
Starter code provided below answer should be in C code please.Star.pdfakaluza07
 

More from akaluza07 (9)

Suppose a division of Washington Instruments Incorporated that sells.pdf
Suppose a division of Washington Instruments Incorporated that sells.pdfSuppose a division of Washington Instruments Incorporated that sells.pdf
Suppose a division of Washington Instruments Incorporated that sells.pdf
 
Use the ER Diagram that you created in your Homework Assignment ER .pdf
Use the ER Diagram that you created in your Homework Assignment ER .pdfUse the ER Diagram that you created in your Homework Assignment ER .pdf
Use the ER Diagram that you created in your Homework Assignment ER .pdf
 
Use the ER Diagram that you created in your Homework Assignment ER.pdf
Use the ER Diagram that you created in your Homework Assignment  ER.pdfUse the ER Diagram that you created in your Homework Assignment  ER.pdf
Use the ER Diagram that you created in your Homework Assignment ER.pdf
 
This includes the following � Setup MongoDB in the cloud � Gen.pdf
This includes the following � Setup MongoDB in the cloud � Gen.pdfThis includes the following � Setup MongoDB in the cloud � Gen.pdf
This includes the following � Setup MongoDB in the cloud � Gen.pdf
 
The Xerox Alto42 Imagine the value of cornering the technological ma.pdf
The Xerox Alto42 Imagine the value of cornering the technological ma.pdfThe Xerox Alto42 Imagine the value of cornering the technological ma.pdf
The Xerox Alto42 Imagine the value of cornering the technological ma.pdf
 
Submit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdfSubmit1) Java Files2) Doc file with the following contents.pdf
Submit1) Java Files2) Doc file with the following contents.pdf
 
The java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdfThe java code works, I just need it to display the results as in t.pdf
The java code works, I just need it to display the results as in t.pdf
 
The first assignment is about assessing the feasibility of the proje.pdf
The first assignment is about assessing the feasibility of the proje.pdfThe first assignment is about assessing the feasibility of the proje.pdf
The first assignment is about assessing the feasibility of the proje.pdf
 
Starter code provided below answer should be in C code please.Star.pdf
Starter code provided below answer should be in C code please.Star.pdfStarter code provided below answer should be in C code please.Star.pdf
Starter code provided below answer should be in C code please.Star.pdf
 

Recently uploaded

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024Janet Corral
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 

Recently uploaded (20)

Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
General AI for Medical Educators April 2024
General AI for Medical Educators April 2024General AI for Medical Educators April 2024
General AI for Medical Educators April 2024
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 

This is to test a balanced tree. I need help testing an unbalanced t.pdf

  • 1. This is to test a balanced tree. I need help testing an unbalanced tree the same way import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.*; public class Test { public static final String INPUT = System.getProperty("user.dir") + "/input/"; public static final String OUTPUT = System.getProperty("user.dir") + "/output/"; public static void main(String[] args) throws FileNotFoundException { TreeMap map = new TreeMap(); long startTime; long totalTime = 0; for(int i = 1; i <= 77;i++) { Scanner scnr = new Scanner(new File("c:/Users/tvazq/IdeaProjects/hw2/resources/" + i + ".okpuncs")); while(scnr.hasNext()) { String word = scnr.next(); startTime = System.nanoTime(); if(!map.containsKey(word))map.put(word, 0); map.put(word, 1 + map.get(word)); totalTime += (System.nanoTime() - startTime); } } PrintWriter out = new PrintWriter("c:/Users/tvazq/IdeaProjects/hw2/part2" + "frequencies.txt"); startTime = System.nanoTime(); Set allWords = map.keySet(); totalTime += (System.nanoTime() - startTime); for(String word: allWords) { startTime = System.nanoTime(); int frequency = map.get(word); totalTime += (System.nanoTime() - startTime); out.printf("%st%dn", word, frequency);
  • 2. out.flush(); } out.close(); System.out.printf("%.3f ms", totalTime/1e6); } } UnbalancedTree.java package part2; import java.util.ArrayList; import java.util.List; class BinaryNode { E element; BinaryNode left; BinaryNode right; BinaryNode(E element) { this.element = element; } } class OrderedKeyValue implements Comparable { String key; int value; OrderedKeyValue(String key, int value) { this.key = key; this.value = value; } @Override public int compareTo(OrderedKeyValue other) { return this.key.compareToIgnoreCase(other.key); } }
  • 3. class UnbalancedTreeMap { BinaryNode root; public UnbalancedTreeMap() { root = null; } public int get(String key) { BinaryNode current = root; while (current != null) { int comparison = key.compareToIgnoreCase(current.element.key); if (comparison < 0) { current = current.left; } else if (comparison > 0) { current = current.right; } else { return current.element.value; } } return 0; } public int put(String key, int value) { BinaryNode parent = null; BinaryNode current = root; int comparison = 0; while (current != null) { comparison = key.compareToIgnoreCase(current.element.key); if (comparison < 0) { parent = current; current = current.left; } else if (comparison > 0) { parent = current; current = current.right; } else {
  • 4. int oldValue = current.element.value; current.element.value = value; return oldValue; } } OrderedKeyValue newElement = new OrderedKeyValue(key, value); if (parent == null) { root = new BinaryNode<>(newElement); } else { if (comparison < 0) { parent.left = new BinaryNode<>(newElement); } else { parent.right = new BinaryNode<>(newElement); } } return 0; } public String[] keySet() { List keys = new ArrayList<>(); inOrderTraversal(root, keys); return keys.toArray(new String[0]); } private void inOrderTraversal(BinaryNode node, List keys) { if (node == null) { return; } inOrderTraversal(node.left, keys); keys.add(node.element.key); inOrderTraversal(node.right, keys); } }