Question: Solve using JAVA and also show the output: (Please read direction) Problem 3: getAncestry method RootedTree.java (Problem 3 isfound in this code) import java.io.IOException ;

Solve using JAVA and also show the output: (Please read direction)

Problem 3: getAncestry method

RootedTree.java (Problem 3 isfound in this code)

import java.io.IOException;

import java.util.ArrayList;

import java.util.List; public class RootedTree { private final String PADDING_STRING = "0-"; public TreeNode<String> root = null; public RootedTree(TreeNode<String> node) { this(); root = node; node.parent = null; } public RootedTree(String filename) throws IOException { this(); load(filename); } public RootedTree() { } public void save(String filename) throws IOException { LineWriter writer = new LineWriter(); writer.open(filename); recursiveSave(writer, "", root); writer.close(); } private void recursiveSave(LineWriter writer, String padding, TreeNode<String> current) { if(current != null) { writer.println(padding + current.element.toString()); for(TreeNode<String> child : current.children) { recursiveSave(writer, padding + PADDING_STRING, child); } } } public void load(String filename) throws IOException { LineReader reader = new LineReader(); reader.open(filename); root = new TreeNode<String>(reader.next()); loadRecursively(reader, root, 0); reader.close(); } private void loadRecursively(LineReader reader, TreeNode<String> current, int level) throws IOException { while(reader.hasNext()) { String line = reader.peek(); String[] split = line.split(PADDING_STRING); int newLevel = split.length - 1; String newString = split[newLevel]; if(newLevel > level + 1 || newLevel <= 0) { throw new IOException("Invalid data format."); } else if(newLevel == level + 1) { TreeNode<String> node = new TreeNode<String>(newString); current.addChild(node); reader.next(); loadRecursively(reader, node, newLevel); } else { return; } } } // Problem 3 (20 pts): Fill in the getAncestry method. This method should return // a list that represents a path of nodes from a node whose label is equal to  // element to the root node. // Note: You are allowed to add helper methods, use recursion, and use the  // SimpleStack class. public List<TreeNode<String>> getAncestry(String element){ return null; } }

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!