Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/maven/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 一般对象的堆_Java_Coding Style - Fatal编程技术网

Java 一般对象的堆

Java 一般对象的堆,java,coding-style,Java,Coding Style,我正在编写一个Heap(Max Heap,意思是Max element是root)类,可以用来“heapify”给定的一组对象。 我知道这个堆的一般结构以及各种算法。 现在对于一般对象,没有定义比较。所以我需要定义两个对象之间的比较。 我的问题是,这个比较函数应该在类堆中还是在类对象中定义?如果我在类堆中定义它,那么对于我使用的每个数据结构,我需要重写比较函数,这是不高效的。这是因为,如果我稍微更改对象,可能最终会在大量位置更改比较。 这件事怎么处理? 多谢各位 class Object{

我正在编写一个Heap(Max Heap,意思是Max element是root)类,可以用来“heapify”给定的一组对象。 我知道这个堆的一般结构以及各种算法。 现在对于一般对象,没有定义比较。所以我需要定义两个对象之间的比较。 我的问题是,这个比较函数应该在类堆中还是在类对象中定义?如果我在类堆中定义它,那么对于我使用的每个数据结构,我需要重写比较函数,这是不高效的。这是因为,如果我稍微更改对象,可能最终会在大量位置更改比较。 这件事怎么处理? 多谢各位

 class Object{
     int value;
     Object (int a) {
         value=a;
     }

     boolean isLessThan(Object a, Object b){
         if (a.value<=b.value){
             return true;
         }
         else return false;
     }
 }

 class Heap{
     Object [] heap=new Object[1000];
     int size=0;
     Heap() {        
     }

     void HeapifyDownwards (int index){
         int left_child=2*index+1;
         int right_child=2*index+2;

         if (size>right_child){
             // both right and left child exist
             Object right= heap[right_child];
             Object left= heap[left_child];
             Object node = heap[index];

             if ((isLessThanEqualTo(right,node)) && (isLessThanEqualTo(left,node))){
                 return;
             }
         }
         else if (size==right_child){
             //only left child exists
         } 
         else {
             // no child exists
         }
     }
 }
类对象{
int值;
对象(INTA){
值=a;
}
布尔值小于(对象a、对象b){
如果(a.valueright\u child){
//左右子项都存在
对象权限=堆[right_child];
对象左=堆[left_child];
对象节点=堆[索引];
if((isLessThanEqualTo(右,节点))&&(isLessThanEqualTo(左,节点))){
返回;
}
}
else if(大小==右\u子项){
//只剩下一个孩子了
} 
否则{
//没有孩子
}
}
}

在Java中处理这些事情的一般方法是:

  • 限制存储在数据结构中的元素以实现可比较的,或
  • 在heap对象中存储一个比较器(在构造时设置)
  • 请看一个例子

    请注意,由于Java是一种(主要是)静态类型的语言,因此对此类数据结构使用泛型非常常见。它们可能值得学习,尽管我可以理解只让heap类为特定类型的对象工作的愿望

    此外,如果本练习的目的是了解堆是如何工作的,那么就太好了。如果您确实想要Java应用程序的优先级队列,Java已经有了一个
    PriorityQueue
    类。

    好! 解决方案是使用可比较对象而不是对象。 然后使用node.compareTo(右)(等)进行比较

    有关更多详细信息,请参阅此链接:

    公共类堆{
    可比[]堆=新可比[1000];
    int size=0;
    堆(){
    }
    公共空间HeapifyDowndown(整数索引){
    int left_child=2*索引+1;
    int right_child=2*索引+2;
    可比右=堆[右\子];
    可比较的左=堆[left_child];
    可比较节点=堆[索引];
    如果(大小>右侧子项){
    //左右子项都存在
    if((node.compareTo(右)>0)和&(node.compareTo(左)>0)){
    返回;
    }
    else if((右.比较(节点)>0)和&(右.比较(左)>0)){
    可比温度=右;
    堆[右\u子]=节点;
    堆[索引]=temp;
    HEAPIFYDOWN(右_儿童);
    }
    else if((左比较(节点)>0)和&(左比较(右)>0)){
    可比温度=左侧;
    heap[left_child]=节点;
    堆[索引]=temp;
    heapifydown(左儿童);
    }
    }
    else if(大小==右\u子项){
    //只剩下一个孩子了
    如果(左。与(节点)>0相比){
    可比温度=左侧;
    heap[left_child]=节点;
    堆[索引]=temp;
    heapifydown(左儿童);
    }
    else{return;}
    } 
    否则{
    返回;
    }
    }
    公共无效堆向上(整数索引){
    int parent_index=(index-1)/2;
    可比较父级=堆[父级索引];
    可比较节点=堆[索引];
    如果(节点比较到(父节点)>0){
    可比温度=父母;
    堆[parent_index]=节点;
    堆[索引]=temp;
    HEAPIFYUP(母指数);
    }
    否则{
    返回;
    }
    }
    公共空白插入(可比){
    堆[size]=in;
    大小++;
    向上倾斜(尺寸1);
    }
    公众可比删除(){
    可比输出=堆[0];
    堆[0]=堆[size-1];
    大小--;
    heapifydown(0);
    返回;
    }
    }
    公共类TestObject实现了可比较的{
    int值;
    测试对象(INTA){
    值=a;
    }
    @凌驾
    公共整数比较(对象b){
    if(this.getClass()==b.getClass()){
    TestObject b_test=(TestObject)b;
    
    如果(this.values花了我五分钟才意识到,您所说的
    对象
    不是
    java.lang.Object
    。我投票赞成重命名您的类;)如果你想比较类实例,那么你应该实现
    Comparable
    ,而不是定义和实现你自己的方法。这很快就会得到回报。我刚刚用class对象来实现比较。我的基本意思是问我是否使用通用的“Object”实现堆那么我如何使用比较呢?很抱歉,答案仍然不清楚。我可以轻松地为任何常规对象实现堆栈。之所以容易是因为不需要比较。但是堆实现需要比较。假设我有一些对象,我需要使用堆数据结构。我会吗(假设优先级任务没有内置)必须创建特定于对象的堆?我正要回答这个问题,这时我看到你给出了自己的答案。将堆限制为实现<代码>可比(我的选项#1)的对象是最简单的方法。
    public class Heap{
        Comparable [] heap=new Comparable[1000];
        int size=0;
        Heap() {        
        }
        public void HeapifyDownwards (int index){
            int left_child=2*index+1;
            int right_child=2*index+2;
    
            Comparable right= heap[right_child];
            Comparable left= heap[left_child];
            Comparable node = heap[index];
    
            if (size>right_child){
                // both right and left child exist
                if ((node.compareTo(right)>0) && (node.compareTo(left)>0)){
                    return;
                }
                else if ((right.compareTo(node)>0) && (right.compareTo(left)>0)){
                    Comparable temp=right;
                    heap[right_child]=node;
                    heap[index]=temp;
                    HeapifyDownwards(right_child);
                }
                else if ((left.compareTo(node)>0) && (left.compareTo(right)>0)){
                    Comparable temp=left;
                    heap[left_child]=node;
                    heap[index]=temp;
                    HeapifyDownwards(left_child);
                }
            }
            else if (size==right_child){
                //only left child exists
                if (left.compareTo(node)>0){
                    Comparable temp=left;
                    heap[left_child]=node;
                    heap[index]=temp;
                    HeapifyDownwards(left_child);
                }
                else {return;}
            } 
            else {
                return;
            }
        }
        public void HeapifyUpwards (int index){
            int parent_index=(index-1)/2;
    
            Comparable parent= heap[parent_index];
            Comparable node = heap[index];
    
            if (node.compareTo(parent)>0){
                Comparable temp= parent;
                heap[parent_index]=node;
                heap[index]=temp;
                HeapifyUpwards(parent_index);
            }
            else{
                return;
            }
        }
        public void Insert (Comparable in){
            heap[size]=in;
            size++;
            HeapifyUpwards(size-1);
        }
        public Comparable Remove (){
            Comparable out=heap[0];
            heap[0]=heap[size-1];
            size--;
            HeapifyDownwards(0);
            return out;
        }
    }
    
    
    public class TestObject implements Comparable{
        int value;
        TestObject (int a) {
            value=a;
        }
    
        @Override
        public int compareTo (Object b){
             if (this.getClass() == b.getClass()){
                TestObject b_test = (TestObject) b;
                if (this.value<b_test.value){
                    return -1;
                }
                else if(this.value==b_test.value){
                    return 0;
                }
                else return 1;
             }
             else return -1;
        }
    
        public void Print (){
            System.out.println(value);
        }
    }