2020软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业的要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 算法
其他参考文献 https://www.csdn.net
学号 <20189624>

第一题:寻找数组中第K大是数 考察算法:排序算法

解题思路:
利用快排的思想,只要不停遍历,直到找到分界点,即该分界点的右边的数都比该分界点大;该分界点左边的数都比该分界点小。而且刚好该分界点即为第k大的数。

解题代码块

package suanfa.demo;


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

public class Quick {
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();  //序列长度
        int[] arr = new int[n];

        for (int i =0; i<n; i++){
            arr[i] = scanner.nextInt(); // 接受给定序列
        }
        int m = scanner.nextInt(); 
        ArrayList<Integer> arrayList = new ArrayList();
        for (int i = 0; i < m; i++) {

            int l = scanner.nextInt();
            int r = scanner.nextInt();
            int k = scanner.nextInt();

            //序列元素索引从1开始,  l 到 r 中的 第 k 大小的数输出 :1 5 2  / 2 3 2
            int[] arr2 = new int[r-l+1];
            for(int z=0; z< r-l+1; z++){
                arr2[z] = arr[l-1+z];
            }
            
            quickShort(arr2,0,arr2.length-1);
            arrayList.add(arr2[r-l-k+1]);//第K大的数输出
        }
        for (Integer integer : arrayList) {
            System.out.println(integer);
        }

    }
    // 快速排序
    public static void quickShort(int[] arr, int left, int right){
        int l = left;
        int r = left;
        int pivot = arr[(left+right)/2];
        int temp = 0;

        while (l < r){

            while(arr[l] < pivot){
                l += 1;
            }

            while(arr[r] > pivot){
                l -= 1;
            }

            if (l >= r){
                break;
            }

            temp = arr[l];
            arr[l] = arr[r];
            arr[r] = temp;


            if (arr[l]==pivot){
                r-=1;
            }

            if (arr[l]==pivot){
                r+=1;
            }
        }

        if(l == r){
            l += 1;
            r -= 1;
        }

        if (left < r){
            quickShort(arr,left,r);
        }

        if(right > l){
            quickShort(arr,l,right);
        }
    }

}

代码实现输出结果


第二题:二叉树遍历

二叉树的先、中、后 序遍历与层级遍历
解题思路:
先序遍历:先根结点,再左子树,最后右子树
中序遍历:先左子树,再根节点,最后右子树
后序遍历:先左子树,再右子树,最后根结点

代码块

package suanfa.demo;

import java.util.LinkedList;

public class paixu{

    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 先序遍历
        System.out.print(root.data+" ");
        if(root.l!=null) {
            A(root.l);
        }
        if(root.r!=null) {
            A(root.r);
        }
    }
    private static void B(Node root) {
        // TODO 中序遍历
        if(root.l!=null) {
            B(root.l);
        }
        System.out.print(root.data+" ");
        if(root.r!=null) {
            B(root.r);
        }
    }
    private static void C(Node root) {
        // TODO 后序遍历
        if(root.l!=null) {
            C(root.l);
        }
        if(root.r!=null) {
            C(root.r);
        }
        System.out.print(root.data+" ");
    }
    private static void D(Node tree) {
        // TODO 层级遍历
        if (tree != null) {

            LinkedList<Node> linkedList = new LinkedList<Node>();
            //先将根节点入队
            linkedList.offer(tree);
            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 @ 2020-10-23 22:56  tzq666  阅读(93)  评论(0)    收藏  举报