2020软件工程作业04

这个作业属于哪个课程 2018软件工程3班
这个作业要求在哪里 2020软件工程作业04
这个作业的目标 算法的实现
参考文献 二叉树的遍历

第一题

题目名称:寻找数组中第K大的数 考察算法:排序算法;

解题思路:

第一行包含一个数n,表示序列长度。
第二行包含n个正整数,表示给定的序列。
第三个包含一个正整数s,表示询问的个数。
接下来的s行,每行三个数l,r,K,表示询问序列从左往右第l个数到第r个数中,从大往小第K大的数是哪个,序列元素从1开始标号。
总共输出m行,每行一个数,表示询问的答案。

解题代码:

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

public class task2 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc = new Scanner(System.in);
		// 定义序列长度
		int n = sc.nextInt();
		// 定义序列
		int[] a = new int[n];
		// 依次输入序列
		for (int i = 0; i < n; i++) {
			a[i] = sc.nextInt();
		}
		// 定义询问s个数
		int s = sc.nextInt();
		// 每行有三个数
		int[][] arr = new int[s][3];
		// 输入的三个数
		for (int i = 0; i < s; i++) {
			// 第s行第1个数
			arr[i][0] = sc.nextInt();
			arr[i][1] = sc.nextInt();
			arr[i][2] = sc.nextInt();
		}
		
		sc.close();//关闭输入资源
		
		for (int i = 0; i < s; i++) {
			System.out.println(getMaxk(a, arr[i][0], arr[i][1], arr[i][2]));
		}
	}

	public static int getMaxk(int[] a, int begin, int end, int k) {
		int aa[] = new int[end - begin + 1];// 创建一个数组,长度为end
		// 用来装表示的开始到结束的数组,第几个到第几个,谁最大
		for (int i = 0; i < aa.length; i++) {
			aa[i] = a[begin + i - 1];
		}
		Arrays.sort(aa);// 进行排序,从小到大
		return aa[aa.length - k];// 第几大
	}

}

运行效果截图:

第二题

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

解题思路:

定义节点以及节点本身的属性和先、中、后 序遍历的方法;
定义二叉树,获取根节点,调用节点的先、中、后 序遍历与层级遍历的方法;
实例化二叉树,初始化根节点,实现二叉树的先、中、后 序遍历与层级遍历;

解题代码:

import java.util.LinkedList;
import javax.swing.tree.TreeNode;
public class task1 {
	public static void main(String[] args) {
		/*
		 * 作业要求:叉树的先、中、后 序遍历与层级遍历 自己实现四个方法,main方法中调用,将结果打印到控制台
		 */
					        /*  二叉树的结构
						        A
						       / \
						      T   6
						     /
						    D
						  /   \
						 N     5
						/ \    /
					        B  4  1
						    \
						     9
						*/
                //计时开始
		long start = System.currentTimeMillis();
		//实例化二叉树;
		BinaryTree binarytree = new BinaryTree();
		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;
		binarytree.setRoot(root);//设置根节点;
	//实现遍历
		System.out.println("先序遍历:");
		binarytree.a();
		System.out.print("\n");
		System.out.println("中序遍历:");
		binarytree.b();
		System.out.print("\n");
		System.out.println("后序遍历:");
		binarytree.c();
		System.out.print("\n");
		System.out.println("层级遍历:");
		binarytree.level(root);
                //计时结束		
		long end = System.currentTimeMillis();
		System.out.print("\n");
		System.out.println("程序运行时间:"+(end-start)+"ms");

	}
}

// 定义二叉树
class BinaryTree {
	private Node root;

	public void setRoot(Node root) {// 获取根节点
		this.root = root;
	}
	// 二叉树调用  节点  的先序遍历,中序遍历,后序遍历方法;
	// 1.先序遍历
	public void a() {
		if (this.root != null) {
			this.root.A();
		} else {
			System.out.println("当前二叉树为空,无法遍历!");
		}

	}
	// 2.中序遍历
	public void b() {
		if (this.root != null) {
			this.root.B();
		} else {
			System.out.println("当前二叉树为空,无法遍历!");
		}

	}
	// 3.后序遍历
	public void c() {
		if (this.root != null) {
			this.root.C();
		} else {
			System.out.println("当前二叉树为空,无法遍历!");
		}

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

}
// 定义节点
class Node {
	// 数据
	Object data;
	// 左节点
	public Node l;
	// 右节点
	public 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;
	}
	// 定义节点的先序遍历,中序遍历,后序遍历方法;
	public void A() {
		// TODO 先序遍历
		// 1.先输出当前节点(初始的时候是root节点)
		// 2.如果左子节点不为空,则递归继续先序遍历;
		// 3.如果右子节点不为空,则递归继续先序遍历;
		System.out.print(this.data+",");
		if (this.l != null) {
			this.l.A();
		}
		if (this.r != null) {
			this.r.A();
		}
	}
	public void B() {
		// TODO 中序遍历
		// 1.如果左子节点不为空,则递归继续中序遍历;
		// 2.输出当前节点;
		// 3.如果右子节点不为空,则递归继续中序遍历;
		if (this.l != null) {
			this.l.B();
		}
		System.out.print(this.data+",");
		if (this.r != null) {
			this.r.B();
		}
	}
	public void C() {
		// TODO 后续遍历
		// 1.如果左子节点不为空,则递归继续后序遍历;
		// 2.如果右子节点不为空,则递归继续后序遍历;
		// 3.输出当前节点;
		if (this.l != null) {
			this.l.C();
		}
		if (this.r != null) {
			this.r.C();
		}
		System.out.print(this.data+",");
	}
}

运行效果截图:

我的作业表格

期数 详情
第一期 自我介绍
第二期 2020软件工程作业02
第三期 2020软件工程作业03
posted @ 2020-10-25 21:33  这斯很快乐  阅读(91)  评论(0编辑  收藏  举报