MCPcopy
hub / github.com/gyoogle/tech-interview-for-developer / remove

Method remove

Algorithm/code/Heap.java:22–55  ·  view source on GitHub ↗
(int[] arr)

Source from the content-addressed store, hash-verified

20 }
21 }
22 static int remove(int[] arr) {
23 if(heapSize == 0) return 0;
24
25 int rm = arr[1];
26 arr[1] = arr[heapSize];
27 arr[heapSize--] = 0;
28
29 for (int i = 1; i*2 <= heapSize;) {
30
31 if(i*2+1 <= heapSize) {
32
33 if(arr[i] < arr[i*2] && arr[i] < arr[i*2+1]) break;
34
35 else if(arr[i*2] < arr[i*2+1]) {
36 swap(i, i*2);
37 i = i*2;
38 }
39 else {
40 swap(i, i*2+1);
41 i = i*2+1;
42 }
43 }
44 else {
45 if(arr[i] > arr[i*2]) {
46 swap(i, i*2);
47 i = i*2;
48 }
49 else
50 break;
51 }
52 }
53
54 return rm;
55 }
56
57 static void swap(int a, int b) {
58 int temp = arr[a];

Callers 2

deleteMethod · 0.80
deleteMethod · 0.80

Calls 1

swapMethod · 0.95

Tested by

no test coverage detected