Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

COMPLETE THE SERIALIZE AND DESERIALIZE METHODS ONLY import java.util.*; public class NTree { protected class Node { E data; Node parent; List children; protected Node(E

COMPLETE THE SERIALIZE AND DESERIALIZE METHODS ONLY

import java.util.*;

public class NTree {

protected class Node { E data; Node parent; List children;

protected Node(E data) { this.data = data; this.children = new ArrayList(); }

protected void addChild(Node c) { children.add(c); } public boolean equals(Node rhs) { return this.data.equals(rhs.data); } }

protected Node root;

public NTree() {}

public NTree(List values, List parents) throws Exception { if (values.size() != parents.size()) throw new Exception(); Map m = new TreeMap<>(); for (int i = 0; i < values.size(); i++) { Node nd = new Node(values.get(i)); m.put(values.get(i), nd); if (parents.get(i) >= 0) { // -1 signals root nd.parent = m.get(values.get(parents.get(i))); nd.parent.addChild(nd); } else root = nd; } }

public boolean equals(NTree rhs) { return equals(root, rhs.root); }

protected boolean equals(Node lhs, Node rhs) { if (lhs == null || rhs == null) return lhs == rhs; if (!lhs.equals(rhs) || lhs.parent != rhs.parent) return false; for (int i = 0; i < lhs.children.size(); i++) { if (!equals(lhs.children.get(i), rhs.children.get(i))) return false; } return true; }

public void serialize(String fname) {}

public void deserialize(String fname) {}

public static void main(String [] args) { try { List food = Arrays.asList("Food", "Plant", "Animal", "Roots", "Leaves", "Fruits", "Fish", "Mammals", "Birds", "Potatoes", "Carrots", "Lettuce", "Cabbage", "Apples", "Pears", "Plums", "Oranges", "Salmon", "Tuna", "Beef", "Lamb", "Chicken", "Duck", "Wild", "Farm", "GrannySmith", "Gala"); List foodparents = Arrays.asList(-1, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 5, 5, 5, 5, 6, 6, 7, 7, 8, 8, 17, 17, 13, 13); NTree foodtree = new NTree(food, foodparents);

foodtree.serialize("foodtree.out"); NTree foodtree2 = new NTree<>(); foodtree2.deserialize("foodtree.out");

System.out.println(foodtree.equals(foodtree2));

List intvalues = Arrays.asList(9, 6, 5, 4, 2, 10, 7, 1, 3, 8, 11, 12, 13, 14); List intparents = Arrays.asList( -1, 0, 1, 1, 1, 2, 2, 2, 3, 3, 8, 8, 8, 8); NTree inttree = new NTree<>(intvalues, intparents);

NTree inttree2 = new NTree<>(); inttree.serialize("inttree.out"); inttree2.deserialize("inttree.out"); System.out.println(inttree.equals(inttree2)); } catch (Exception e) { e.printStackTrace(); } }

}

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_2

Step: 3

blur-text-image_3

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

Java How To Program Late Objects Version

Authors: Paul Deitel, Deitel & Associates

8th Edition

0136123716, 9780136123712

More Books

Students also viewed these Programming questions