2020软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 算法和数据结构初体验
其他参考文献 算法的时间复杂度和空间复杂度计算

看了一下时间和空间复杂度的相关介绍,就像在看科普论文一样,对于很多的名词和公式十分的陌生,犹如刘姥姥进了大观园,因为以前从来没有考虑过这些问题,还有学好高数也是很重要的。

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

解题思路

  1. 初始化数组来存放序列
  2. 遍历序列元素
  3. 将第k大的数据存入结果数组中
  4. 调用Arraylist的sort方法从大到小排序
  5. 输出结果数组

解题代码

package com.wufan.sort;

import java.util.ArrayList;
import java.util.Scanner;

public class KSort {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        //序列长度
        int n = sc.nextInt();

        //存放序列
        ArrayList<Integer> list = new ArrayList<>();
        //存放排序的序列
        ArrayList<Integer> lists = new ArrayList<>();

        //初始化序列
        for (int i = 0;i < n;i++){
            list.add(i,sc.nextInt());
        }
        //询问个数
        int m = sc.nextInt();

        //遍历个数
        for (int i = 0;i < m;i++){
            //初始位置第l个数
            int l = sc.nextInt();
            //到达位置第r个数
            int r = sc.nextInt();
            //询问第K大的数
            int K = sc.nextInt();

            //存入数组
            for (int j = l - 1;j < r;j++){
                lists.add(list.get(j));
            }

            //从大到小排序
            lists.sort(null);
            //输出询问的答案
            System.out.println(lists.get(lists.size() - K));

            lists.clear();
        }
    }
}

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

解题思路

  1. 先序遍历:先访问根,再访问子树的根,最后访问右子树的根。
  2. 中序遍历:先访问左子树,然后访问根,然后访问右子树。
  3. 后序遍历:先访问左子树,如果左子树还有子树,继续访问左子树,访问完所有子树再返回根。
  4. 层次遍历:每一层从左到右遍历,借助队列实现。

解题代码

package com.wufan.binary;

import java.util.LinkedList;

public class BinaryTree {
    public static void main(String[] args) {
        Node root = into();
        // 先序遍历
        System.out.print(" 先序遍历:");
        A(root);
        // 中序遍历
        System.out.print(" 中序遍历:");
        B(root);
        // 后续遍历
        System.out.print(" 后续遍历:");
        C(root);
        // 层级遍历
        System.out.print(" 层次遍历:");
        D(root);

    }

    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 层级遍历
        if(root == null) return;
        LinkedList<Node> linkedList = new LinkedList<>();
        Node current = null;
        //将根节点入队
        linkedList.offer(root);
        while (! linkedList.isEmpty()){
            //出队队头元素并访问
            current = linkedList.poll();
            System.out.print(current.data);
            //如果当前节点的左节点不为空入队
            if (current.l != null) {
                linkedList.offer(current.l);
            }
            //如果当前节点的右节点不为空,把右节点入队
            if (current.r != null){
                linkedList.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;
        }
    }
}

原文地址:https://www.cnblogs.com/songwufan/p/13893600.html