Java development with recursion encountered by the pit return

Source: Internet
Author: User

Recently in a distribution system, infinite level, need to use recursive traversal, before the release of the post code on the problem, but the algorithm is no problem, read the reader please correct, the last blog address is as follows:

http://blog.csdn.net/qq_35624642/article/details/54235472

On the internet also looked at the related recursive algorithm, many people suggest using the Loop + stack replacement recursion to prevent stack overflow.

There is no comparison, interested readers can search.

Call recursion in the process of using the IF () {}else{}, and then want to use return to end the loop need to pay special attention, this time to encounter this pit, can only say that their recursive algorithm is not very understanding.

The four basic laws of recursion are: (quoted from "Data structure and algorithmic analysis--c language description" Mark Allen Weiss)
1. Benchmark situation.
2. Keep pushing.
3. Design rules.
4. Principle of synthetic benefits.

Here is the scope of return,

Return is the end of the current function, and for the parent function you have done the function, the parent function executes the next statement.
Did not think the parent function immediately encountered a return, the parent function is over, Grandpa function for the parent function is completed, Grandpa function will then execute the next statement
I didn't think ...
I didn't think ...

End, back to where the recursive function was originally invoked.

The benchmark case for this function is the case of Num <= 1, in which case it can be solved without recursion, which is to return 1 directly.
and return Numfact (NUM-1); is "keep pushing". Return 8fact (8-1); You need to know fact (7), to know fact (7), you need to return 7fact (7-1); To know fact (6), you need to return 6fact (6-1); So go on until you encounter a "benchmark scenario." Front of the parent function, grandfather function metaphor is very image.

So in the previous place added a return to get the correct results, the modified code is as follows:


/** * Statistical agents themselves splitting * *@paramuserlistList of agents with superior and subordinate relationships *@paramUserlistbList of agents, no subordinate relationship *@returnList of agents * *PrivateList<user> Counttree (list<user> userlist, list<user> userlistb) { for(User usera:userlist) {Double fee = 0.0;//Total commission Double split = 0.0;////Splitting ratio differenceif(! Collectionutils.isempty (Usera.getwb_type ()) &&! Collectionutils.isempty (usera.getfs_user_id ())) {fee = Moneyutil.multiply (Usera.getwb_type (), UserA.getFs_use R_ID ())//To calculate on the basis of settlement a own merchant splitting}if(Usera.getso_child (). Equals ("1") {Double fscode = 0.0;//subordinate contribution to superior agent splitting a Double fscodefee = 0.0;//Agent Settlement basis Summary (yuan) Aif(! Collectionutils.isempty (USERLISTB)) { for(User Userb:userlistb) {if(userb.getso_id (). Equals (usera.getsu_id ())) {if(! Collectionutils.isempty (Usera.getwb_type ()) &&! Collectionutils.isempty (Userb.getwb_type ())) {split = Moneyutil.subtract (Usera.getwb_type ()),
                        Userb.getwb_type ()); }if(Userb.getso_child (). Equals ("1") {Fscodefee = Moneyutil.add (Fscodefee, Moneyutil.convertdouble (userb.getfs_user_id ()), 4, M Oneyutil.round_half_up)//Agent settlement basis (yuan) +b fscodefee = Countrecursion (Fscodefee, Userlistb, userb.getsu_id ())//C based on settlement basis Level below splitting Fscode =moneyutil.multiply (Fscodefee, Split, 4, Moneyutil.round_half_up)//Calculate subordinate contribution to superior agent splitting}Else{Fscodefee = Moneyutil.add (Fscodefee, Moneyutil.convertdouble (userb.getfs_user_id ()), 4, Mone Yutil.round_half_up)/Agent settlement basis (yuan) +b fscode = Moneyutil.add (Fscode, moneyutil.multiply (Split, Fscodefee, 4, Moneyut Il.round_half_up)//(b splitting}} fee = Moneyutil.add based on settlement basis (FsC Ode, Fee, 4, Moneyutil.round_half_up); Usera.setfs_code (moneyutil.rounddouble string.valueof (Fscode), 4, Moneyutil.round_half_up)); Usera.setfs_code_fee (moneyutil.rounddouble string.valueof (Fscodefee), 4, Moneyutil.round_half_up)); Usera.setfs_fee_split (moneyutil.rounddouble string.valueof (fee), 4, Moneyutil.round_half_up)); } returnuserlist; /** * Using recursive traversal agents, and statistics of the maid *@seeReturn Countrecursion (Fscodefee, UserList, user.getsu_id ())//The return here is important to jump out of the loop *@paramFscodefeeMerchant Flow statistics for dealers *@paramuserlistList of agents *@paramso_idParent Agent ID *@returnTotal Commission * *PrivateDouble countrecursion (double fscodefee, list<user> userlist, String so_id) { for(User user:userlist) {if(user.getso_id (). Equals (so_id)) {if(User.getso_child (). Equals ("1") {Fscodefee = Moneyutil.add (Fscodefee, Moneyutil.convertdouble (user.getfs_user_id ()), 4, Moneyutil.round_half_up);////Agent settlement basis (yuan) +c returnCountrecursion (Fscodefee, UserList, user.getsu_id ()); }Else{Fscodefee = Moneyutil.add (Fscodefee, Moneyutil.convertdouble (user.getfs_user_id ()), 4, Moneyutil.round_half_up)/Agent settlement basis (Yuan) +c}}} returnFscodefee; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.