软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 算法与数据结构
其他参考文献
1.寻找数组中第K大的数
通过冒泡排序将序列从大到小排序从而得到k
思路
先定义一个数组,再通过一个方法将各个数据进行添加进去,并判断,最后输出;
代码:
package test;

import java.util.Scanner;

public class Test01 {
		public static void main(String[] args) {
	            Scanner sc = new Scanner(System.in);
	            //输入数组长度
		     System.out.println("请输入序列长度:");
		     int nums = sc.nextInt();
		     //创建一个数组
		     int[] num = new int[nums];
		     System.out.println("请输入要查询的序列数据:");
		     //数据循环输入数组
		     for (int i = 0; i <nums; i++){
		    	 num[i] = sc.nextInt();
		        }
		     System.out.println("请输入要查询的个数");
		     int frequency = sc.nextInt();
		     for(int j = 1;j<=frequency;j++) {
		    	 System.out.println("输入查询的数:");
		    	 int first = sc.nextInt();
		    	 int last = sc.nextInt();
		    	 int number = sc.nextInt();
                          //调用方法
		    	 sort(num,first,last,number);
		     }
		}
		private static void sort(int[] num,int first,int last,int number) {
			//判断输入是否正确  first>last
			if(first>last) {
				System.out.println("输入错误,请重新输入");
				Scanner sc = new Scanner(System.in);
				first = sc.nextInt();
				last = sc.nextInt();
				number = sc.nextInt();
			}
			int i2 = 0;
			int i3 = 0;
			int i4 = 1;
			for(int i = 0;i<num.length;i++) {
				//判断输入是否正确  number>长度
				if(num[i] == first) {
					 i2 = i;
				}else if(num[i] == last) {
					 i3 = i;
				}else if(first == i2 && last == i3) {
					 i4 = i3-i2+1;	
				}
				if(i4 < number && i4 != 1) {
					System.out.println("输入错误,请重新输入");
					Scanner sc = new Scanner(System.in);
					first = sc.nextInt();
					last = sc.nextInt();
					number = sc.nextInt();
				}
				//开始真正计算
				if(num[i] == last) {
					System.out.println(num[i-number+1]);
				}
			}
		}
}

运行截图

2.二叉树的先、中、后 序遍历与层级遍历
思路
前序遍历:根结点 ---> 左子树 ---> 右子树
中序遍历:左子树---> 根结点 ---> 右子树
后序遍历:左子树 ---> 右子树 ---> 根结点
层次遍历:只需按层次遍历即可
代码:

package test;

import java.util.LinkedList;

public class Test02 {
	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<Node> linkedList = new LinkedList<Node>();
            //先将根节点入队
            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 @ 2020-10-27 15:59  吴小晃  阅读(77)  评论(0编辑  收藏  举报