// Start main()================================================================ public static void main(String args[]) throws FileNotFoundException { Node[] firstTenThousand = new Node[10000]; // Stores the first ten thousand entries from input PrintWriter Out = new PrintWriter("TopTenThousand.txt"); // Ready for output to file int inputValue; // Stores each input value one at a time int count = 0; // Track line number of input values Scanner readFile = null; if (args.length > 0) { readFile = new Scanner(new File(args[0])); // Open Input File } else // No Input Given { System.out.println("Proper input file was not given as command line perameter"); System.out.println("Example: java FindTopTenThousand data.txt"); System.exit(-1); } // Read in first ten thousand values for (int i = 0; i < 10000 && readFile.hasNextInt(); i++) { inputValue = readFile.nextInt(); count++; firstTenThousand[i] = new Node(inputValue, count); } // End for loop MinHeap heapArray = new MinHeap(firstTenThousand); // Turn first then thousand values into a min heap // Read all of the remaining entries in input file while (readFile.hasNextInt()) { inputValue = readFile.nextInt(); count++; // If new value is larger than smallest of current values if (heapArray.heap[0].key < inputValue) { heapArray.exchangeSmallest(inputValue, count); // Exchange and heapify } } // End while loop readFile.close(); // Close input file Out.println("Ten thousand highest numbers from input file:"); // Formating for output file Out.println(); // Print final results stored in heap for (int i = 0; i < 10000; i++) { Out.println( "Number From Input File: " + heapArray.heap[i].key + " Input File Row Index: " + heapArray.heap[i].lineIndex); } // End for loop Out.close(); // Close output file } // End main()
/** * Start building tree method obtained from canvas * * @return root */ private HuffTree buildTree() { HuffTree tmp1, tmp2, tmp3 = null; while (Heap.heapsize() > 1) { // While two items left tmp1 = (HuffTree) Heap.removemin(); tmp2 = (HuffTree) Heap.removemin(); tmp3 = new HuffTree(tmp1.root(), tmp2.root(), tmp1.weight() + tmp2.weight()); Heap.insert(tmp3); // Return new tree to heap tmp1.setCode(""); tmp2.setCode(""); tmp3.setCode(""); } return tmp3; // Return the tree }
public static void main(String[] args) { // Default values int numInputFiles = 2; String tempDirectory = System.getProperty("user.dir"); String outputDirectory = ""; String inputDirectory = ""; // Readers BufferedReader inputReader = null; BufferedReader[] bufferedReaders = null; FileReader fr = null; // Writers BufferedWriter[] bufferedWriters = null; int currentFile = 0; // Files for input/output of runs File[] inputs = null; File[] outputs = null; int comparisonsCount = 0; int runsCount = 0; int passesCount = 0; try { System.out.println(); // Get the arguments if they exist if (args.length == 1) { inputDirectory = args[args.length - 1]; } else if (args.length > 1) { for (int i = 0; i < args.length; i++) { if (args[i].equals("-k")) { if (isNumeric(args[i + 1])) { // Check for any value above 2 (the default) if (Integer.parseInt(args[i + 1]) > 2) { // Get the number of input files numInputFiles = Integer.parseInt(args[i + 1]); } } } else if (args[i].equals("-t")) { tempDirectory = args[i + 1]; } else if (args[i].equals("-o")) { outputDirectory = args[i + 1]; } } System.out.println(args[args.length - 2]); if (!args[args.length - 2].contains("-")) { inputDirectory = args[args.length - 1]; } } System.out.println("Number of input files:" + numInputFiles); System.out.println("Temporary directory:" + tempDirectory); System.out.println("Output directory:" + outputDirectory); System.out.println("Input directory:" + inputDirectory); System.out.println(); // Create array of temp Files inputs = new File[numInputFiles]; // Need the same number of writers as input files bufferedWriters = new BufferedWriter[numInputFiles]; bufferedReaders = new BufferedReader[numInputFiles]; String filePrefix = "tmp_"; // Create the array of input files for (int i = 0; i < numInputFiles; i++) { inputs[i] = new File(tempDirectory + "/" + filePrefix + i + ".txt"); inputs[i].createNewFile(); bufferedWriters[i] = new BufferedWriter(new FileWriter(inputs[i].getAbsoluteFile())); } inputReader = new BufferedReader(new FileReader(inputDirectory)); // Start creating the runs String[] run = new String[15]; for (int i = 0; i < 15; i++) { // Put the first 15 elements from raw data into the array if (inputReader.ready()) run[i] = inputReader.readLine(); } // Build the priority queue by passing it the unsorted array MinHeap heap = new MinHeap(run); System.out.print("initial unsorted heap:"); heap.traverse(); heap.heapify(); System.out.print("sorted heap:"); heap.traverse(); // Count on the temp file which we are writing to int tempWriteCounter = 0; String line = ""; boolean endOfStream = false; // While the heap is not empty while (!heap.isEmpty() && !endOfStream) { // Write minimum to output String min = heap.remove(); System.out.println("wrote: " + min + " to output"); bufferedWriters[tempWriteCounter].write(min); bufferedWriters[tempWriteCounter].newLine(); // Make sure reader is ready if (inputReader.ready() && !endOfStream) { // If there are more elements to read in // if((line = inputReader.readLine()) != null){ line = inputReader.readLine(); System.out.println("read in: " + line); // If the new input is smaller than the last element output to file, // add it to the dead space of the heap using .append(line); if (MinHeap.precedesValue(line, min)) { System.out.println(line + " is less than " + min + ", so append " + line); heap.append(line); } else { System.out.println(line + " is greater than " + min + ", so insert " + line); heap.insert(line); } } else { endOfStream = true; System.out.println("Reached end of stream-1"); } heap.traverse(); // If the heap is empty AND there are more elements to read in, // fill the heap up, heapify()and write to next temp file if (heap.isEmpty() && !endOfStream) { System.out.println("HEAP IS NOW EMPTY... begin next run"); // Find out how many new elements need to be added to fill the heap int numElements = heap.getEmptyCells(); // Create the array of appropriate size String[] remaining = new String[numElements]; System.out.println("number of new elements needed: " + numElements); // Index for the new array int i = 0; // While there are more elements to read in AND there are still empty spaces in the heap while (!endOfStream && i < numElements) { if (inputReader.ready()) { line = inputReader.readLine(); System.out.println("read in: " + line); remaining[i] = line; } else { endOfStream = true; System.out.println("Reached end of stream-2"); } } // Fill the empty spaces in the heap heap.fill(remaining); heap.heapify(); heap.traverse(); // Proceed to the next temp file tempWriteCounter++; } } inputReader.close(); // Close the writers for (int i = 0; i < bufferedWriters.length; i++) { bufferedWriters[i].close(); // bufferedReaders[i].close(); } System.out.println(); } catch (Exception e) { System.err.println("Error: " + e.getMessage()); } // System.out.println(); // Priority Queue MinHeap minHeap = new MinHeap(); // Stuff to show that insert() works /* minHeap.insert("d"); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.insert("b"); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.insert("e"); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.insert("c"); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.insert("a"); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.remove(); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.remove(); System.out.println(); minHeap.traverse(); System.out.println(); minHeap.remove(); System.out.println(); minHeap.traverse(); System.out.println(); */ // Stuff to show that heapify() works /* String[] array = new String[14]; array[0] = "n"; array[1] = "i"; array[2] = "e"; array[3] = "l"; array[4] = "b"; array[5] = "h"; array[6] = "d"; array[7] = "a"; array[8] = "k"; array[9] = "o"; array[10] = "j"; array[11] = "c"; array[12] = "f"; array[13] = "g"; minHeap = new MinHeap(array); minHeap.traverse(); System.out.println(); minHeap.heapify(); minHeap.traverse(); */ System.out.println(); }