Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a java prog using heapsort for sorting a list of inputs stored in entry.t

ID: 3681070 • Letter: C

Question

Create a java prog using heapsort for sorting a list of inputs stored in entry.txt file. Print the output of preordertraversal of the heap that you created and the sorted list.

Hint: Use HeapPriorityQueue code to create the heap we needed. Either use AbstractTree code to implement preorder traversal or use this algorithm yourself.

Input:  The common input for this program which is in the entry.txt is as following:

<3, A> <8, Z> <12, B> <9, M> <17, T> <31, U> <13, D> <24, E> <9, L> <7, B> <1, N> <5, G>

Output: For the given input, the corresponding output is:

Preorder:

<1, N> <3, A> <9, M> <24, E> <9, L> <7, B> <17, T> <8, Z> <5, G> <12, B> <31, U> <13, D>

Sorted List:

<1, N> <3, A> <5, G> <7, B> <8, Z> <9, L> <9, M> <12, B> <13, D> <17, T> <24, E> <31, U>

Please provide an executable java code for the above question. Please Include a working .java file if you can, Thanks a lot.

Explanation / Answer

Using TreeMap :-

package com.app;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.util.Map; import java.util.TreeMap;

public class HeapSortProgram { public static void main(String[] args) throws IOException,ClassNotFoundException {

TreeMap tmap=new TreeMap();

tmap.put(3, "A");

tmap.put(8, "z");

tmap.put(12, "B");

tmap.put(9, "M");

tmap.put(17, "T");

tmap.put(31, "U");

tmap.put(13, "D");

tmap.put(24, "E");

tmap.put(9, "L");

tmap.put(7, "B");

tmap.put(1, "N");

tmap.put(5, "G");

for(Map.Entry m:tmap.entrySet())

{

System.out.println(m.getKey()+" "+m.getValue());

}

FileOutputStream fout=new FileOutputStream("C:/Users/MR_DELL/Downloads/entry1.txt");

ObjectOutputStream out=new ObjectOutputStream(fout);

out.writeObject(tmap);

out.close();

FileInputStream fis = new FileInputStream("C:/Users/MR_DELL/Downloads/entry1.txt");

ObjectInputStream ois = new ObjectInputStream(fis);

Map anotherList = (Map) ois.readObject();

ois.close();

System.out.println("**************"); System.out.println(anotherList);

}

}

1 N
3 A
5 G
7 B
8 z
9 L
12 B
13 D
17 T
24 E
31 U
**************
{1=N, 3=A, 5=G, 7=B, 8=z, 9=L, 12=B, 13=D, 17=T, 24=E, 31=U}

2nd Method :-

in 2 nd Method i didn't took key value because time is over that time . so otherwise i will place defnetly .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote