朱嗣仪  

| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/2018SE |
| ---- | ---- | ---- |
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406 |
|这个作业的目标 | 区间K大数查询和二叉树的先、中、后 序遍历与层级遍历 |
| 学号 | 20189743 |
第一题
1.题目名称:用排序算法找数组中第K大的数。
2.解题思路:给定一个原始数组,在数组中选定第1到第r个数,再进行排序找出第K大的数。
3.解题代码:
import java.util.Arrays;
import java.util.Scanner;

public class main {

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    //定义一个数组num用于存放数列
    int[] num = new int[n];
    sc.nextLine();

    //输入数列
    for (int i = 0; i < n; i++) {
        num[i] = sc.nextInt();
    }

    //输入查询的次数
    int m = sc.nextInt();

    //定义存放查询指令的二维数组arr
    int[][] arr = new int[m][3];

    //输入查询的指令
    for (int i = 0; i < m; i++) {
        arr[i][0] = sc.nextInt();
        arr[i][1] = sc.nextInt();
        arr[i][2] = sc.nextInt();
    }


    for (int i = 0; i < m; i++) {
        //定义暂存数组,区间就是二维数组里,每一行的第2个数减去第1个数+1
        int[] temp = new int[arr[i][1] - arr[i][0] + 1];
        //复制区间里的数字,注意数列是从1开始,而数组下标是0开始,所以,查询条件左右都需-1
        //temp = Arrays.copyOfRange(num, arr[i][0] - 1, arr[i][1]原作者此处忘了-1);
        temp = Arrays.copyOfRange(num, arr[i][0] - 1, arr[i][1]-1);
        Arrays.sort(temp);

        System.out.println(temp[arr[i][1] - arr[i][0] + 1 - arr[i][2]]);
    }
}

}
运行结果

第二题
1.题目名称:二叉树的先、中、后 序遍历与层级遍历
2.解题思路:先序遍历:首先访问根结点然后遍历左子树,最后遍历右子树。在遍历左、右子树时,仍然先访问根结点,然后遍历左子树,最后遍历右子树,如果二叉树为空则返回。
中序遍历:首先遍历左子树,然后访问根结点,最后遍历右子树。若二叉树为空则结束返回
后序遍历:首先遍历左子树,然后遍历右子树,最后访问根结点,在遍历左、右子树时,仍然先遍历左子树,然后遍历右子树,最后遍历根结点
层级遍历:首先建立一个循环队列,先将二叉树头结点入队列,然后出队列,访问该结点,如果它有左子树,则将左子树的根结点入队:如果它有右子树,则将右子树的根结点入队。然后出队列,对出队结点访问,如此反复,直到队列为空为止
3.解题代码:
import java.util.LinkedList;

public class Main {

public static void main(String[] args) {
/*
作业要求:叉树的先、中、后 序遍历与层级遍历
自己实现四个方法,main方法中调用,将结果打印到控制台
/
/
二叉树的结构
A
/
T 6
/
D
/
N 5
/ \ /
B 4 1

9
*/
Node root = into();
// 先序遍历
System.out.print("先序:");
A(root);
System.out.println();
// 中序遍历
System.out.print("中序:");
B(root);
System.out.println();
// 后续遍历
System.out.print("后序:");
C(root);
System.out.println();
// 层级遍历
System.out.print("层级:");
D(root);

}

private static void A(Node root) {
// TODO 先序遍历
if (root != null) {
System.out.print(root.data);
A(root.l);
A(root.r);
}
}

private static void B(Node root) {
// TODO 中序遍历
if (root != null) {
B(root.l);
System.out.print(root.data);
B(root.r);
}
}

private static void C(Node root) {
// TODO 后序遍历
if (root != null) {
C(root.l);
C(root.r);
System.out.print(root.data);
}
}

private static void D(Node root) {
// TODO 层级遍历
if (root != null) {
LinkedList linkedList = new LinkedList();
//先将根节点入队
linkedList.offer(root);
Node node = null;
while (!linkedList.isEmpty()) {
node = (Node) linkedList.pop();
System.out.print(node.data);
if (node.l != null) {
//将出队结点的左子树根入队
linkedList.offer(node.l);
}
if (node.r != null) {
//将出队结点的右子树根入队
linkedList.offer(node.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;
}

}
结果

posted on 2020-10-29 15:54  朱嗣仪  阅读(76)  评论(0编辑  收藏  举报