Question

In: Computer Science

1/ a) What determines whether you should use a loop or recursion when you are writing...

1/

a) What determines whether you should use a loop or recursion when you are writing an algorithm for a tree, and why does it matter?

b) Why did we put Head and Tail nodes in our List class?

Solutions

Expert Solution

/*If you any query do comment in the comment section else like the solution*/


There is nothing that cannot be written in recursion or by using iterative methods, but in case of trees writing the code in iterative approach makes code look complex and a bit difficult to understand and implement where as code written using recursive function are way too cleaner than iterative one and it also easy to understand. Recursion also makes code shorter in length.

We keep head node in a list for very obvious reason because without it we will not be able to traverse the linked list i,e, we need an entry point of linked list which is provided by the head pointer and tail pointer is required to add a new node without traversing the linked list completely starting from head to tail just by updating the next pointer of tail node to new node and updating the tail pointer to the new node, this operation can be done in O(1) time.


Related Solutions

ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your...
ASAP PLEASE!!!! USING JAVA /* 1. When should you use a do-while loop? ** Write your answer as a multi-line Java comment ** */ /* 2. Identify the algorithm that matches this code snippet. Your choices are:   sum and average, counting matches, first match, prompt until match, and   comparing adjacent values.  Write your answer below the coded.        int firstNum = 0;   int number = scnr.nextInt();   while (scnr.hasNextInt())   {   int input = scnr.nextInt();   if (input == number)   {   firstNum++;   }   }...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want...
Recursion Discussions Question: Why would you want to use recursion? Concerning programming, when would you want to use iterative vs. recursive programming? Where does coding fit in concerning the software process (i.e. its use in design, in coding, etc.)? Describe recursion problems, and why does it seem to fit with recursion? (e.g. nature, math, music, sports game, etc.)  
1. In the demonstration where a magnet is brought near a loop, what determines the induced...
1. In the demonstration where a magnet is brought near a loop, what determines the induced current? the strength of the magnetic field intercepted by the loop the rate at which the number of magnetic field lines intercepted by the loop changes the spacing of the magnetic field lines intercepted by the loop the number of magnetic field lines intercepted by the loop 2. Which describes the flux of a magnetic field that is tangent to a section on a...
In this writing assignment, you will argue whether a bank should have the legal right to...
In this writing assignment, you will argue whether a bank should have the legal right to assign a mortgage to a third party. Deliverables: A 150-word (2-3 paragraph) paper Step 1: Write a persuasive statement. Write a short persuasive statement either supporting or opposing a bank's right to assign a mortgage to a third party (for example, a third-party financial institution).
How should you determine whether to take or reject a project? What measure should you use...
How should you determine whether to take or reject a project? What measure should you use to figure out the dollar value of the project today? If the rate of return is positive, can percentage price change be negative? How many years will it take you to double your money if your rate of return is 7% annually?
what determines whether a paralegal is competent
what determines whether a paralegal is competent
1. Describe the cost-benefit analysis an individual should use when trying to decide whether or not...
1. Describe the cost-benefit analysis an individual should use when trying to decide whether or not to invest in general on-the-job training. Explain why these decisions may differ for women versus men. Graphical analysis is strongly encouraged. 2. Explain general and firm-specific on-the-job training. Would employers be less likely to invest in general on-the-job training if they regard women as more likely to leave? Why or why not? Would employers be less likely to invest in firm-specific training if they...
When naming and writing molecular formulas do you use the prefixes and when you name ionic...
When naming and writing molecular formulas do you use the prefixes and when you name ionic compounds and formulas you write the formula based off of their ion charges?
Deciding Factors What factors should a business consider when deciding whether to use a SaaS product...
Deciding Factors What factors should a business consider when deciding whether to use a SaaS product vs. a local running, non-cloud alternative? Are the factors the same for an individual or are they different?
1. How should you determine whether to take or reject a project? What measure should you...
1. How should you determine whether to take or reject a project? What measure should you use to figure out the dollar value of the project today? 2. If rate of return is positive, can percentage price change be negative? 3. How many years will it take you to double your money if your rate of return is 7% annually? Can you please show math. Thanks
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT