Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a JAVA program on recursion 1. Define a class called Node. It has two at

ID: 3903065 • Letter: T

Question

This is a JAVA program on recursion

1. Define a class called Node. It has two attributes: - ID (a string) - next (a Node) When a new Node is created only set its ID. The next attribute should be set to null. Also write a print method that prints the Node’s ID to the console.

2. Write code that create a chain of 5 Nodes. First create 5 new nodes called n1 (id = A), n2 (id = B), n3 (id = C) etc. Add a method called addNext to your Node class that has one parameter (a Node). It set the next attribute to that parameter. Write code that uses the addNext method to join your 5 nodes into a chain: A -> B -> … E.

3. Modify your print method so that after it prints the node’s ID, it check’s whether there is a next Node, if there is a next node, it then prints that node (using the same modified print method). This is the recursive step. Execute your modified print method on E, it should just print E, then run it on A, it should print all the nodes. You will now repeat the above steps for a binary tree. Start a new project.

4. Define a class called node. It has three attributes: - ID ( s string) - left (a Node) - right (a Node) This time ‘next’ has been replaced by ‘left’ and ‘right’. Again write code that sets the ID with a parameter and sets left & right to null. Also write a print method that prints the Node’s ID to the console.

5. Write code that create a binary tree. You will need to create the Nodes there wire them up using addLeft and addRight methods.

6. Modify your print method so that it is recursive i.e. printing the root node will print the whole tree. Write test code that prints a leaf node, then prints the root node. You will now repeat the above steps for an arbitrary tree. Start a new project.

7. Define a class called node. It has three attributes: - ID ( s string) - children (a list of Node) This time ‘next’ has been replaced by a list of nodes. Again write code that sets the ID with a parameter and sets children to an empty list. Also write a print method that prints the Node’s ID to the console.

8. Write code that create an arbitrary tree. You will need to create the Nodes there wire them up using your addChild method.

9. Modify your print method so that it is recursive i.e. printing the root node will print the whole tree. Write test code that prints a leaf node, then prints the root node.

10. Extend your Node class so it has an extra attribute called ‘value’. Extend the constructor so that when the ID is set the value is also set. Add a recursive method called totalValue that returns the value of the current node and all nodes below it.

Explanation / Answer

I am posting the answers of 1st 3 part here.I will add other parts soon

Below is the JAVA code I hope that i have provided sufficient comments for your better understanding

class LinkedList
{
Node root;  

static class Node
{
string data;
Node next;
  
//constructor
Node(string s)
{
data=s;
next=NULL;
}
}

//recursive function is used to print the contents of the list
//This will print the list from "E" to "A"
public static void traversal(LN l)
{
if(l!=NULL)
{
traversal(l.next);
System.out.println(l.data);
}
}

public static void main(String[] args)
{
LinkedList head = new LinkedList();
  
//Add 5 nodes with a value from "A" to "E"
  
//root contains "A"
head.root=new Node("A");
Node second=new Node("B");
Node third=new Node("C");
Node fourth=new Node("D");
Node fifth=new Node("E");
  
//connect the individual nodes
head.root.next=second;
second.next=third;
third.next=fourth;
fourth.next=fifth;
  
//print the list
traversal(head);
}
}


Hope i have answered your question satisfactorily.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote