-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryheap.cpp
More file actions
72 lines (60 loc) · 1.11 KB
/
Copy pathbinaryheap.cpp
File metadata and controls
72 lines (60 loc) · 1.11 KB
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
#include<iostream>
using namespace std;
struct Heap{
int *array;
int count;//no. of elements in the array
int capacity;
};
//creating heap
struct Heap* createHeap(int capacity){
struct Heap *h=new Heap();
if(h==NULL){
cout<<"Memory error";
return 0;
}
else{
cout<<"Heap created";
}
h->count=0;
h->capacity=capacity;
h->array=new int[capacity]();
if(h->array==NULL){
cout<<"Memory error";
return 0;
}
return h;
}
int insert(struct Heap *h,int data){
int i;
cout<<"\nInserting data";
if(h->count==h->capacity&&h->count!=0)
cout<<"NO more space";
else{
h->count++;
i=h->count-1;
while(i>=0&&data>h->array[(i-1)/2]){
h->array[i]=h->array[(i-1)/2];
if(i==0)
break;
else
i=(i-1)/2;
}
h->array[i]=data;
cout<<endl<<i<<" ="<<data;
cout<<"\ninserted "<<data;
}
}
int getMaximum(struct Heap *h){
if(h->count==0)
return -1;
return h->array[0];
}
int main(){
Heap *h=createHeap(5);
insert(h,1);
insert(h,2);
insert(h,3);
cout<<"Hello";
cout<<"\nmaximum element is "<<getMaximum(h);
cout<<h->array[3];
}