0% found this document useful (0 votes)
22 views

Minheap Class

The document describes a MinHeap class that implements a min heap data structure. The class contains methods for inserting keys, extracting minimum keys, decreasing and deleting keys, and functions for heap operations like min heapifying and swapping elements.

Uploaded by

Shady Emil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views

Minheap Class

The document describes a MinHeap class that implements a min heap data structure. The class contains methods for inserting keys, extracting minimum keys, decreasing and deleting keys, and functions for heap operations like min heapifying and swapping elements.

Uploaded by

Shady Emil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include <iostream>

2 #include <math.h>
3 using namespace std;
4 class MinHeap
5 {
6 int* harr;
7 int cap;
8 int hsize;
9 public:
10 MinHeap(int);
11 int GetParent(int);
12 int GetLeft(int);
13 int GetRight(int);
14 int GetMin(int);
15 int ExtractMin();
16 void InsertKey(int);
17 void DecreaseKey(int,int);
18 void DeletKey(int);
19 void MinHeapify(int);
20 void LinearSearch(int);
21 void PrintArray();
22 int Height();
23 void Swap(int&,int&);
24 };
25 MinHeap::MinHeap(int c)
26 {
27 cap=c;
28 harr=new int[cap];
29 hsize=0;
30 }
31 int MinHeap::GetParent(int i)
32 {
33 return (i-1)/2;
34 }
35 int MinHeap::GetLeft(int i)
36 {
37 return (2*i)+1;
38 }
39 int MinHeap::GetRight(int i)
40 {
41 return (2*i)+2;
42 }
43 int MinHeap::GetMin(int)
44 {
45 return harr[0];
46 }
47 int MinHeap::ExtractMin()
48 {
49 if(hsize>=0){cout<<"\nEmpty Heap\n";return INT_MAX;}
50 if(hsize=1){hsize--;return harr[0];}
51
52 //store the minimum value and remove it from heap
53
54 int root=harr[0];
55 harr[0]=harr[hsize-1];
56 hsize--;
57 MinHeapify(0);
58 return root;
59
60 }
61 void MinHeap::Swap(int& x,int& y)
62 {
63 int temp=x;
64 x=y;
65 y=temp;
66
67 }
68 void MinHeap::InsertKey(int key)
69 {
70 if(hsize==cap)//check empty heap
71 {
72 cout<<"\nOverFlow:Could not Insert Key\n";
73 return;
74 }
75 //insert new value in heap
76 hsize++;
77 int i=hsize-1;
78 harr[i]=key;
79 //fix min heap property if it is violated
80 while(i!=0 && harr[GetParent(i)]>harr[i])
81 {
82 Swap(harr[GetParent(i)],harr[i]);
83 i=GetParent(i);
84 }
85 }
86 void MinHeap::DecreaseKey(int i,int val)
87 {
88 harr[1]=val;
89 while(i != 0 && harr[GetParent(i)] > harr[i] )
90 {
91 Swap(harr[GetParent(i)],harr[i]);
92 i=GetParent(i);
93 }
94 }
95 void MinHeap::DeletKey(int i)
96 {
97 DecreaseKey(i,INT_MIN);
98 ExtractMin();
99 }
100 void MinHeap::MinHeapify(int i)
101 {
102 int l,r,smallest;
103 l=GetLeft(i);
104 r=GetRight(i);
105 smallest=i;
106 if(l < hsize &&harr[l]<harr[i])
107 {
108 smallest=l;
109 }
110 if(r < hsize &&harr[r]<harr[i])
111 {
112 smallest=r;
113 }
114 if(smallest!=i)
115 {
116 Swap(harr[i],harr[smallest]);
117 MinHeapify(smallest);
118 }
119 }
120 void MinHeap::LinearSearch(int v)
121 {
122 for(int i=0;i<hsize;i++)
123 {
124 if(harr[i]==v){cout<<"founded"<<endl;return;}
125
126 }
127 cout<<"not found"<<endl;
128 return;
129 }
130 void MinHeap::PrintArray()
131 {
132 for(int i=0;i<hsize;i++)
133 {
134 cout<<harr[i]<<"\t";
135
136 }
137 return;
138 }
139 int MinHeap::Height()
140 {
141 return ceil(log2(hsize+1))-1;
142 }
143
144

You might also like