Question: This is a JAVA program on recursion 1. Define a class called Node. It has two attributes: - ID (a string) - next (a Node)

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 Nodes 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 nodes ID, it checks 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 Nodes 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 Nodes 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.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!