• 利用完全二叉树的性质,如何创建一个大根堆和一个小根堆?


    大根堆

    大根堆:每个结点的值不大于他的父亲结点的值

    分析如下:

    假设对{ 27,15,19,18,28,34,65,49,25,37 }这样一个集合的数据创建成堆;

     

     

     

     

    代码如下:

    1. //建立大根堆
    2. public class TestHeap{
    3. public int[] array;
    4. public int usedSize;//当前有效数组长度
    5. public TestHeap(){
    6. this.array = new int[10];
    7. this.usedSize = 0;
    8. }
    9. //初始化数组
    10. public void InitArray(int[] arrayClone){
    11. array = Arrays.copyOf(arrayClone, arrayClone.length);
    12. usedSize = array.length;
    13. }
    14. //创建大根堆
    15. public void createHeap(){
    16. for(int parent = (usedSize - 1 - 1) / 2; parent >= 0; parent--){
    17. adjustment(parent, usedSize);
    18. }
    19. }
    20. //调整
    21. public void adjustment(int parent, int len){
    22. //左子树结点下标
    23. int child = parent * 2 + 1;
    24. //调整
    25. while(child < len){
    26. //先判断有没有右孩子,如果右,找出最大值
    27. if(child + 1 < len && array[child] < array[child + 1]){
    28. child++;//如果右子树大,child++就指向他,如果左子树大,就不用管,直接进行下一步判断交换
    29. }
    30. //若左右子树的最大值大于父亲结点则交换
    31. if(array[child] > array[parent]){
    32. swap(array, child, parent);
    33. parent = child;
    34. child = parent * 2 + 1;
    35. } else{
    36. break;
    37. }
    38. }
    39. }
    40. //交换
    41. public void swap(int[] array, int child, int parent){
    42. int tmp = array[child];
    43. array[child] = array[parent];
    44. array[parent] = tmp;
    45. }
    46. }

    小根堆

    小根堆:每个结点的值不小于他的父亲结点的值

         分析与大根堆类似,只是比较出更小的进行替换

    代码如下:

    1. //建立大根堆
    2. public class TestHeap{
    3. public int[] array;
    4. public int usedSize;//当前有效数组长度
    5. public TestHeap(){
    6. this.array = new int[10];
    7. this.usedSize = 0;
    8. }
    9. //初始化数组
    10. public void InitArray(int[] arrayClone){
    11. array = Arrays.copyOf(arrayClone, arrayClone.length);
    12. usedSize = array.length;
    13. }
    14. //创建大根堆
    15. public void createHeap(){
    16. for(int parent = (usedSize - 1 - 1) / 2; parent >= 0; parent--){
    17. adjustment(parent, usedSize);
    18. }
    19. }
    20. //调整
    21. public void adjustment(int parent, int len){
    22. //左子树结点下标
    23. int child = parent * 2 + 1;
    24. //调整
    25. while(child < len){
    26. //先判断有没有右孩子,如果右,找出最小值
    27. if(child + 1 < len && array[child] > array[child + 1]){
    28. child++;//如果右子树小,child++就指向他,如果左子树小,就不用管,直接进行下一步判断交换
    29. }
    30. //若左右子树的最大值小于父亲结点则交换
    31. if(array[child] < array[parent]){
    32. swap(array, child, parent);
    33. parent = child;
    34. child = parent * 2 + 1;
    35. } else{
    36. break;
    37. }
    38. }
    39. }
    40. //交换
    41. public void swap(int[] array, int child, int parent){
    42. int tmp = array[child];
    43. array[child] = array[parent];
    44. array[parent] = tmp;
    45. }
    46. }

  • 相关阅读:
    数据仓库(7)数仓规范设计
    【DOM】-- 事件机制
    吃透底层:从路由到前缀树
    如何理解深度学习的训练过程
    MindFusion.Diagramming for ASP.NET MVC 4.2 Crack
    两步随机接入机制的深度解析和未来增强
    笔试强训 Day6
    【SQL刷题】DAY16----SQL高级联结专项练习
    java中stream常用api介绍
    (附源码)spring boot校园万能跑系统 毕业设计 160934
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/126191800