• 大顶堆(递归实现)


      //简单用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 if(indexVal > parentVal) 35 { 36 swap(parent,index); 37 HeapUp(parent); 38 } 39 40 } 41 } 42 43 /* 44 *将指定元素的位置进行下移操作 45 */ 46 private void HeapDown(int index) 47 { 48 int heapSize = heapList.size(); //这里进行了重复的计算,可以将作为形参传入,或者将该函数,写成非递归形式 49 50 if(index > heapSize - 1) 51 {//节点不存在 52 return; 53 } 54 55 int child = index * 2; //左孩子节点 56 57 if(child > (heapSize - 2)) 58 {//当前节点为叶子节点,不能进行下移操作,直接返回 59 //-2是由于最后一个元素已经是要删除的节点,不在计算范围之内 60 return; 61 } 62 else if(child < heapSize - 2) 63 {//有两个孩子节点 64 if((Integer)heapList.get(child) < (Integer)heapList.get(child + 1)) 65 { 66 child++; //右孩子结点值大,作为新的父节点 67 } 68 } 69 70 if(heapList.get(child).intValue() > heapList.get(index).intValue()) 71 {//孩子节点的值大,进行下移 72 swap(child, index); 73 HeapDown(child);//继续进行下移操作 74 } 75 76 } 77 78 /* 79 *向大顶堆中插入一个元素 80 */ 81 public void HeapInsert(int value) 82 { 83 int heapSize = heapList.size(); 84 85 if(heapSize == 0) 86 {//第一个元素不为堆中的元素,跳过 87 heapList.add(-100); 88 } 89 90 heapList.add(value); 91 heapSize++; //添加新元素后,改变堆的大小 92 93 HeapUp(heapSize - 1); 94 } 95 96 /* 97 *从大顶堆中删除一个元素 98 */ 99 public void HeapDelete(int value) 100 { 101 int index = 1,heapSize = heapList.size(); 102 for(; index < heapSize; index++) 103 { 104 if(heapList.get(index).intValue() == value) 105 { 106 break; 107 } 108 } 109 110 if (index >= heapSize) 111 {//元素不存在 112 return; 113 } 114 115 heapList.set(index, heapList.get(heapSize-1)); //将最后一个叶子节点值赋值到当前节点 116 HeapDown(index); 117 118 int parent = index / 2; 119 120 if(parent > 0 && ( heapList.get(index).intValue() > (Integer)heapList.get(parent).intValue() )) 121 {//如果下移操作后该元素大于父节点还要进行上移 122 HeapUp(index); 123 } 124 125 heapList.remove(heapSize - 1); 126 } 127 128 /* 129 * 打印堆元素 130 */ 131 public void PrintHeap() 132 { 133 for(int i = 1; i < heapList.size(); i++) 134 { 135 System.out.print(heapList.get(i) + " "); 136 } 137 138 System.out.println(); 139 } 140 141 public static void main(String args[]) 142 { 143 BigHeap bigHeap = new BigHeap(); 144 145 bigHeap.HeapInsert(1); 146 bigHeap.HeapInsert(3); 147 bigHeap.HeapInsert(4); 148 bigHeap.HeapInsert(5); 149 bigHeap.HeapInsert(8); 150 bigHeap.HeapInsert(2); 151 bigHeap.HeapInsert(7); 152 bigHeap.HeapInsert(6); 153 154 bigHeap.PrintHeap(); 155 156 bigHeap.HeapDelete(2); 157 bigHeap.PrintHeap(); 158 bigHeap.HeapDelete(8); 159 bigHeap.PrintHeap(); 160 } 161 }

    注:这里有一点需要说明,就是在删除堆中的一个元素时,如果遇到下面的情况,交换12和14两个节点元素后,14>13需要进行上移操作,来保证堆的结构。如上述代码中删除函数中所示。

  • 相关阅读:
    随笔
    随笔
    第一个存储过程
    mysql 存储过程
    join
    随笔
    玩家注册登录
    mysql 存储二进制数据
    mysql学习
    socket listen/accept
  • 原文地址:https://www.cnblogs.com/daimadebanyungong/p/4970848.html
Copyright © 2020-2023  润新知