Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help getting this Java program to run. import java.util.*; public class DrawTree { public String[] draw(int[] parents, String[] names) { int rootIndex =

I need help getting this Java program to run.

import java.util.*;

public class DrawTree { public String[] draw(int[] parents, String[] names) { int rootIndex = findRoot(parents); List result = new ArrayList<>(); drawSubtree(parents, names, rootIndex, "", result); return result.toArray(new String[result.size()]); } private int findRoot(int[] parents) { for (int i = 0; i < parents.length; i++) { if (parents[i] == -1) { return i; } } return -1; // invalid input } private void drawSubtree(int[] parents, String[] names, int rootIndex, String prefix, List result) { result.add(prefix + "+-" + names[rootIndex]); List children = new ArrayList<>(); for (int i = 0; i < parents.length; i++) { if (parents[i] == rootIndex) { children.add(i); } } children.sort(Comparator.comparingInt(i -> i)); for (int child : children) { drawSubtree(parents, names, child, prefix + " |", result); } } }

//here is the class DrawTree drawTree = new DrawTree(); int[] parents = {-1, 0, 1, 1, 0, 0, 5, 5}; String[] names = {"Root", "SubB", "LEAF1", "LEAF2", "LEAF3", "SubA", "LEAF4", "LEAF5"}; String[] result = drawTree.draw(parents, names); for (String line : result) { System.out.println(line); }

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

Datacasting How To Stream Databases Over The Internet

Authors: Jessica Keyes

1st Edition

007034678X, 978-0070346789

More Books

Students also viewed these Databases questions

Question

Question Can a Roth IRA invest in stock of the IRA owners business?

Answered: 1 week ago