Link to home
Start Free TrialLog in
Avatar of capbry
capbry

asked on

Error sorting vertices using a maximising heap

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)
SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Glad to help ;-)

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