-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheap.cpp
78 lines (71 loc) · 1.92 KB
/
heap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
using namespace std;
class heap {
public:
int arr[100];
int size ;
heap() {
arr[0] = -1;
size = 0;
}
//the insertion code takes time complexity of logn
void insert(int val) {
size = size +1 ;//increasing the size of the array
int index = size; //giving the value of size to the array
arr[index ] = val; // puttin the new value in the new index
while(index >1) {
int parent = index/2; //finiding the parent index
if(arr[parent] < arr[index]) {
swap(arr[parent],arr[index]); //swaping the values as in treee the higher value stays in above
index = parent;
}
else {
return;
}
}
}
void print() {
for(int i=1; i<=size;i++) {
cout<<arr[i] <<" ";
}
cout<<endl;
}
//deleting also takes time complexity of logn
void del () {
if(size == 0) {
cout<<"noting to delete" <<endl;
return;
}
//putting the last element in the first element
arr[1] = arr[size]; //swapping the values
size--; //decreasing the size and rwmoving the last element
//now re organising the tree
int i=1;
while(i <size) {
int leftindex = 2*i;
int rightindex = 2 *i + 1;
if(leftindex < size && arr[i] <arr[leftindex]) {
swap(arr[i],arr[leftindex]);
i = leftindex;
}
else if(rightindex < size && arr[i] < arr[rightindex]) {
swap(arr[i] , arr[rightindex]);
i= rightindex;
}
else {
return;
}
}
}
};
int main() {
heap h ;
h.insert(50);
h.insert(55);
h.insert(53);
h.insert(52);
h.insert(54);
h.print();
h.del();
h.print();
}