2020软件工程作业04

|这个作业属于哪个课程|https://edu.cnblogs.com/campus/zswxy/2018SE/|
| ---- | ---- | ---- |
|这个作业要求在哪里|https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406|
|这个作业的目标|熟悉算法知识并完成算法题目|

第一题

解题代码:

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

            public class sf {
                Scanner  input =new Scanner(System.in);
                int n=0;

	System.out.print("输入序列长度:");
	n =input.nextInt();

	
int [] line = new int[n];
System.out.print("输入序列值:");
for (int i = 0; i < n; i++) {
	line[i]=input.nextInt();       	   
}   
System.out.print("输入询问次数:");
int m =input.nextInt();
int l,r,k;

int []answer =new int[m];
for (int j = 0; j < m; j++) {
	System.out.print("输入l r k:");
	l =input.nextInt();
	r=input.nextInt();
	k=input.nextInt();
	
		
		int[] newline =Arrays.copyOfRange(line, l-1, r);			
		for (int i = 0; i < newline.length-1; i++) {
			for (int o = 0; o < newline.length-1-i; o++) {
				if (newline[o]>newline[o+1]) {
					int temp =newline[o];
					newline[o]=newline[o+1];
					newline[o+1]=temp;
				}
			}
		
		
		
		answer[j]=newline[newline.length-k];
	}
}
System.out.println("输出结果:");
for (int i : answer) {
	System.out.println(i);
}

}
`

解题思路:

获取输入的数字
获取序列的长度
获取序列具体的值
定义一个数组arr利用for循环将输入的值保存
输入查询次数
输入查询数组中区间及区间内值位置
输出结果

第二题

解题代码:

`
package test;
import java.util.LinkedList;
public class test1 {
public static void main(String[] args) {
/* 二叉树的结构
A
/
T 6
/
D
/
N 5
/ \ /
B 4 1

9
*/
Node root = into();
// 先序遍历
A(root);
System.out.println("先序遍历");
// 中序遍历
B(root);
System.out.println("中序遍历");
// 后续遍历
C(root);
System.out.println("后序遍历");
// 层级遍历
D(root);
System.out.println("层级遍历");

}
 private static void A(Node tree) {
        // TODO 先序遍历
        if (tree != null) {
            System.out.print(tree.data + " ");
            A(tree.l);
            A(tree.r);
        }

    }

private static void B(Node tree) {
    // TODO 中序遍历
    if (tree != null) {
        B(tree.l);
        System.out.print(tree.data + " ");
        B(tree.r);
    }

}

private static void C(Node tree) {
    // TODO 后续遍历
    if (tree != null) {
        C(tree.l);
        C(tree.r);
        System.out.print(tree.data + " ");
    }

}

private static void D(Node tree) {
    // TODO 层级遍历
    if (tree != null) {
        LinkedList<Node> linkedList = new LinkedList<Node>();
        //先将根节点入队,LinkedList类是双向列表,列表中的每个节点都包含了对前一个和后一个元素的引用.
        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-28 22:30  潋燃  阅读(81)  评论(0编辑  收藏  举报