Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6.  
  7. package lab10heap;
  8.  
  9. /**
  10. *
  11. * @author usci
  12. */
  13. public class BinaryHeap {
  14. private Object[] maxHeap;
  15. private int size=0;
  16. public BinaryHeap(){
  17. this(10);
  18. }
  19. public BinaryHeap(int i){
  20. size=0;
  21. maxHeap = new Object[i];
  22. }
  23. public void buildHeap(int[] a,int size){
  24. int i;
  25. for(i=size/2;size>=0;i--){
  26. reHeap(a,i,size);
  27. }
  28. }
  29. public void reHeap(int[] a,int root,int size){
  30. int maxChild,tmp;
  31. boolean isHeap=false;
  32. while((!isHeap)){
  33. if(2*root+1==size){
  34. maxChild=2*root+1;
  35. }
  36. else{
  37. if(a[2*root+2]>a[2*root+1]){
  38. maxChild = 2*root+2;
  39. }
  40. else{
  41. maxChild = 2*root+1;
  42. }
  43. }
  44. if(a[maxChild]>a[root]){
  45. tmp=a[root];
  46. a[root]=a[maxChild];
  47. a[maxChild]=tmp;
  48. }
  49. else{
  50. isHeap=true;
  51. }
  52. }
  53. }
  54. public void add(int a){
  55. maxHeap[size]=a;
  56. size++;
  57. if(size>maxHeap.length/2){
  58. expandHeap();
  59. }
  60. reHeapUp();
  61. }
  62. public void remove(int a){
  63. int i,index=-1;
  64. for(i=0;i<size;i++){
  65. if(maxHeap[i].equals(a)){
  66. index=i;
  67. break;
  68. }
  69. }
  70. if(index==-1){
  71. return;
  72. }
  73. removeIndex(i);
  74. }
  75. public void removeIndex(int a){
  76. if(a==size){
  77. maxHeap[a]=null;
  78. return;
  79. }
  80. maxHeap[a]=maxHeap[size-1];
  81. maxHeap[size-1]=null;
  82. size--;
  83. reHeapDown();
  84. }
  85. public boolean isEmpty(){
  86. return size==0;
  87. }
  88. public void reHeapUp(){
  89. int index=size-1,parent=(index-1)/2,tmp;
  90. boolean isHeap=false;
  91. while(parent>=0&&(!isHeap)){
  92. if((int)maxHeap[parent]<(int)maxHeap[index]){
  93. tmp=(int)maxHeap[parent];
  94. maxHeap[parent]=maxHeap[index];
  95. maxHeap[index]=tmp;
  96. index=parent;
  97. parent=(index-1)/2;
  98. }
  99. else{
  100. isHeap=true;
  101. }
  102. }
  103. }
  104. public void reHeapDown(){
  105. int i,maxChild,tmp;
  106. for(i=0;i<size&&2*i+2<size;){
  107. if((int)maxHeap[2*i+1]>(int)maxHeap[2*i+2]){
  108. maxChild=2*i+1;
  109. }
  110. else{
  111. maxChild=2*i+2;
  112. }
  113. if((int)maxHeap[maxChild]>(int)maxHeap[i]){
  114. tmp=(int)maxHeap[i];
  115. maxHeap[i]=maxHeap[maxChild];
  116. maxHeap[maxChild]=tmp;
  117. i=maxChild;
  118. }
  119. else{
  120. break;
  121. }
  122. }
  123. }
  124. public void printHeap(){
  125. String s="";
  126. int i;
  127. for(i=0;i<size;i++){
  128. System.out.print("["+i+"]\t");
  129. }
  130. System.out.println("");
  131. for(i=0;i<size;i++){
  132. System.out.print(((int)maxHeap[i])+" \t");
  133. }
  134. System.out.println("\n");
  135. }
  136. public void expandHeap(){
  137. Object[] old = maxHeap;
  138. maxHeap = new Object[4*size];
  139. size=0;
  140. for(int i=0;i<old.length;i++){
  141. if(old[i]!=null){
  142. maxHeap[i]=old[i];
  143. size++;
  144. }
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement