2020软件工程作业04

这个作业属于哪个课程 https://edu.cnblogs.com/campus/zswxy/2018SE/
这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/2018SE/homework/11406
这个作业的目标 <算法训练>
学号 <20189638>

1.寻找数组中第K大是数

  • 思路:对指定的 [ l , r ] 区间进行从大到小的排序,然后输出第 l+k-1 个数,就是第k个大的数,注:每次查询要创建数组存储临时数组。
#include <iostream>
 2 using namespace std;
 3 int main()
 4 {
 5     int a[1001];
 6     int n,m;
 7     cin>>n;
 8     for(int i=1;i<=n;i++)
 9         cin>>a[i];
10     cin>>m;
11     while(m--){
12         int b[1001];
13         int l,r,k;
14         cin>>l>>r>>k;
15         //赋值给新的数组,以免影响到后面的查询
16         for(int i=l;i<=r;i++)
17             b[i]=a[i]; 
18         //从大到小排序 
19         for(int i=1;i<=r-l;i++)
20             for(int j=l;j<=r-i;j++){
21                 if(b[j] < b[j+1]){
22                     int t;
23                     t=b[j];b[j]=b[j+1];b[j+1]=t;
24                 }
25             }
26         cout<<b[l+k-1]<<endl;
27     }
28     return 0;
29 }


2.二叉树的先、中、后 序遍历与层级遍历

  • 思路:
    1.前序遍历:指的是先访问根,然后再访问子树的遍历方式;例如:F, B, A, D, C, E, G, I, H.
    2.中序遍历:先访问左子树,然后访问根,再访问右子树;先访问右子树,然后访问根,再访问左子树;不过,一般按照第一种的方式进行中序遍历;例如:A, B, C, D, E, F, G, H, I.
    3.后序遍历:先访问子树,再访问根的遍历方式;例如:A, C, E, D, B, H, I, G, F.

  • 四种基本的遍历思想为:
    1.前序遍历:根结点 ---> 左子树 ---> 右子树
    2.中序遍历:左子树---> 根结点 ---> 右子树
    3.后序遍历:左子树 ---> 右子树 ---> 根结点
    4.层次遍历:仅仅需按层次遍历就可以


    public static void main(String[] args) {
        /*
            作业要求:叉树的先、中、后 序遍历与层级遍历
            自己实现四个方法,main方法中调用,将结果打印到控制台
         */
        /*  二叉树的结构
                     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-29 19:35  干就完了!!!  阅读(85)  评论(0)    收藏  举报