「java数据实验」java实验结果与分析
本篇文章给大家谈谈java数据实验,以及java实验结果与分析对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
求数据结构(JAVA版)实验树和二叉树题目答案
/**
* @param args
之前在大学的时候写的一个二叉树算法,运行应该没有问题,就看适不适合你的项目了 */
public static void main(String[] args) {
BiTree e = new BiTree(5);
BiTree g = new BiTree(7);
BiTree h = new BiTree(8);
BiTree l = new BiTree(12);
BiTree m = new BiTree(13);
BiTree n = new BiTree(14);
BiTree k = new BiTree(11, n, null);
BiTree j = new BiTree(10, l, m);
BiTree i = new BiTree(9, j, k);
BiTree d = new BiTree(4, null, g);
BiTree f = new BiTree(6, h, i);
BiTree b = new BiTree(2, d, e);
BiTree c = new BiTree(3, f, null);
BiTree tree = new BiTree(1, b, c);
System.out.println("递归前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非递归前序遍历二叉树结果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("递归中序遍历二叉树的结果为:");
tree.inOrder(tree);
System.out.println();
System.out.println("非递归中序遍历二叉树的结果为:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("递归后序遍历二叉树的结果为:");
tree.postOrder(tree);
System.out.println();
System.out.println("非递归后序遍历二叉树的结果为:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("递归求二叉树中所有结点的和为:"+getSumByRecursion(tree));
System.out.println("非递归求二叉树中所有结点的和为:"+getSumByNoRecursion(tree));
System.out.println("二叉树中,每个节点所在的层数为:");
for (int p = 1; p = 14; p++)
System.out.println(p + "所在的层为:" + tree.level(p));
System.out.println("二叉树的高度为:" + height(tree));
System.out.println("二叉树中节点总数为:" + nodes(tree));
System.out.println("二叉树中叶子节点总数为:" + leaf(tree));
System.out.println("二叉树中父节点总数为:" + fatherNodes(tree));
System.out.println("二叉树中只拥有一个孩子的父节点数:" + oneChildFather(tree));
System.out.println("二叉树中只拥有左孩子的父节点总数:" + leftChildFather(tree));
System.out.println("二叉树中只拥有右孩子的父节点总数:" + rightChildFather(tree));
System.out.println("二叉树中同时拥有两个孩子的父节点个数为:" + doubleChildFather(tree));
System.out.println("--------------------------------------");
tree.exChange();
System.out.println("交换每个节点的左右孩子节点后......");
System.out.println("递归前序遍历二叉树结果: ");
tree.preOrder(tree);
System.out.println();
System.out.println("非递归前序遍历二叉树结果: ");
tree.iterativePreOrder(tree);
System.out.println();
System.out.println("递归中序遍历二叉树的结果为:");
tree.inOrder(tree);
System.out.println();
System.out.println("非递归中序遍历二叉树的结果为:");
tree.iterativeInOrder(tree);
System.out.println();
System.out.println("递归后序遍历二叉树的结果为:");
tree.postOrder(tree);
System.out.println();
System.out.println("非递归后序遍历二叉树的结果为:");
tree.iterativePostOrder(tree);
System.out.println();
System.out.println("层次遍历二叉树结果: ");
tree.LayerOrder(tree);
System.out.println();
System.out.println("递归求二叉树中所有结点的和为:"+getSumByRecursion(tree));
System.out.println("非递归求二叉树中所有结点的和为:"+getSumByNoRecursion(tree));
System.out.println("二叉树中,每个节点所在的层数为:");
for (int p = 1; p = 14; p++)
System.out.println(p + "所在的层为:" + tree.level(p));
System.out.println("二叉树的高度为:" + height(tree));
System.out.println("二叉树中节点总数为:" + nodes(tree));
System.out.println("二叉树中叶子节点总数为:" + leaf(tree));
System.out.println("二叉树中父节点总数为:" + fatherNodes(tree));
System.out.println("二叉树中只拥有一个孩子的父节点数:" + oneChildFather(tree));
System.out.println("二叉树中只拥有左孩子的父节点总数:" + leftChildFather(tree));
System.out.println("二叉树中只拥有右孩子的父节点总数:" + rightChildFather(tree));
System.out.println("二叉树中同时拥有两个孩子的父节点个数为:" + doubleChildFather(tree));
}
}
JAVA实验---数据类型转换
public class TypeConvert {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String inputString = console.nextLine().trim();
String numRegex = "(\\-|\\+)?[0-9]+(\\.[0-9]+)?";
if(inputString.matches(numRegex)){ // 输入的是数字
String intRegex = "(\\-)?[0-9]+";
if(inputString.charAt(0)=='+'){
inputString = inputString.substring(1);
}
System.out.println(intRegex);
if(inputString.matches(intRegex)){
long input = Long.parseLong(inputString);
System.out.println("byte:"+(byte)input);
System.out.println("short:"+(short)input);
System.out.println("int:"+(int)input);
System.out.println("long:"+input);
System.out.println("char:"+(char)input);
}else{
double input = Double.parseDouble(inputString);
System.out.println("short:"+(short)input);
System.out.println("int:"+(int)input);
System.out.println("long:"+(long)input);
System.out.println("float:"+(float)input);
System.out.println("double:"+input);
}
}else{
System.out.println("输入错误");
}
}
}
急..用JAVA语言实现数据结构实验!
发了我大半天时间专门帮你写的,注释没写仔细,有什么不明白的或者有什么小bebug就给我留言。
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
public class Test {
//I/O读取文件
public String getFile(String path) {
StringBuffer context = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
String temp = br.readLine();
while (temp!=null) {
context.append(temp+"\n");
temp = br.readLine();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return context.toString().toLowerCase();
}
//分割内容方法
public List mySplit(String context) {
String[] words = {};
List all = new ArrayList();
//按标点符号,分割内容的正则表达式
String regex = "\\W";//[():,./'\"\n\r\f\\s-]
words = context.split(regex);
for(int i=0; iwords.length; i++) {
if(!words[i].equals(""))//把空格去掉
all.add(words[i]);
}
return all;
}
//统计全部单词及其个数
public Hashtable contWords(List all) {
//用于保存全部的单词及其个数
Hashtable allTable = new Hashtable();
for(int i=0; iall.size(); i++) {
//两个临时的变量,一个键一个值
String temp = all.get(i).toString();
int count = 0;
for(int j=0; jall.size(); j++) {
if(temp.equalsIgnoreCase(all.get(j).toString())) {
count++;
}
}
allTable.put(temp, count);
}
return allTable;
}
//找出个数最多的那五个单词
public Hashtable findMax5(Hashtable allTable) {
//用于保存结果的Hashtable
Hashtable result = new Hashtable();
Object[] keyToValue = allTable.entrySet().toArray();
Object[] values = allTable.values().toArray();
int[] v = new int[5];
for(int i=0; ivalues.length; i++) {
int value = Integer.parseInt(values[i].toString());
int min = findMin(v);
if(valuemin) {
for(int j=0; jv.length; j++) {
if(v[j]==min) {
v[j] = value;
break;
}
}
}
}
//把v里面的无素从大到小排序一下
for(int i=0; iv.length; i++) {
for(int j=i+1; jv.length; j++) {
if(v[i]v[j]) {
int temp = v[i];
v[i] = v[j];
v[j] = temp;
}
}
}
for(int i=0; iv.length; i++) {
// System.out.println(v[i]);
for(int j=0; jkeyToValue.length; j++) {
String ktv = keyToValue[j].toString();
int tv = Integer.parseInt(ktv.substring(ktv.indexOf("=")+1));
if(v[i]==tv) {
//保证只取五个频率最高的单词
if(result.size()=5) break;
String key = ktv.substring(0, ktv.indexOf("="));
result.put(key, v[i]);
}
}
}
return result;
}
//简单的查找数组中最小的那个数
public int findMin(int[] v) {
for(int i=0; iv.length-1; i++) {
if(v[i]v[i+1]) {
int temp = v[i];
v[i] = v[i+1];
v[i+1] = temp;
}
}
return v[v.length-1];
}
//打印出结果
public static void printResult(Hashtable result) {
System.out.println("排前五的单词情况如下:");
Enumeration e = result.keys();
Iterator it = result.values().iterator();
while (e.hasMoreElements()) {
System.out.println(e.nextElement() + " 的个数为: " + it.next());
}
}
//main方法
public static void main(String[] args) {
Test test = new Test();
String context = test.getFile("c:/test3.txt");
List all = test.mySplit(context);
Hashtable allTable = test.contWords(all);
Hashtable result = test.findMax5(allTable);
// System.out.println(result);
Test.printResult(result);
}
}
java统计每组实验数据中的最大值,最小值,平均值。
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Test {
private static String readDataFromConsole(String prompt) {
Scanner scanner = new Scanner(System.in);
System.out.print(prompt);
return scanner.nextLine();
}
private static void handArray(String input) {
if( null == input || "".equals(input)){
System.out.println("输入参数不能为空");
return;
}
DecimalFormat df=new DecimalFormat("#.00");
String[] tempArray = input.split(" ");
Double min = null;
Double max = null;
BigDecimal total = new BigDecimal(0.0);
for(String temp : tempArray){
if(min == null){
min = Double.parseDouble(temp);
}else{
if( min Double.parseDouble(temp)){
min = Double.parseDouble(temp);
}
}
if(max == null){
max = Double.parseDouble(temp);
}else{
if( max Double.parseDouble(temp)){
max = Double.parseDouble(temp);
}
}
total = total.add(new BigDecimal(Double.parseDouble(temp))) ;
}
BigDecimal ave = total.divide(new BigDecimal(tempArray.length), 2);
System.out.println(df.format(min) +" "+df.format(max)+" "+df.format(ave.doubleValue()));
}
public static void main(String[] args) {
Integer repeat = new Integer(1);
while ( repeat 0 ){
String str = readDataFromConsole("Please input running time:");
repeat = Integer.parseInt(str);
if( repeat = 0 || repeat 100){
System.out.println("Input running time number must between 0 and 100 ");
}else{
String[] inputArrays = new String[repeat*2];
for(int i=0; i repeat*2; i++ ){
Scanner scanner = new Scanner(System.in);
inputArrays[i] = scanner.nextLine();
}
System.out.println("输出");
for(String input: inputArrays){
if(input.split(" ").length1){
handArray(input);
}
}
repeat=0;
}
}
}
}
输入输出:
Please input running time:2
3
3 6 9
5
1.9 1.8 3.44 5.6 6.7
输出
3.00 9.00 6.00
1.80 6.70 3.89
关于java数据实验和java实验结果与分析的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。
发布于:2022-11-23,除非注明,否则均为
原创文章,转载请注明出处。