These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references.
Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
--question #1
require: BST.java Node.java Question1.java
compile : javac Question1.java
run : java Question1
--question #2
require: Heap.java Question2.java
compile : javac Question2.java
run : java Question2
import java.awt.HeadlessException;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author
*/
public class Question1 extends JFrame {
private JScrollPane jScrollPane1;
private JTextArea textArea;
/**
* constructor
* @param mes string to be showed
* @throws HeadlessException
*/
public Question1(String mes) throws HeadlessException {
super("Question 1");
textArea = new JTextArea(mes);
jScrollPane1 = new JScrollPane(textArea);
add(jScrollPane1);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
/**
* main function
* @param args
*/
public static void main(String[] args) {
Object[] options = {"Read from file",
"Generate numbers"};
int n = JOptionPane.showOptionDialog(null,
"What action do you want to take?",
"Question 1",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
BST bst ;
if (n == 0) {
bst = fromFile();
} else {
bst = generate();
}
int k = 0;
try {
String num = JOptionPane.showInputDialog(null,
"Ente k value: ");
k = Integer.parseInt(num);
} catch (Exception e) {
k = 0;
}
String mes = "";
mes += "Height of the binary search tree "
+ bst.height() + System.lineSeparator();
ArrayList<Integer> al = bst.k_smallest(k);
mes += k + "th smallest element: " + al.toString()
+ System.lineSeparator();
Object[] options1 = {"Ascending",
"Descending"};
n = JOptionPane.showOptionDialog(null,
"Which order of input do you want to show?",
"Question 1",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options1,
options1[0]);
ArrayList<Integer> ll;
if (n == 0) {
ll = bst.ascending();
mes += "Ascending order: ";
} else {
ll = bst.descending();
mes += "Ascending order: ";
This is only a preview of the solution.
Please use the purchase button to see the entire solution.