Computer programming ideas Series 1] recursive (Recursion)

To iterate is human, to recurse, divine.
People understand iteration, God understand recursion.

1233356-e6ca4f9b4eed7c07
image

Recursion (Recursion algorithm), in computer science refers to a repeat of the same problem into sub-problems and problem-solving methods.

Recursive method can be used to solve a lot of problems in computer science, so it is a very important concept in computer science. The vast majority of programming languages ​​support from call functions, functions in these languages ​​can be done by calling itself recursively.

Theoretical calculation can prove recursive loop effect may be completely substituted, so accustomed to using a recursive function for recycling in many programming languages ​​(e.g. Scheme) in.

Suppose we recursion factorial operator f (n)

f (n) -> n == 1 ? 1 : n * f(n-1) 

f which used f, how to understand it? Very simple, the equation can be expanded:

f(6)
=> 6 * f(5)
=> 6 * (5 * f(4))
=> 6 * (5 * (4 * f(3)))
=> 6 * (5 * (4 * (3 * f(2))))
=> 6 * (5 * (4 * (3 * (2 * f(1)))))
=> 6 * (5 * (4 * (3 * (2 * 1))))
=> 6 * (5 * (4 * (3 * 2)))
=> 6 * (5 * (4 * 6))
=> 6 * (5 * 24)
=> 6 * 120
=> 720 

The sample code

/**
 * @author: Jack
 * 2020-01-11 02:35
 */
class NavTree : NavVisitable {
    var deptNo = ""
    var deptName = ""
    var hasPermission = false
    var leaf = false
    var children : MutableList<NavTree> = mutableListOf()

    constructor(deptNo: String, deptName: String, hasPermission: Boolean, leaf: Boolean) {
        this.deptNo = deptNo
        this.deptName = deptName
        this.hasPermission = hasPermission
        this.leaf = leaf
    }


    /**
     * 接受访问:递归遍历
     */
    override fun accept(visitor: NavVisitor) {
        // 访问当前节点
        visitor.visitTree(this)
        if (children != null) {
            // 访问所有孩子节点
            for (child in this.children!!) {
                val childVisitor = visitor.visitTree(child)
                child.accept(childVisitor)
            }
        }

    }

    /**
     * 构建孩子节点列表
     */
    fun children(node: NavTree): NavTree {
        if (children != null) {
            for (child in children) {
                if (child.deptNo == node.deptNo) {
                    return child
                }
            }
            children.add(node)
        }
        return node
    }

    override fun toString(): String {
        return "NavTree(deptNo='$deptNo', deptName='$deptName', hasPermission=$hasPermission, leaf=$leaf, children=$children)"
    }

}

Recursive thinking

interface TreeVisitor {
    /**
     * 访问函数
     *
     * @param t 访问对象(树节点)
     */
    void visit(ItemVO t);
}

    /**
     * 访问者模式,递归遍历树节点
     *
     * @param t       树节点
     * @param visitor 访问者
     */
    private static void visitTree(ItemVO t, TreeVisitor visitor) {
        visitor.visit(t);
        if (null != t.children) {
            for (ItemVO child : t.children) {
                visitTree(child, visitor);
            }
        }
    }

Visitors Design Patterns

1233356-bfad520c87b9cd4d
image

In the visitor pattern (Visitor Pattern), we use a visitor class, it changes the algorithm execution element classes. In this way, the algorithm elements can change as the visitor change. This type of design pattern belongs behavioral patterns. Depending on the mode, the subject has received a visitor element objects, such objects can visitors processing operations on the element object.

Introduction

Intent: separating the main data structures and data manipulation.

Mainly to solve: stable data structures and variable operating coupling problem.

When to use: the need for a lot of different and unrelated to the operation of the structure of an object in an object, and to make these operations need to avoid "contamination" of these classes of objects, these packages use of visitors to the class.

How to fix: add a reception of foreign visitors to provide interfaces to be accessed inside the class.

The key code: the data base class which has a method to accept visitors, the self-reference incoming visitors.

Application examples: You friend's house guest, you are a visitor, a friend accepts your access, through your friend's description, then the description of a friend make a judgment, which is the visitor pattern.

Advantages: 1, in line with the principle of single responsibility. 2, excellent scalability. 3, flexibility.

Disadvantages: 1, the specific elements of the visitor announced details of a violation of the principle of Demeter. 2, the specific elements of change more difficult. 3, in violation of the Dependency Inversion principle, rely on a specific class that does not rely on abstraction.

Be used: 1, the object corresponding to the object class structure rarely changes, but often need to define a new operation on the object structure. 2, the need for the structure of a target object in a lot of different and unrelated operations, and these operations need to avoid "contamination" class of these objects, we do not want to modify these classes when adding new operations.

Note: Visitors can be unified function, you can make a report, UI, interceptors and filters.

In mathematical logic and computer science, a recursive function is a recursive function μ- or class of functions from natural numbers to the natural numbers, it is intuitive to some sense "computable." In fact, proved recursive function is precisely computable functions in computability Turing machine theory. Recursive function about primitive recursive functions, and their inductive definition (see below) built upon primitive recursive functions. However, not all recursive functions are primitive recursive function - this function is the most famous Ackermann function.

Other class of functions is equivalent λ- Markov algorithm and recursive functions computable functions.

Recursion Fibonacci number

1233356-d50deb5e3d41991b
image

Recursive factorial

1233356-5c572b5be665605b
image

Kotlin developer community

1233356-4cc10b922a41aa80

Related Topics domestic first Kotlin developer community public numbers, the main share exchange Kotlin programming language, Spring Boot, Android, React.js / Node.js, functional programming, programming ideas and so on.

The more hustle and bustle of the world, the more you need quiet to think.

Released 1571 original articles · won praise 627 · views 610 000 +

Guess you like

Origin blog.csdn.net/universsky2015/article/details/104552371