Location>code7788 >text

Recursively obtaining department numbers

Popularity:474 ℃/2025-04-17 15:37:54

Preface

Recursion is a very important concept in programming. Simply put, recursion refers to a function calling itself directly or indirectly in its definition. This calling mechanism allows functions to solve complex problems by decomposing problems into smaller similar subproblems.

‌Definition of Recursion‌
Recursion is a method of calling the function itself in a function definition. It usually contains one or more base cases that end the recursive call chain, and one or more recursive steps that break the problem down into smaller subproblems.

‌The principle of recursion‌

1. Benchmark case‌: This is the termination condition of recursive calls.  When the benchmark case is met, the function will no longer call itself, but will return the result.
 2. Recursive step‌: In the recursive step, the function will call itself to solve a similar problem with a smaller scale.  This process will continue until the benchmark situation is reached.

Example of recursion‌

Now I'll get the department tree as an example

public List<SysDept> getDeptTree() {
        try {
            //1. Query all department information
            List<SysDept> sysDepts = ();
            //2. Group all departments according to parent id
            Map<Long, List<SysDept>> childrenMap = ()
                    .collect((SysDept::getParentId));
            //3. Assembly department tree structure
            List<SysDept> result = new ArrayList<>();
            for (SysDept sysDept : sysDepts) {
                //4. Assume that the parentId of the top department is 0
                if (() == 0L) { 
                    //5. Make children of the top department all its sub-departments
                    List<SysDept> children = getChildren(sysDept, childrenMap);
                    (children);
                    (sysDept);
                }
            }
            return result;
        } catch (Exception e) {
            throw new RuntimeException("Failed to obtain the department tree structure!"+());
        }
    }
    
    /*** Recursively obtain sub-department*/ 
    private List<SysDept> getChildren(SysDept root, Map<Long, List<SysDept>> childrenMap) {
        List<SysDept> treeList = ((), new ArrayList<>());
        for (SysDept sysDept : treeList) {
            //6. Recursively obtain sub-department
            (getChildren(sysDept, childrenMap));
        }
        return treeList;
    }

Notes‌

  • ‌Efficiency Problem‌: Recursion may cause stack overflow or repeated calculations (such as naive recursion of Fibonacci sequences), and need to be combinedTail recursive optimizationorDynamic planningimprove.
  • ‌Understanding difficulty‌: It is necessary to analyze through the "progress + regression" process, such as the gradual expansion and backtracking of factorials.

‌Summary‌: Recursion is a powerful programming paradigm suitable for scenarios where problems can be decomposed into self-similar structures, but termination conditions and optimization performance need to be designed with caution.