Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Error sorting vertices using a maximising heap

Posted on 2004-04-11
3
Medium Priority
?
218 Views
Last Modified: 2013-11-23
Hi,
      I'm attempting to sort some 3D shapes by inserting their vertices into a max-heap and then removing the maximum value until the vertices are in descending order.  But so far i've encountered three errors. Please take a look at my code and the error messages and tell me what you think.

public class OcclusionPipeline extends Mesh implements RenderPipeline{
    Point3D[] pts;
    private int size;
    public int capacity(){return(pts.length);}
    public int size(){return(size);}
   
 
    public OcclusionPipeline(Point3D[] pts){
    super(pts);
   
    pts = new Point3D[2];
    size = 0;
       
}
   
   public OcclusionPipeline(int initialCapacity)
   {
         pts = new Point3D[initialCapacity];
         size = 0;
         }
         
   private int parent(int i)
      {
            return ((i - 1) / 2);
      }

      private int left(int i)
      {
            return (i * 2 + 1);
      }

      private int right(int i)
      {
            return (i * 2 + 2);
      }

      
      private void swap(int i, int j)
      {
            Point3D temp;

            temp   = pts[i];
            pts[i] = pts[j];
            pts[j] = temp;
      }

      
      private void heapify(int i)
      {
            int leftChild  = left(i);
            int rightChild = right(i);
            int greatest   = i;


            
            if(leftChild < size() && pts[leftChild] > pts[i])
                    greatest = leftChild;

            if (rightChild < size() && pts[rightChild] > pts[greatest])
                  greatest = rightChild;

            if (greatest != i) {
                  swap(i, greatest);
                  heapify(greatest);
            }
      }


  public void deleteMax()
      {
            
            if (size() > 0) {
                  pts[0] = pts[--size];
                  heapify(0);

                  if (size() * 2 < capacity() && size() > 2) {
                        Point3D[] tmp = new Point3D[size()];

                        for (int i = 0; i < tmp.length; i++)
                              tmp[i] = pts[i];

                        pts = tmp;
                  }
            }
            else {
                  System.err.println("deleteMax failed");
            }
      }

      
      public void insert(Point3D v_item)
      {
            int current;

            
            if (size == capacity()) {
                  Point3D[] tmp = new Point3D[2 * pts.length];

                  for (int i = 0; i < pts.length; i++)
                        tmp[i] = pts[i];

                  pts = tmp;
            }

            
            current = size++;

            
            while (current != 0 && pts[parent(current)] < v_item) {
                  
                  pts[current] = pts[parent(current)];

                  
                  current = parent(current);
            }

            
            pts[current] = v_item;
      }

      
      public Point3D max()
      {
            if (size > 0)
                  return pts[0];

            System.err.println("max failed");
            
      }

The javac compiler gives the following error messages:

Operator > cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            if(leftChild < size() && pts[leftChild] > pts[i])
                                            ^

Operator > cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            if (rightChild < size() && pts[rightChild] > pts[greatest])
                                                 ^

Operator < cannot be applied to g3d.geom.Point3D, g3d.geom.Point3D
            while (current != 0 && pts[parent(current)] < v_item)
0
Comment
Question by:capbry
  • 2
3 Comments
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 10803793
ypou cannot use the < and > operators with your Point3D instances.
You need to instead compare the x, y, z values assciated with that object.
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 700 total points
ID: 10803898
I'm assuming that Point3D is your own class? You must be having some getX (), getY (), getZ () methods (or some ways to access the co-ordinates)? Elaborating a little more on what objects said, you should use:

if ( rightChild.getX () < size () && .... so on
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 10839220
Glad to help ;-)

BTW, why a C :-( you could've asked for more help if you wanted clarification.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question