2020软件工程作业04
这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/2018SE |
---|---|
这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406 |
这个作业的目标 | <排序算法和二叉树的排序和遍历学习> |
其他参考文献 | <博客园和CSDN> |
学号 | <20189736> |
第一题:寻找数组中第K大是数 考察算法:排序算法
解题思路:
首先需要对指定索引范围内的数组进行降序排序,进而输出指定索引位置元素。先用java数组类中写好的升序方法排序,然后倒序赋给新的数组从而实现降序排序。
解题代码如下:
package test;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] ages){
Scanner sc=new Scanner(System.in);
int n,m;
n=sc.nextInt();
int[] array=new int[n+1];
//输入给定的序列 注意:序列的顺序是不可以变得。
for(int i=1;i<=n;i++){
array[i]=sc.nextInt();
}
//询问个数
m=sc.nextInt();
int[] l=new int[m+1];
int[] r=new int[m+1];
int[] k=new int[m+1];
for(int i=0;i<m;i++){
l[i]=sc.nextInt();
r[i]=sc.nextInt();
k[i]=sc.nextInt();
}
//分别输出要求
//第一 先截取l到r这一段距离
int[][] point=new int[m][n+1];
int sum=1;
for(int i=0;i<m;i++){
for(int j=l[i];j<=r[i];j++){
point[i][sum++]=array[j];
}
//把拿出来这一段进行排序
Arrays.sort(point[i]);
String st=Arrays.toString(point[i]);
System.out.println(point[i][(point[i].length-k[i])]); //排序是从小到大的,所以要从数组后面开始数K。
sum=1;
}
}
}
运行结果
第二题:二叉树的先、中、后 序遍历与层级遍历
解题思路:
先序遍历:首先访问根节点,然后先序遍历其右子树,再先序遍历其左子树;
中序遍历:首先中序遍历根节点的左子树麻烦后访问根节点,最后中序遍历其右子树;
后序遍历:首先后序遍历根节点的左子树,然后后序遍历根节点的右子树,左后访问根节点。
先找后序遍历的最后一位,这个结点便是头节点,再在中序遍历结点中找到该结点,这个结点左边是左指树,右边便是右结点。最后将这个分成两部分,不断递归。
解题代码如下:
package text4;
import java.util.LinkedList;
public class ercahshu {
public static void main(String[] args) {
/*
作业要求:叉树的先、中、后 序遍历与层级遍历
自己实现四个方法,main方法中调用,将结果打印到控制台
*/
/* 二叉树的结构
A
/ \
T 6
/
D
/ \
N 5
/ \ /
B 4 1
\
9
*/
Node root = into();
// 先序遍历
System.out.println("先序遍历");
A(root);
// 中序遍历
System.out.println("\n");
System.out.println("中序遍历");
B(root);
// 后续遍历
System.out.println("\n");
System.out.println("后续遍历");
C(root);
// 层级遍历
System.out.println("\n");
System.out.println("层级遍历");
D(root);
}
private static void A(Node node) {
// TODO 先序遍历
System.out.print(node.data + "\t");
if(node.l != null){
A(node.l);
}
if(node.r != null){
A(node.r);
}
}
private static void B(Node node) {
// TODO 中序遍历
if(node.l != null){
B(node.l);
}
System.out.print(node.data + "\t");
if(node.r != null){
B(node.r);
}
}
private static void C(Node node) {
// TODO 后续遍历
if (node.l !=null){
C (node.l);
}
if(node.r != null){
C(node.r);
}
System.out.print(node.data + "\t");
}
private static void D(Node node) {
// TODO 层级遍历
if(node == null) {
return ;
}
LinkedList<Node> queue = new LinkedList<>();
Node current = null;
queue.offer(node);
while(!queue.isEmpty()) {
current = queue.poll();//出队队头元素并访问
System.out.print(current.data+ "\t");
if(current.l != null) { //如果当前节点的左节点不为空入队
queue.offer(current.l);
}
if(current.r != null) {//如果当前节点的右节点不为空,把右节点入队
queue.offer(current.r);
}
}
}
// 构建一颗树,返回根节点
private static Node into(){
Node root = new Node("A");
Node node1 = new Node("T");
Node node2 = new Node("D");
Node node3 = new Node("N");
Node node4 = new Node("B");
Node node5 = new Node("6");
Node node6 = new Node("5");
Node node7 = new Node("4");
Node node8 = new Node("9");
Node node9 = new Node("1");
root.l = node1;
node1.l = node2;
node2.l = node3;
node2.r = node6;
node3.r = node7;
node7.r = node8;
node6.l = node9;
node3.l = node4;
root.r = node5;
return root;
}
// 节点
static class Node{
// 数据
Object data;
// 左孩子
Node l;
// 右孩子
Node r;
public Node(){}
public Node(Object data) {
this.data = data;
this.l = null;
this.r = null;
}
public Node(Object data, Node l, Node r) {
this.data = data;
this.l = l;
this.r = r;
}
}
}
运行结果