Concept explainers
Explanation of Solution
Method definition for “reverse()”:
The recursive method definition for “reverse()” is given below:
/* Recursive method definition for "reverse" with parameter */
private Node reverse(Node list)
{
//Create a node "newList"
Node newList;
/* If the given node is null or last node is null, then */
if((list == null) || (list.next == null))
{
/* Return the node */
return list;
}
/* Recursively call the method "reverse" for remaining node */
newList = reverse(list.next);
//Modify references for middle sequence
list.next.next = list;
list.next = null;
//Return new head node in each recursion
return newList;
}
/* Recursive Method definition for reverse method without parameter */
private void reverse()
{
/* Call the method "reverse" with list head */
first = reverse(first);
}
Explanation:
The above method definition is used to reverse the elements in a list.
- Recursive method definition of “reverse()” with an parameter “list”.
- Create a node “newList”.
- If the given node is null or last node is null, then return the node.
- Recursively call the method “reverse” for remaining node.
- Modify references for middle sequence.
- Finally, return new head node in each recursion.
- Recursive method definition of “reverse()” without parameter.
- Call the method “reverse” with list head.
Complete code:
The complete executable code for reverse the elements in a list using recursive “reverse()” method is given below:
//Define "LinkedList1" class
class LinkedList1
{
/** The code for this part is same as the textbook of "LinkedList1" class */
/* Recursive method definition for "reverse" with parameter */
private Node reverse(Node list)
{
//Create a node "newList"
Node newList;
/* If the given node is null or last node is null, then */
if((list == null) || (list.next == null))
{
/* Return the node */
return list;
}
/* Recursively call the method "reverse" for remaining node */
newList = reverse(list...
Want to see the full answer?
Check out a sample textbook solutionChapter 19 Solutions
Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)
- Write the recursive method for adding a node in a linked list.arrow_forwardBy using JAVA, write a recursive display function, that print a linked list in reverse order.arrow_forwardPlease give me the proper answer with step by step solution Qns:Java question Implement a method that adds a value in a linked list using recursion and using this signature: public void addValue(int value)arrow_forward
- Implement the inordermethod in BST using a stack instead of recursion. Write a test program thatprompts the user to enter 15 integers, stores them in a BST, and invokes theinorder method to display the elements.arrow_forwardIs there a smaller-caller in this method? Does the recursive call to the algorithm involve a smaller case of the original problem? private int recSize(BSTNode<T> node) // Returns the number of elements in subtree rooted at node. { if (node == null) return 0; else return 1 + recSize(node.getLeft()) + recSize(node.getRight()); }arrow_forwardWrite a program to implement BST (Bindary Search Tree) by using linked list. Implement the following methods: 1) Insert 2)Search 3)The traversal method that gives the output in increasing order. Give name of method and implement the method. Code in C.arrow_forward
- Each recursion causes a new activation frame to be placed on the stack.true of false?arrow_forwardHow to answer the following question screenshot shows our binary search tree For the binary search trees, the public method, size, calls the private recursive method, recSize, and passes it a reference to the root of the tree, returning to the caller whatever is returned to it by recSize. Critique the following implementation of recSize assuming its parameter variable is node: if (node == null) return 0; else if ((node.getLeft() == null) && (node.getRight() == null)) return 1; else if ((node.getLeft() == null)) return (1 + recSize(node.getRight())); else return (1 + recSize(node.getLeft())); A. The code will throw a "null pointer exception" under certain circumstances. B. The code will return an incorrect result under certain circumstances. C. The code works correctly but could be simpler. D. The code works correctly. If an application uses the remove method on an Iterator object returned by our binary search tree: A. a TreeUnderflowException is…arrow_forwardWrite a recursive private method called countDegree to be included in class BinaryTree as discussed in the lectures. If a node is having two child nodes, then its degree is two, if it is having one child node, its degree is one and leaf nodes have degree 0. The method counts and returns the total degree of all the nodes in the binary tree. Example: If a binary tree is having 9 nodes such that 3 nodes, each have 2 child nodes, 2 nodes each have only one child and there are 4 leaf nodes. So, the total degree of the binary tree = 3x2 + 2x1 + 0 = 8. This method is called from a public method countDegreeBT, given as follows: public int countDegreeBT() { return countDegree(root); } Method heading: private int countDegree(Node<E> node)arrow_forward
- Answer the given question with a proper explanation and step-by-step solution. Write a recursive method(Code ) static void mirrorTree(node root) This will take a tree as input and then change the tree such that it becomes a mirror of itself. We have the following methods implemented(you can use them and assume we coded somewhere else): getLeft(), getRight(), setLeft(), setRight(), getValue(). All the values in a node are integers. [Hint: Use them, and think how you would write when the tree only has 2 children ] Example:arrow_forwardImplement the complete code Delete that, when given an info value newElement, finds and deletes the first element with this value, if it exists. This involves also considering the four special cases listed for deleting a node from the beginning or the end of a linked list.arrow_forwardWrite a recursive instance method isSorted that takes a Link parameter and determines whether a linked list is sorted in descending order or not (return a boolean value).arrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning