Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

image text in transcribed 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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions