Days did not update, these two days is the weekend, to everyone to tidy up a few things, there is about the scope, closure, and recursion, closure and recursion, for most of the first contact programming people still have some difficulty, yesterday, took a little time to everyone to organize a bit, today, to everyone upload up, let us see, Part of the personal point of view, if there are errors, welcome to point out
This article, to everyone talk about recursion, yesterday, finishing three articles took a little time, check out some information, their understanding and everyone said, but a lot of I also speak is not very clear, so this one will use a lot of small examples, small practice, to everyone say, hope you can tell us clearly.
1. Recursion 1.1. What is recursion
In the program, the so-called recursion, is the function itself directly or indirectly call themselves
- Call yourself directly
- Call yourself indirectly
The most important thing in terms of recursion is to jump out of the structure, because jumping out can have results
1.2. The so-called recursion is the thought of normalization
Recursive calls, write recursive functions, and ultimately convert them to this function
If there is a function f, if it is a recursive function, then the problem in the function body is converted to the form F
Recursive thinking is the transformation of a problem into a solved problem to achieve
function f(){ ...f(...)... }
Example: 1, 2, 3, 4, 5, ..., 100
- First assume that the recursive function is already written, assuming that it is foo. That is, foo (100) is for 1 to 100 and
- Looking for a recursive relationship. is the relationship between N and N-1, or n-2: foo (n) = = n + foo (n-1)
var res = foo( 100 ); var res = foo( 99 ) + 100;
- To convert a recursive structure to a recursive body
function foo( n ) { return n + foo( n - 1 ); }
- Convert 100 to 99
- Convert 99 to 98
- ...
- Convert 2 to 1
- 1 result is 1.
- That is: Foo (1) is 1
- Add a critical condition to the recursive body
function foo( n ) { if ( n == 1 ) return 1; return n + foo( n - 1 ); }
Exercise: Ask 1, 3, 5, 7, 9, ... The result of the nth item is the same as the first n. Serial number starting from 0
To find the nth item
- First assume that the recursive function is already written, assuming that it is FN. Then the nth item is FN (n)
- To find a recursive relationship: FN (n) = = f (n-1) + 2
- Recursive body
function fn( n ) { return fn( n-1 ) + 2; }
- Find the critical condition
- Beg N-n-1
- Beg N-1-N-2
- ...
- Request 1-0
- No. 0, it's 1.
- Join the critical condition
function fn( n ) { if ( n == 0 ) return 1; return fn( n-1 ) + 2; }
Top N and
- Assuming it is done, SUM (n) is the top N and
- To find a recursive relationship: the first n items and equals the nth item + the sum of the previous n-1 items
- Get the recursive body
function sum( n ) { return fn( n ) + sum( n - 1 ); }
- Find the critical condition
- Get the recursive function
function sum( n ) { if ( n == 0 ) return 1; return fn( n ) + sum( n - 1 ); }
Exercises: 2, 4, 6, 8, 10 nth and the first N and
Item n
function fn( n ) { if ( n == 0 ) return 2; return fn( n-1 ) + 2;}
Top N and
function sum( n ) { if ( n == 0 ) return 2; return sum( n - 1 ) + fn( n );}
Exercise: Series: 1, 1, 2, 4, 7, 11, 16, ... To find the nth item, the first N and.
To find the nth item
- Assuming you've got the results fn, FN (10) is the 10th item
- Find a recursive relationship
- 0, 1 = fn (0) + 0 = fn (1)
- 1, 2 = FN (1) + 1 = fn (2)
- 2, 3 = FN (2) + 2 = fn (3)
- ...
- n-1, n = fn (n-1) + n-1 = FN (n)
- The recursive body is also clear, the critical condition is n = = 0 = 1
function fn( n ) { if ( n == 0 ) return 1; return fn( n-1 ) + n - 1; }
If starting from 1, then the nth item is
- Assuming you've got the results fn, FN (10) is the 10th item
- Find a recursive relationship
- 1, 2 = FN (1) + 0 = fn (2)
- 2, 3 = FN (2) + 1 = fn (3)
- 3, 4 = FN (3) + 2 = fn (4)
- ...
- n-1, n = fn (n-1) + n-2 = FN (n)
- Critical condition n = = 1 = 1
Top N and
function sum( n ) { if ( n == 0 ) return 1; return sum( n - 1 ) + fn( n ); }
If starting from 0
0 1 2 3 4 5 61, 1, 2, 4, 7, 11, 16,
If starting from 1
1 2 3 4 5 6 71, 1, 2, 4, 7, 11, 16,
Exercise: Fibonacci Series: 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... To find its nth item.
Recursive relationship fn (n) = = fn (n-1) + fn (n-2)
function fib( n ) { if ( n == 0 || n == 1 ) return 1; return fib( n - 1 ) + fib( n - 2 ); }
1.3. Advanced recursive practice 1.3.1. Factorial
Factorial is an operation, and the factorial of a number means that the number is multiplied from 1 onwards. For example 3! Represents 1 * 2 * 3
. 5! 1 * 2 * 3 * 4 * 5
Rule 0 has no factorial, and factorial starts with 1.
To find the factorial of n
function foo ( n ) { if ( n == 1 ) return 1; return foo( n - 1 ) * n; }
1.3.2. exponentiation
To seek power is to seek a certain number of times
2*2 2 Squared, 2 of the 2-time Square
To find the M-Order of N
And finally get a function power (n, m)
The M of N is the M-n multiplication, which is n times (m-1) n multiplication.
function power ( n, m ) { if ( m == 1 ) return n; return power( n, m - 1 ) * n; }
1.3.3. Deep copy
Some people may not know what a deep copy is, and a deep copy corresponds to a shallow copy.
- What is a deep copy, what is a shallow copy
- If you copy a copy of all the reference structures of the data, the data is in memory independent of the deep copy
- If the copy is made only for the properties of the current object, and the property is a reference type this is not considered, then the shallow copy
- Copy: Copies a copy. Refers to copying object data.
- When discussing the deep copy and the shallow copy, make sure that the object's properties are also reference types.
- Encapsulation of code
- Using object-oriented thinking, it is common for objects to have a copy of the method to complete their own copy
- If you need to encapsulate an object into a shallow copy
- This is inside a function (method) that represents the object that called the function (method).
If you want to implement a deep copy then you need to consider the properties of the object, with properties, ... Copy them all.
If you want to implement:
- Assuming clone (O1,O2) has been implemented, copy the members of the object O2 to O1
- Simple algorithm to copy the O2 Zodiac to the O1
function clone( o1, o2 ) { for ( var k in o2 ) { o1[ k ] = o2[ k ]; } }
Find a recursive relationship or call it a problem that has been resolved
Complex implementation: Clone (O)-NEWOBJ
function clone( o ) { var temp = {}; for ( var k in o ) { if ( typeof o[ k ] == ‘object‘ ) { temp[ k ] = clone( o[ k ] ); } else { temp[ k ] = o[ k ]; } } return temp; }
Please implement getelementsbyclassname with recursion
<div> <div>1 <div class="c">2</div> <div>3</div> </div> <div class="c">4</div> <div>5 <div>6</div> <div class="c">7</div> </div> <div>8</div></div>
- If a method Byclass (node, "C", list) is implemented, the element that matches the class attribute C is found on a node
- Finds in child elements of the current element, if any, stored in an array
- First, the child node is traversed, and then see if the child node has child nodes, if there is no direct judgment, if there is no recursion
function byClass( node, className, list ) { var arr = node.childNodes; for ( var i = 0; i < arr.length; i++ ) { if ( arr[ i ].className == className ) { list.push( arr[ i ] ); } if ( arr[ i ].childNodes.length > 0 ) { byClass( arr[ i ], className, list ); } } }
Related Concepts of recursion