Question: Suppose that you have a linked list that stores sections and subsections of a document. The next pointer points the next section, and the sub
Suppose that you have a linked list that stores sections and subsections of a document. The next pointer points the next section, and the sub pointer points to the first subsection of this section. The nodes are represented as below: private static class Node { public String data; public Node next; public Node sub; public Node(String value) { data = value; // next and sub by default are null } } (20 points) Write a recursive function that flattens the document based on the Node references, so that all of the Nodes appear in order, and all sub fields are null. The function should take as an argument a Node, and return the Node that is the first node in the flattened list. For example, consider the document where represented by the picture below:
public static Node flatten (Node first) { }
what should I write in the middle part?
and please tell me the time complexity of this algorithm.
Thank you
(20 points) Write a recursive function that flattens the document based on the Node references, so that all of the Nodes appear in order, and all sub fields are null. The function should take as an argument a Node, and return the Node that is the first node in the flattened list. For example, consider the document where represented by the picture below: data: "Section 1" data: "Section 2" data: "Section 3" next: next: next: null sub: sub: null sub: data: "Section 3.1" data: "Section 1.1" data: "Section 1.2" data: "Section 1.3" next: null next: next: - next: null sub: null sub: null sub: 1 sub: null data: "Section 1.2.1" data: "Section 1.2.2" next: next: null sub: null sub: null The flattened list would look like: data: "Section 1" data: "Section 1.1" data: "Section 1.2" data: "Section 1.2.1" data: "Section 12.2" data: "Section 1.3 data: "Section 2 data: "Section 3 data: "Section 3.1" next: next: next: next: next next: next: next: next: null sub: sub: null sub: null sub: null sub: null sub: null sub: null sub: null sub: null
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
