This is java Run empirical studies to compute the average and standard deviation
ID: 3598598 • Letter: T
Question
This is java
Run empirical studies to compute the average and standard deviation of the average length of a path to a random node in a BST built by insertion of N random keys into an initially empty tree, for N from 100 to 12,800 (with N doubling from 100, 200, 400 ...)
Details:
- I recommend developing your own BST implementation in MyBST.java, building on the code in the BST class provided in algs4.jar.
- For each tree size, do 1000 trial trees. That means: create an empty BST object, generate N keys, and put them into the BST.
- Once you have a filled BST, the average path length on this tree (mt) is the sum of all node depths divided by the number of nodes, plus 1. You'll need to create a function in your BST implementation to compute and return this.
- I'm asking you to compute the average and standard deviations of those mt values.
- Print those values to a table, captured in your README file
Explanation / Answer
package com.one;
public class X00000000 extends Polynomial {
public static void main(String args[]) throws Exception {
Polynomial p = new X00000000(" X^5"), q = new X00000000("X^2 - X + 1");
Utility.run(p, q);
}
public X00000000(String s) {
// parse string character by character
double coef = 0; // coefficient of term
int deg = 0; // degree of term
String[] terms = s.split(" ");
for (int i = 0; i < terms.length; i++) {
String term = terms[i];
// System.out.println("term:"+term);
String prevTerm = "";
if (i != 0)
prevTerm = terms[i - 1];
if (term.startsWith("X")) {
if (term.length() == 3 && term.contains("^")) {
coef = 1;
deg = Integer.parseInt(term.substring(2));
} else if (term.length() == 1) {
deg = 1;
if (prevTerm.equals("-"))
coef = -1;
else
coef = 1;
}
} else if (term.startsWith("-") || term.startsWith("+"))
continue;
else if (term.length() == 1 && Character.isDigit(term.charAt(0))) {
coef = Integer.parseInt(term);
if (prevTerm.equals("-"))
coef *= -1;
deg = 0;
}
Term T = new Term(coef, deg);
if (data.isEmpty()) {
data.addFirst(T);
} else {
data.addLast(T);
}
}
}
public X00000000() {
super();
}
public Polynomial add(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial subtract(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial multiply(Polynomial p) {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial divide(Polynomial p) throws Exception {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
public Polynomial remainder(Polynomial p) throws Exception {
Polynomial ans = new X00000000();
// complete this code
return ans;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.