In: Computer Science
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?
/*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.