-
Notifications
You must be signed in to change notification settings - Fork 0
/
HEAP01.BAK
88 lines (76 loc) · 1.35 KB
/
HEAP01.BAK
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
79
80
81
82
83
84
85
86
87
88
#include<iostream.h>
#include<conio.h>
#include<alloc.h>
void swap(int *x,int *y)
{
int temp ;
temp=*x;
*x=*y;
*y=temp;
}
class Heap
{
int *harr;
/* declaration of array since it is a heap and heap concept based on array
in which parents are declared at ((i-1)/2) left children will be at (2*i+1)
and right children is at ((2*i)+2) */
int heap_size,cap;
public:
Heap(int size)
{
cap=size;
harr=new int[cap];
}
int parent(int i )
{
return ((i-1)/2);
}
int leftchild(int i)
{
return ((2*i)+1);
}
int rightchild(int i)
{
return ((2*i)+2);
}
void insert_key(int k);
//function created to insert the elements in an array / Heap in this case
void print()
{
for(int i=0;i<cap;i++)
{
cout<<harr[i];
cout<<"\t";
}
}
};
void Heap::insert_key(int k )
{
if(heap_size==cap)
{
cout<<"Overflow";
}
heap_size++;
int i=heap_size-1;
harr[i]=k;
while(i!= 0 && harr[parent(i)] > harr[i])
{
swap(&harr[parent(i)],&harr[i]);
i=parent(i);
}
}
int main()
{
clrscr();
Heap h(8);
h.insert_key(2);
h.insert_key(1);
h.insert_key(8);
h.insert_key(10);
h.insert_key(25);
h.insert_key(29);
h.insert_key(30);
h.print();
getch();
return 0;
}