-
Notifications
You must be signed in to change notification settings - Fork 0
/
MinHeap.cpp
84 lines (65 loc) · 849 Bytes
/
MinHeap.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
79
80
81
82
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
int tree[100],n=-1;
void insert(int item)
{
int par;
n++;
int ptr=n;
while(ptr>0)
{
par=(ptr-1)/2;
if(item>tree[par])
{
tree[ptr]=item;
return;
}
else
{
tree[ptr]=tree[par];
ptr=par;
}
}
tree[ptr]=item;
}
void deletion()
{
int item=tree[0];
int last=tree[n];
n--;
int ptr=0,left=1,right=2;
while(right<=n)
{
if(last<tree[left]&&last<tree[right])
{
tree[ptr]=last;
return;
}
if(tree[left]<tree[right])
{
tree[ptr]=tree[left];
ptr=left;
}
else
{
tree[ptr]=tree[right];
ptr=rightl
}
left=2*ptr+1;
right=2*ptr+2;
}
if(left==n && last>tree[left])
{
tree[ptr]=tree[left];
tree[left]=last;
}
else
tree[ptr]=last;
}
void display()
{
int k;
for(k=0;k<=n;k++)
printf("%d \t",tree[k]);
}