Apache的小顶堆实现

package  org.apache.solr.util;
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import java.util.Arrays;

/** A native long priority queue.
 * 小顶堆实现的优先级队列,从1开始的。top()=heap[1];
 * @lucene.internal
*/
public class LongPriorityQueue {
	
	public static void main(String[] args) {
		LongPriorityQueue heap = new LongPriorityQueue(3, 3, 0) ;
		heap.add(1) ;
		heap.add(2) ;
		heap.add(3) ;
		//prt(heap);
		heap.insert(4) ;
		
		for(long l : heap.heap)
			System.out.println( l );
		System.out.println("---- ");
		prt(heap);
	}
	public static void prt(LongPriorityQueue heap){
		while( heap.size() !=0){
			System.out.print ("\t"+ heap.pop() );
		}
		System.out.println();
	}
	
  protected int size;             // number of elements currently in the queue
  protected int currentCapacity;  // number of elements the queue can hold w/o expanding
  protected int maxSize;          // max number of elements allowed in the queue 
  protected long[] heap;
  protected final long sentinel;   // represents a null return value

  public LongPriorityQueue(int initialSize, int maxSize, long sentinel) {
    this.maxSize = maxSize;
    this.sentinel = sentinel;
    initialize(initialSize);
  }

  protected void initialize(int sz) {
    int heapSize;
    if (0 == sz)
      // We allocate 1 extra to avoid if statement in top()
      heapSize = 2;
    else {
      // NOTE: we add +1 because all access to heap is
      // 1-based not 0-based.  heap[0] is unused.
      heapSize = Math.max(sz, sz + 1); // handle overflow,处理溢出问题
    }
    heap = new long[heapSize];
    currentCapacity = sz;
  }

  public int getCurrentCapacity() {
    return currentCapacity;
  }

  public void resize(int sz) {
    int heapSize;
    if (sz > maxSize) {
      maxSize = sz;
    }
    if (0 == sz)
      // We allocate 1 extra to avoid if statement in top()
      heapSize = 2;
    else {
      heapSize = Math.max(sz, sz + 1); // handle overflow
    }
    heap = Arrays.copyOf(heap, heapSize);
    currentCapacity = sz;
  }
  /**
   * Adds an object to a PriorityQueue in log(size) time.
   * It returns the smallest object (if any) that was
   * dropped off the heap减少 because it was full, or
   * the sentinel value.
   *
   *  This can be
   * the given parameter (in case it is smaller than the
   * full heap's minimum, and couldn't be added), or another
   * object that was previously the smallest value in the
   * heap and now has been replaced by a larger one, or null
   * if the queue wasn't yet full with maxSize elements.
   */
  public long insertWithOverflow(long element) {
    if (size < maxSize) {
      add(element);
      return sentinel;
    } else if (element > heap[1]) {//越大的数往里插入
      long ret = heap[1];
      heap[1] = element;
      updateTop();
      return ret;
    } else {
      return element;
    }
  }

  /** inserts the element and returns true if this element caused another element
   * to be dropped from the queue. */
  public boolean insert(long element) {
    if (size < maxSize) {
      add(element);
      return false;
    } else if (element > heap[1]) {
      // long ret = heap[1];
      heap[1] = element;
      updateTop();
      return true;
    } else {
      return false;
    }
  }

  /** Removes and returns the least element of the PriorityQueue in log(size)
    time.  Only valid if size() > 0.
   */
  public long pop() {
    long result = heap[1];            // save first value
    heap[1] = heap[size];            // move last to first
    size--;
    downHeap();          // adjust heap
    return result;
  }
  
  /**
   * Should be called when the Object at top changes values.
   * @return the new 'top' element.
   */
  public long updateTop() {
    downHeap();
    return heap[1];
  }

  /** Returns the number of elements currently stored in the PriorityQueue. */
  public int size() {
    return size;
  }

  /** Returns the array used to hold the heap, with the smallest item at array[1]
   *  and the last (but not necessarily largest) at array[size()].  This is *not*
   *  fully sorted.
   */
  public long[] getInternalArray() {
    return heap;
  }

  /** Pops the smallest n items from the heap, placing them in the internal array at
   *  arr[size] through arr[size-(n-1)] with the smallest (first element popped)
   *  being at arr[size].  The internal array is returned.
   */
  public long[] sort(int n) {
    while (--n >= 0) {
      long result = heap[1];            // save first value
      heap[1] = heap[size];            // move last to first
      heap[size] = result;                  // place it last
      size--;
      downHeap();          // adjust heap
    }
    return heap;
  }

  /** Removes all entries from the PriorityQueue. */
  public void clear() {
    size = 0;
  }

  private void upHeap() {
    int i = size;
    long node = heap[i];        // save bottom node
    int j = i >>> 1;
    while (j > 0 && node < heap[j]) {
      heap[i] = heap[j];        // shift parents down
      i = j;
      j = j >>> 1;
    }
    heap[i] = node;          // install saved node
  }

  private void downHeap() {
    int i = 1;
    long node = heap[i];        // save top node
    int j = i << 1;          // find smaller child
    int k = j + 1;
    if (k <= size && heap[k] < heap[j]) {
      j = k;
    }
    while (j <= size && heap[j] < node) {
      heap[i] = heap[j];        // shift up child
      i = j;
      j = i << 1;
      k = j + 1;
      if (k <= size && heap[k] < heap[j]) {
        j = k;
      }
    }
    heap[i] = node;          // install saved node
  }

  /**
   * Adds an object to a PriorityQueue in log(size) time. If one tries to add
   * more objects than maxSize from initialize an
   * {@link ArrayIndexOutOfBoundsException} is thrown.
   * @return the new 'top' element in the queue.
   */
  private long add(long element) {
    if (size >= currentCapacity) {
      int newSize = Math.min(currentCapacity <<1, maxSize);
      if (newSize < currentCapacity) newSize = Integer.MAX_VALUE;  // handle overflow
      resize(newSize);
    }
    size++;
    heap[size] = element;//将插入元素插入在末尾,然后调整堆
    upHeap();
    return heap[1];
  }

 /**
   * Adds an object to a PriorityQueue in log(size) time. If one tries to add
   * more objects than the current capacity, an
   * {@link ArrayIndexOutOfBoundsException} is thrown.
   */
  private void addNoCheck(long element) {
    ++size;
    heap[size] = element;
    upHeap();
  }
  /** Returns the least element of the PriorityQueue in constant time. */
  public long top() {
    return heap[1];
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值