//简单用java实现一下堆的数据结构
1 package com.datastruct; 2 3 import java.util.ArrayList; 4 5 public class BigHeap { 6 7 8 ArrayList<Integer> heapList = new ArrayList<>(); 9 10 /* 11 *交换堆中的两个元素 12 */ 13 private void swap(int srcIndex,int dstIndex) 14 { 15 int tmp = heapList.get(srcIndex); 16 17 heapList.set(srcIndex,heapList.get(dstIndex)); 18 heapList.set(dstIndex, tmp); 19 20 } 21 22 /* 23 *将指定元素的位置进行上移操作 24 */ 25 private void HeapUp(int index) 26 { 27 28 if(index > 1) 29 { 30 int parent = index / 2; 31 int parentVal = heapList.get(parent).intValue(); 32 int indexVal = heapList.get(index).intValue(); 33 34
大顶堆(递归实现)
最新推荐文章于 2023-10-05 19:54:45 发布