2020软件工程作业04

| 这个作业属于哪个课程 | https://edu.cnblogs.com/campus/zswxy/2018SE |
| ---- | ---- | ---- |
| 这个作业要求在哪里 | https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406 |
| 这个作业的目标 | 学习算法与数据结构,并完成两道算法题 |
| 其他参考文献 | 无|

第一题:
题目名称

寻找数组中第K大的数 考察算法:排序算法

解题思路

将正整数存储在列表中,按照左边界和右边界获得新的列表并进行从大到小的排序,这里使用的是快速排序,最后返回K值。

解题代码:
package qiyue;

import java.util.Scanner;

public class qiyue {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("请输入序列的个数:");
int num1 = sc.nextInt();
System.out.println("请输入序列:");
int[] arr1 = new int[num1 + 1];
for(int i = 1; i <= num1; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("请输入要查找的个数:");
int[] arr2 = new int[num1 + 1];
int temp;
int l,r,k;
int num2 = sc.nextInt();
System.out.println("请输入三个数:");
for(int i = 1; i <= num2; i++) {
for(int j = 1;j <= num1; j++) {
arr2[j]=arr1[j];
}
l = sc.nextInt();
r = sc.nextInt();
k = sc.nextInt();
//用冒泡排序排列
for(int z = l;z <= r; z++) {
for(int j = z;j <= r; j++) {
if(arr2[j] > arr2[z]) {
temp=arr2[j];
arr2[j]=arr2[z];
arr2[z]=temp;
}
}
}
System.out.println("从左往右,从大往小第"+ k + "大的数是:");
System.out.println(arr2[l + k - 1]);
}

}
}

 运行结果:

心得:在此次作业中明白自己对于实操代码确实还存在很多不足,需要通过日后学习,来丰富自己的知识,做到知行合一。
此次代码主要是对老师和同学的代码学习,但在学习过程中更加了解了java是一次收获不小的学习。

第二题:

题目名称

二叉树的先、中、后序遍历与层级遍历 考察算法:搜索算法

解题思路

先序遍历:根在前,从左往右,一棵树的根永远在左子树前面,左子树又永远在右子树前面

中序遍历:根在中,从左往右,一棵树的左子树永远在根前面,根永远在右子树前面

后序遍历:根在后,从左往右,一棵树的左子树永远在右子树前面,右子树永远在根前面

层级遍历:就是按层,从上到下,从左到右遍历

解题代码:

package qiyue;

import java.util.LinkedList;

import puyuexing.zhou.Node;

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

9
*/
Node root = into();
// 先序遍历
System.out.println("\n"+"前序遍历");
A(root);
// 中序遍历
System.out.println("\n"+"中序遍历");
B(root);
// 后续遍历
System.out.println("\n"+"后序遍历");
C(root);
// 层级遍历
System.out.println("\n"+"层序遍历");
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 层级遍历
    	LinkedList list= new LinkedList<>();
        list.add(root);
        while(! list.isEmpty()) {
            Node ww=(Node) 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 17:38  猫大概  阅读(110)  评论(0)    收藏  举报