Please modify my code into Java language because my professor is
NOT accepting C++ code for my assignment. Here's the question:
Program 3: Serialize a tree
Given the NTree class, write
methods serialize and deserialize, which write the
tree to a text file and read it back.
Compile and test your code.
"foodtree.out" gives an example of a serialized format that is
inefficient in time and space. Implement a better version for extra
credit.
Here's the given Ntree class.
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();
}
}
}
Here's the foodtree.out:
Food
Food Plant
Food Animal
Food Plant Roots
Food Plant Leaves
Food Plant Fruits
Food Animal Fish
Food Animal Mammals
Food Animal Birds
Food Plant Roots Potatoes
Food Plant Roots Carrots
Food Plant Leaves Lettuce
Food Plant Leaves Cabbage
Food Plant Fruits Apples
Food Plant Fruits Pears
Food Plant Fruits Plums
Food Plant Fruits Oranges
Food Animal Fish Salmon
Food Animal Fish Tuna
Food Animal Mammals Beef
Food Animal Mammals Lamb
Food Animal Birds Chicken
Food Animal Birds Duck
Food Animal Fish Salmon Wild
Food Animal Fish Salmon Farm
Food Plant Fruits Apples GrannySmith
Food Plant Fruits Apples Gala
And here's my code that needs to be modified into Java
language:
// A C program to demonstrate serialization and deserialization
of
// Binary Tree
#include <stdio.h>
#define MARKER -1
/* A binary tree Node has key, pointer to left and right
children */
struct Node
{
int key;
struct Node* left, *right;
};
/* Helper function that allocates a new Node with the
given key and NULL left and right pointers. */
Node* newNode(int key)
{
Node* temp = new Node;
temp->key = key;
temp->left = temp->right = NULL;
return (temp);
}
// This function stores a tree in a file pointed by fp
void serialize(Node *root, FILE *fp)
{
// If current node is NULL, store marker
if (root == NULL)
{
fprintf(fp, "%d ", MARKER);
return;
}
// Else, store current node and recur for its
children
fprintf(fp, "%d ", root->key);
serialize(root->left, fp);
serialize(root->right, fp);
}
// This function constructs a tree from a file pointed by
'fp'
void deSerialize(Node *&root, FILE *fp)
{
// Read next item from file. If there are no more
items or next
// item is marker, then return
int val;
if ( !fscanf(fp, "%d ", &val) || val ==
MARKER)
return;
// Else create node with this item and recur for
children
root = newNode(val);
deSerialize(root->left, fp);
deSerialize(root->right, fp);
}
// A simple inorder traversal used for testing the constructed
tree
void inorder(Node *root)
{
if (root)
{
inorder(root->left);
printf("%d ", root->key);
inorder(root->right);
}
}
/* Driver program to test above functions*/
int main()
{
// Let us construct a tree shown in the above
figure
struct Node *root = newNode(20);
root->left
= newNode(8);
root->right
= newNode(22);
root->left->left
= newNode(4);
root->left->right =
newNode(12);
root->left->right->left = newNode(10);
root->left->right->right = newNode(14);
// Let us open a file and serialize the tree into
the file
FILE *fp = fopen("tree.txt", "w");
if (fp == NULL)
{
puts("Could not open file");
return 0;
}
serialize(root, fp);
fclose(fp);
// Let us deserialize the stored tree into
root1
Node *root1 = NULL;
fp = fopen("tree.txt", "r");
deSerialize(root1, fp);
printf("Inorder Traversal of the tree constructed
from file:\n");
inorder(root1);
return 0;
}
Please modify my code into Java language because my professor is NOT accepting C++ code for my assignment. Here's the qu
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am