forked from hankcs/HanLP
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
222 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* <summary></summary> | ||
* <author>He Han</author> | ||
* <email>[email protected]</email> | ||
* <create-date>2015/11/22 13:23</create-date> | ||
* | ||
* <copyright file="MaxHeap.java" company="码农场"> | ||
* Copyright (c) 2008-2015, 码农场. All Right Reserved, http://www.hankcs.com/ | ||
* This source is subject to Hankcs. Please contact Hankcs to get more information. | ||
* </copyright> | ||
*/ | ||
package com.hankcs.hanlp.algoritm; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* 用固定容量的优先队列模拟的最大堆,用于解决求topN大的问题 | ||
* | ||
* @author hankcs | ||
*/ | ||
public class MaxHeap<E> | ||
{ | ||
/** | ||
* 优先队列 | ||
*/ | ||
private PriorityQueue<E> queue; | ||
/** | ||
* 堆的最大容量 | ||
*/ | ||
private int maxSize; | ||
|
||
/** | ||
* 构造最大堆 | ||
* @param maxSize 保留多少个元素 | ||
* @param comparator 比较器,生成最大堆使用o1-o2,生成最小堆使用o2-o1,并修改 e.compareTo(peek) 比较规则 | ||
*/ | ||
public MaxHeap(int maxSize, Comparator<E> comparator) | ||
{ | ||
if (maxSize <= 0) | ||
throw new IllegalArgumentException(); | ||
this.maxSize = maxSize; | ||
this.queue = new PriorityQueue<E>(maxSize, comparator); | ||
} | ||
|
||
/** | ||
* 添加一个元素 | ||
* @param e 元素 | ||
* @return 是否添加成功 | ||
*/ | ||
public boolean add(E e) | ||
{ | ||
if (queue.size() < maxSize) | ||
{ // 未达到最大容量,直接添加 | ||
queue.add(e); | ||
return true; | ||
} | ||
else | ||
{ // 队列已满 | ||
E peek = queue.peek(); | ||
if (queue.comparator().compare(e, peek) > 0) | ||
{ // 将新元素与当前堆顶元素比较,保留较小的元素 | ||
queue.poll(); | ||
queue.add(e); | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* 添加许多元素 | ||
* @param collection | ||
*/ | ||
public MaxHeap<E> addAll(Collection<E> collection) | ||
{ | ||
for (E e : collection) | ||
{ | ||
add(e); | ||
} | ||
|
||
return this; | ||
} | ||
|
||
/** | ||
* 转为有序列表,自毁性操作 | ||
* @return | ||
*/ | ||
public List<E> toList() | ||
{ | ||
ArrayList<E> list = new ArrayList<E>(queue.size()); | ||
while (!queue.isEmpty()) | ||
{ | ||
list.add(0, queue.poll()); | ||
} | ||
|
||
return list; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.hankcs.test.algorithm; | ||
|
||
import com.hankcs.hanlp.algoritm.MaxHeap; | ||
import junit.framework.TestCase; | ||
|
||
import java.util.Collections; | ||
import java.util.Comparator; | ||
|
||
public class MaxHeapTest extends TestCase | ||
{ | ||
final MaxHeap<Integer> heap = new MaxHeap<Integer>(5, new Comparator<Integer>() | ||
{ | ||
@Override | ||
public int compare(Integer o1, Integer o2) | ||
{ | ||
return o1.compareTo(o2); | ||
} | ||
}); | ||
|
||
public void testAdd() throws Exception | ||
{ | ||
heap.add(1); | ||
heap.add(3); | ||
heap.add(5); | ||
heap.add(7); | ||
heap.add(9); | ||
heap.add(8); | ||
heap.add(6); | ||
heap.add(4); | ||
heap.add(2); | ||
heap.add(0); | ||
} | ||
|
||
public void testAddAll() throws Exception | ||
{ | ||
|
||
} | ||
|
||
public void testToList() throws Exception | ||
{ | ||
testAdd(); | ||
System.out.println(heap.toList()); | ||
} | ||
} |