SSM框架项目实践 二分搜索树

学习目标:

需求分析与SSM环境准备(上)

学习二分搜索树删除最大元素与最小元素


学习内容:

Spring与SpringMVC环境准备,解决请求与响应乱码问题,完成Spring与Mybatis的整合,继承Junit单元测试与Logback日志。代码如下:

pom.xml:



    4.0.0

    org.example
    imooc-reader
    1.0-SNAPSHOT

    
        8
        8
    
    
        
            aliyun
            aliyun
            https://maven.aliyun.com/repository/public
        
    
    
        
            org.springframework
            spring-webmvc
            5.3.8
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        
        
            org.springframework
            spring-jdbc
            5.3.8
        
        
            org.mybatis
            mybatis
            3.5.7
        
        
            org.mybatis
            mybatis-spring
            2.0.6
        
        
            mysql
            mysql-connector-java
            8.0.27
        
        
            com.alibaba
            druid
            1.2.13-SNSAPSHOT
        
        
            junit
            junit
            4.12
            test
        
        
            org.springframework
            spring-test
            5.3.8
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
    

applicationContext.xml:



    
    
        
            
                
                    
                        application/json;charset=utf-8
                    
                
            
        
    
    


    
        
        
        
        
        
        
    

    
        
        
        
    

    
        
    

web.xml:



    
        index.html
    
    
        spring-mvc
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:applicationContext*.xml
        
        0
    
    
        spring-mvc
        /
    
    
        characterFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterFilter
        /*
    

mybatis-config.xml:




    
        
    

logback.xml:



    
        
            [%thread] %d %level %logger{10} -%msg%n
            UTF-8
        
    








    
        
    



二分搜索树:

import com.sun.corba.se.impl.resolver.SplitLocalResolverImpl;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

public class BST> {
    private class Node {
        public E e;
        public Node left, right;

        public Node(E e) {
            this.e = e;
            left = null;
            right = null;
        }
    }

    private Node root;
    private int size;

    public BST() {
        root = null;
        size = 0;
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    //向二分搜索树中添加新的元素e
    public void add(E e) {
        root = add(root, e);
    }

    //向以node为根的二分搜索树中插入元素E,递归算法
    //返回插入节点后二分搜索树的根
    private Node add(Node node, E e) {

        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        } else if (e.compareTo(node.e) > 0)
            node.right = add(node.right, e);
        return node;
    }

    //看二分搜索树中是否包含元素e
    public boolean contains(E e) {
        return contains(root, e);
    }

    //以node为根的二分搜索树中是否包含元素e,递归算法
    private boolean contains(Node node, E e) {
        if (node == null)
            return false;
        if (e.compareTo(node.e) == 0)
            return true;
        else if (e.compareTo(node.e) < 0)
            return contains(node.left, e);
        else
            return contains(node.right, e);
    }

    //二分搜索树的前序遍历
    public void preOrder() {
        preOrder(root);
    }

    //前序遍历以node为根的二分搜索树,递归算法
    private void preOrder(Node node) {
        if (node == null)
            return;
        System.out.println(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }

    //二分搜索树的非递归前序遍历
    public void preOrderNR() {
        Stack stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node cur = stack.pop();
            System.out.println(cur.e);
            if(cur.right!=null)
                stack.push(cur.right);
            if (cur.left != null)
                stack.push(cur.left);
        }
    }

    //二分搜索树的中序遍历
    public void inOrder() {
        inOrder(root);
    }

    //中序遍历以node为根的二分搜索树,递归算法
    private void inOrder(Node node) {
        if (node == null) {
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    //二分搜索树的后序遍历
    public void postOrder() {
        postOrder(root);
    }
    //后序遍历以node为根的二分搜索树,递归算法
    private void postOrder(Node node){
        if(node==null){
            return;
        }
        postOrder(node.left);
        postOrder(node.right);
        System.out.println(node.e);
    }
    //二分搜索树的层序遍历
    public void levelOrder(){
        Queue q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            Node cur = q.remove();
            System.out.println(cur.e);
            if (cur.left != null)
                q.add(cur.left);
            if (cur.right != null)
                q.add(cur.right);
        }
    }
    //寻找二分搜索树的最小元素
    public E minimum(){
        if (size == 0)
            throw new IllegalArgumentException("BST is empty!");
        return minimum(root).e;
    }
    //返回以node为根的二分搜索树的最小值所在的节点
    private Node minimum(Node node){
        if(node.left==null)
            return node;
        return minimum(node.left);
    }
    //寻找二分搜索树的最大元素
    public E maximum(){
        if (size == 0)
            throw new IllegalArgumentException("BST is empty!");
        return maximum(root).e;
    }
    //返回以node为根的二分搜索树的最大值所在的节点
    private Node maximum(Node node){
        if(node.right==null)
            return node;
        return minimum(node.right);
    }
    //从二分搜索树中删除最小值所在的节点,返回最小值
    public E removeMin(){
        E ret = minimum();
        root=removeMin(root);
        return ret;
    }

    //删除掉以node为根的二分搜索树中的最小节点
    //返回删除节点后新的二分搜索树的根
    private Node removeMin(Node node){
        if(node.left==null){
            Node rightNode=node.right;
            node.right=null;
            size--;
            return rightNode;
        }
        node.left = removeMin(node.left);
        return node;
    }
    //从二分搜索树中删除最大值所在的节点,返回最小值
    public E removeMax(){
        E ret = maximum();
        root=removeMax(root);
        return ret;
    }

    //删除掉以node为根的二分搜索树中的最大节点
    //返回删除节点后新的二分搜索树的根
    private Node removeMax(Node node){
        if(node.right==null){
            Node leftNode=node.left;
            node.left=null;
            size--;
            return leftNode;
        }
        node.right = removeMax(node.right);
        return node;
    }
    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        generateBSTString(root, 0, res);
        return res.toString();
    }

    //生成以node为根节点,深度为depth的描述二叉树的字符串
    private void generateBSTString(Node node, int depth, StringBuilder res) {
        if (node == null) {
            res.append(generateDepthString(depth) + "null\n");
            return;
        }
        res.append(generateDepthString(depth) + node.e + "\n");
        generateBSTString(node.left, depth + 1, res);
        generateBSTString(node.right, depth + 1, res);
    }

    private String generateDepthString(int depth) {
        StringBuilder res = new StringBuilder();
        for (int i = 0; i < depth; i++) {
            res.append("--");
        }
        return res.toString();
    }

}

学习时间:

08:30-10:30 11:30-12:30 15:00-16:00


学习产出:

对于SSM框架的环境准备进行了系统的学习,同时学会了Spring与Mybatis的整合,对于以后基于SSM框架的开发有了一定的经验。

对于二分搜索树删除最大值和最小值进行了代码的实现,对于明天学习在二分搜索树中删除任意一个元素打下了基础。

你可能感兴趣的:(日志,java,spring,开发语言,算法)