Google Answers Logo
View Question
 
Q: java java java ( No Answer,   0 Comments )
Question  
Subject: java java java
Category: Computers > Programming
Asked by: rejct-ga
List Price: $10.00
Posted: 18 Jun 2003 21:42 PDT
Expires: 18 Jul 2003 21:42 PDT
Question ID: 219078
part of the program is below.... the progran builds a heap from
reading a txt file numbers... the first nuber read is going to be the
size of the array and everything else will be set in the array. then
the prog needs to convert the list into a max heap .....anyhelp would
be great

import java.awt.*;
import java.io.*;

public class Heap {
//used for read in
static int x;
static String [] item;
static int [] IntValue;

//Private Variables
private int count;/*knows how many entries in the array */
private int count_compares;/*knows how many compares have been madein
the heap sort so far */
public static int c;
public static int heapsize;
//public static int A;

//Other Variables
int z =1;
private int[] heap;

// Constructor
 public  Heap(int x) {
   //int A = HeapSize(a[]);
      count =heapsize;
      count_compares = 0;
  }
//public functions

/*causes int to be inserted into heap*/
public void Insert(int c){
	heap[z]= c;
		z++;
	}
//public int Remove();/*removes root of heap and then returns it into
heap structure.*/
public void heapify(int []heap,int heapsize){
int l = leftChild(heapsize);
int r = rightChild(heapsize);
int largest;
if (l <= heapSize(heap)&& heap[l-1] > heap[heapsize-1]){
largest = l;
}
else
{
largest = heapsize;
}
if (r <= heapSize(heap)&& heap[r-1] > heap[largest-1]){
largest = r;
}
if(largest!=heapsize){
//exchancge a[i],a[largest]
int tmp= heap[heapsize-1];heap[heapsize-1]=heap[largest-1];heap[largest-1]=tmp;
heapify(heap,largest);

}
}

public int Compares(){
	count_compares++;
	return count_compares;
	}


/*causes the values of the private variable count_compares to be
returned*/
public int heapSize(int[]heap){
return heap.length;
}

//Private Functions
/*The parent mapping function returns
the index of the parent of the argument*/
private int parent(int i){
	return (i/2);
	}
/*the left hand child mapping function returnsthe
index of the right childof the argument */
private int leftChild(int i){
	return (2*i);
	}
/*the right hand child mapping function returns the
index of the right child of the argument */
private int rightChild(int i){
	return (2*i+1);
	}

/*a recursive function that moves a value
up the child chain until it finds its place*/
//bubbleUp(int i){

/*a recursive function that moves a value
down the child chain until it finds its place*/
//bubbleDown(int i);

// Creates a binary tree storing the value x.
 public static void main(String[] args) throws IOException {
// creates a FileReader object that reads from filename.txt
BufferedReader stdin = new BufferedReader
				(new InputStreamReader(System.in));
	System.out.println("Enter filename");
	 String file = stdin.readLine();
 FileReader fr = new FileReader(file);

  // BufferedReader uses FileReader to retrieve file contents
 BufferedReader br = new BufferedReader(fr);

  // reads in line from the file
 String lineOne = br.readLine();

  //  converts the String to an int
 try
 {
 int lineOneIntValue = Integer.parseInt(lineOne);
   //System.out.println(lineOneIntValue);
 x=lineOneIntValue;
 }
 catch(NumberFormatException n)
 {
  System.out.println("NumberFormatException: " + n.getMessage());
 }

   //System.out.println("blah blah blah"+ x);
   item = new String[x];
   IntValue = new int[x];

   //reads in the rest of the array
   for(int i=0;i<x;i++)
    item[i] = br.readLine();
   /*
   String lineOne = br.readLine();
   */
//saves the integers into an array
 try
 {
 for(int j=0;j<x;j++)
  IntValue[j] = Integer.parseInt(item[j]);
 /*
  int lineTwoIntValue = Integer.parseInt(lineTwo);
 */
 for(int k=0;k<x;k++)
  System.out.println(IntValue[k]);
/*
System.out.println(lineTwoIntValue);
*/
 }
 catch(NumberFormatException n)
 {
  System.out.println("NumberFormatException: " + n.getMessage());
 }
Heap h =new Heap(x); // creates heap obj.
h.Insert( Integer.parseInt(stdin.readLine().trim()));

System.out.println("End of Program");
   }//end-o-main
}//end-o-heap-class
Answer  
There is no answer at this time.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy