SlideShare a Scribd company logo
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

PDF
TO DO add your implementation and JavaDocs.public class ThreeT.pdf
DOCX
Table.java Huffman code frequency tableimport java.io.;im.docx
PDF
Add to BST.java a method height() that computes the height of the tr.pdf
DOCX
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
PDF
A perfect left-sided binary tree is a binary tree where every intern.pdf
DOCX
Required to augment the authors Binary Search Tree (BST) code to .docx
DOCX
Once you have all the structures working as intended- it is time to co.docx
PDF
It is the main program that implement the min(), max(), floor(), cei.pdf
TO DO add your implementation and JavaDocs.public class ThreeT.pdf
Table.java Huffman code frequency tableimport java.io.;im.docx
Add to BST.java a method height() that computes the height of the tr.pdf
package edu-ser222-m03_02- --- - A binary search tree based impleme.docx
A perfect left-sided binary tree is a binary tree where every intern.pdf
Required to augment the authors Binary Search Tree (BST) code to .docx
Once you have all the structures working as intended- it is time to co.docx
It is the main program that implement the min(), max(), floor(), cei.pdf

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

PDF
JavaIm not sure how to implement this code can someone help me .pdf
RTF
RTF
PDF
package DataStructures; public class HelloWorld AnyType extends.pdf
PDF
6. Generics. Collections. Streams
PDF
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
PDF
Given the following codepackage data1;import java.util.;p.pdf
DOCX
PathOfMostResistance
DOCX
hw4.DS_Store__MACOSXhw4._.DS_Storehw4HW4Test.javahw4.docx
PDF
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
PDF
java set1 program.pdf
PDF
How to do the main method for this programBinaryNode.javapublic.pdf
PDF
I have a .java program that I need to modify so that it1) reads i.pdf
PDF
For the code below complete the preOrder() method so that it perform.pdf
PDF
Create an implementation of a binary tree using the recursive appr.pdf
PPTX
unit 4 for trees data structure notes it is
PDF
Please read the comment ins codeExpressionTree.java-------------.pdf
PDF
Can some one redo this code without the try-catch and an alternative.pdf
PDF
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
DOCX
Write a program that displays an AVL tree along with its balance fact.docx
JavaIm not sure how to implement this code can someone help me .pdf
package DataStructures; public class HelloWorld AnyType extends.pdf
6. Generics. Collections. Streams
Modify HuffmanTree.java and HuffmanNode.java to allow the user to se.pdf
Given the following codepackage data1;import java.util.;p.pdf
PathOfMostResistance
hw4.DS_Store__MACOSXhw4._.DS_Storehw4HW4Test.javahw4.docx
( PLEASE SHOW HOW TO IMPLEMENT THE DELETION FUNCTION )SAMPLE OUTPU.pdf
java set1 program.pdf
How to do the main method for this programBinaryNode.javapublic.pdf
I have a .java program that I need to modify so that it1) reads i.pdf
For the code below complete the preOrder() method so that it perform.pdf
Create an implementation of a binary tree using the recursive appr.pdf
unit 4 for trees data structure notes it is
Please read the comment ins codeExpressionTree.java-------------.pdf
Can some one redo this code without the try-catch and an alternative.pdf
(1)Objective Binary Search Tree traversal (2 points)Use traversal.pdf
Write a program that displays an AVL tree along with its balance fact.docx

More from akaluza07 (9)

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

Recently uploaded (20)

PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Cell Types and Its function , kingdom of life
PDF
RMMM.pdf make it easy to upload and study
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Institutional Correction lecture only . . .
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Computing-Curriculum for Schools in Ghana
Anesthesia in Laparoscopic Surgery in India
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Supply Chain Operations Speaking Notes -ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
GDM (1) (1).pptx small presentation for students
Cell Types and Its function , kingdom of life
RMMM.pdf make it easy to upload and study
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
Module 4: Burden of Disease Tutorial Slides S2 2025
Final Presentation General Medicine 03-08-2024.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Institutional Correction lecture only . . .
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Computing-Curriculum for Schools in Ghana

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); } }