java堆结构_堆结构的java实现

import java.util.Comparator;

/**

* 堆数据结构

*

* @author Administrator

*

*/

public class Heap {

/**

* 以数组形式存储堆元素

*/

private T[] heap;

/**

* 用于比较堆中的元素。c.compare(根,叶子) > 0。

* 使用相反的Comparator可以创建最大堆、最小堆。

*/

private Comparator super T> c;

public Heap(T[] a, Comparator super T> c) {

this.heap = a;

this.c = c;

buildHeap();

}

/**

* 返回值为i/2

*

* @param i

* @return

*/

private int parent(int i) {

return (i - 1) >> 1;

}

/**

* 返回值为2*i

*

* @param i

* @return

*/

private int left(int i) {

return ((i + 1) << 1) - 1;

}

/**

* 返回值为2*i+1

*

* @param i

* @return

*/

private int right(int i) {

return (i + 1) << 1;

}

/**

* 堆化

*

* @param i

* 堆化的起始节点

*/

private void heapify(int i) {

heapify(i, heap.length);

}

/**

* 堆化,

*

* @param i

* @param size 堆化的范围

*/

private void heapify(int i, int size) {

int l = left(i);

int r = right(i);

int next = i;

if (l < size && c.compare(heap[l], heap[i]) > 0)

next = l;

if (r < size && c.compare(heap[r], heap[next]) > 0)

next = r;

if (i == next)

return;

swap(i, next);

heapify(next, size);

}

/**

* 对堆进行排序

*/

public void sort() {

// buildHeap();

for (int i = heap.length - 1; i > 0; i--) {

swap(0, i);

heapify(0, i);

}

}

/**

* 交换数组值

*

* @param arr

* @param i

* @param j

*/

private void swap(int i, int j) {

T tmp = heap[i];

heap[i] = heap[j];

heap[j] = tmp;

}

/**

* 创建堆

*/

private void buildHeap() {

for (int i = (heap.length) / 2 - 1; i >= 0; i--) {

heapify(i);

}

}

public void setRoot(T root) {

heap[0] = root;

heapify(0);

}

public T root() {

return heap[0];

}

/**

* 取出最大元素并从堆中删除最大元素。

*

* @param

* @param a

* @param comp

* @return

*/

// public T extractMax(T[] a, Comparator super T> comp) {

// if (a.length == 0) {

// throw new

// IllegalArgumentException("can not extract max element in empty heap");

// }

// T max = a[0];

// a[0] = a[a.length - 1];

// heapify(0, a.length - 1);

// return max;

// }

/**

* @param args

*/

public static void main(String[] args) {

Integer[] temp = null;

temp = new Integer[] { 5, 2, 4, 6, 1, 3, 2, 6 };

temp = new Integer[] { 16, 14, 8, 7, 9, 3, 2, 4, 1 };

Comparator comp = new Comparator() {

@Override

public int compare(Integer o1, Integer o2) {

return o1 - o2;

}

};

//创建最大堆

Heap heap = new Heap(temp, comp);

// heap.buildHeap();

for (int i : temp) {

System.out.print(i + " ");

}

System.out.println();

heap.sort();

for (int i : temp) {

System.out.print(i + " ");

}

System.out.println();

}

}

通过Comparator控制堆的性质(是最大堆还是最小堆)

创建最大堆

Heap heap = new Heap(topn,new Comparator() {

@Override

public int compare(Integer o1, Integer o2) {

//生成最大堆使用o1-o2,生成最小堆使用o2-o1

return o1-o2;

}

});

创建最小堆

Heap heap = new Heap(topn,new Comparator() {

@Override

public int compare(Integer o1, Integer o2) {

//生成最大堆使用o1-o2,生成最小堆使用o2-o1

return o2-o1;

}

});

分享到:

18e900b8666ce6f233d25ec02f95ee59.png

72dd548719f0ace4d5f9bca64e1d7715.png

2011-05-27 16:33

浏览 8401

评论

2 楼

attend

2012-01-13

不好意思,看错了。 if (r < size && c.compare(heap[r], heap[next]) > 0)   看成

if (r < size && c.compare(heap[r], heap[i]) > 0)  了。

1 楼

attend

2012-01-13

private void heapify(int i, int size) {

int l = left(i);

int r = right(i);

int next = i;

if (l < size && c.compare(heap[l], heap[i]) > 0)

next = l;

if (r < size && c.compare(heap[r], heap[next]) > 0)

next = r;

if (i == next)

return;

swap(i, next);

heapify(next, size);

}

有点问题,

swap(i, next);

heapify(next, size);

应该放到if块里吧?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值