2020软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE/
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 学习算法
学号 20189751


第一题 寻找数组中第K大的数

解题思路:1.输入长度,定义数组

                      2.再输入数组元素到数组中

                      3.再来一次输入询问个数

                      4.for循环

                      5.冒泡排序求k

package myhomework;

import java.util.Arrays;
import java.util.Scanner;

public class test1 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//数组长度
        int[] a = new int[n];
        for(int i=0;i<n;i++)
        {
            a[i] = in.nextInt();
        }        
        int m = in.nextInt();//整数
        int[] dd = new int[m];
        for(int i=0;i<m;i++)
        {
            int l = in.nextInt();
            int r = in.nextInt();
            int k = in.nextInt();
            int[] b = new int[r-l+1];//创建数组
            for(int j=0;j<r-l+1;j++)
            {
                b[j]=a[j+l-1];
            }
            Arrays.sort(b);//数组按数字升序排序
            dd[i]=b[r-l+1-k];
        }
        for(int i=0;i<m;i++)
        {
            System.out.println(dd[i]);
        }
    }
}

 

第二题 二叉树的先、中、后 序遍历与层级遍历

按照先中后的循序,从二叉树的根节点依次输出节点数据,按照从左向右的方向去访问。
层次遍历:按照树的层次自上而下的遍历二叉树。

package homework;

import java.util.LinkedList;
public class test2 {
    public static void main(String[] args) {
        /*
            作业要求:叉树的先、中、后 序遍历与层级遍历
            自己实现四个方法,main方法中调用,将结果打印到控制台
         */
        /*  二叉树的结构
                     A
                    / \
                   T   6
                  /
                 D
               /   \
              N     5
             / \    /
            B   4  1
                 \
                  9
         */
        Node root = into();
        // 先序遍历;
        A(root);
        System.out.println();
        System.out.println("先序遍历") ;
        // 中序遍历
        B(root);
        System.out.println();
        System.out.println("中序遍历");
        // 后续遍历
        C(root);
        System.out.println();
        System.out.println("后序遍历");
        // 层级遍历
        D(root);
        System.out.println();
        System.out.println("层级遍历");
    }

    private static void A(Node root) {
        // TODO 先序遍历
        if (root == null) return;
        System.out.print(root.data +" ");
        A(root.l);
        A(root.r);
    }
    private static void B(Node  root) {
        // TODO 中序遍历
        if (root == null) return;
        B(root.l);
        System.out.print(root.data+" ");
        B(root.r);
    }
    private static void C(Node  root) {
        // TODO 后续遍历
        if (root == null) return;
        C(root.l);
        C(root.r);
        System.out.print(root.data +" ");
    }
    private static void D(Node root) {
        // TODO 层级遍历
        LinkedList<Node> list= new LinkedList<>();
        list.add(root);
        while(! list.isEmpty()) {
            Node ww=list.pop();
            if(ww.l !=null) {
                list.offer(ww.l);
            }
            if(ww.r !=null) {
                list.offer(ww.r);
            }
            System.out.print(ww.data +" ");
        }
    }

    // 构建一颗树,返回根节点
    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 @ 2020-10-28 23:26  JMDFC  阅读(106)  评论(0编辑  收藏  举报