-
Notifications
You must be signed in to change notification settings - Fork 0
/
heap.c
145 lines (132 loc) · 2.56 KB
/
heap.c
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int count=0;
int binArray[20];
void heapify(struct node* root)
{
int temp;
if(root->left != NULL && root->right!=NULL && root->left->data < root->data && root->left->data < root->right->data)
{
temp = root->data;
root->data = root->left->data;
root->left->data = temp;
heapify(root->left);
}
else if(root->right != NULL && root->left !=NULL && root->right->data < root->data){
temp = root->data;
root->data = root->right->data;
root->right->data = temp;
heapify(root->right);
}
return;
}
struct node* extractMin(struct node* root)
{
if(count == 2)
{
printf("Minimum is %d\n",root->data);
count--;
return NULL;
}
else if(count > 2)
{
printf("Minimum is %d\n",root->data);
struct node* temp = root;
int i = NumberToBinary(count-1);
while(i > 0)
{
temp=(binArray[i] == 0)?(temp->left):(temp->right);
i--;
}
if(binArray[i] == 0)
{
root -> data = temp -> left -> data;
temp -> left = NULL;
}
else
{
root -> data = temp -> right -> data;
temp -> right = NULL;
}
count--;
heapify(root);
return root;
}
else
{
return root;
}
}
void inorder(struct node *root)
{
if (root != NULL)
{
inorder(root->left);
printf("%d\n", root->data);
inorder(root->right);
}
}
struct node* insert(struct node *root,int value,int i)
{
if(root == NULL)
{
struct node* newNode = (struct node*)malloc(sizeof(struct node));
newNode->data = value;
newNode->left = newNode->right = NULL;
count++;
return newNode;
}
int temp;
if(binArray[i]==0)
{
if(root->data>value)
{
temp = root->data;
root->data = value;
value = temp;
}
i--;
root->left = insert(root->left,value,i);
}
else
{
if(root->data>value)
{
temp = root->data;
root->data = value;
value = temp;
}
i--;
root->right = insert(root->right,value,i);
}
}
int NumberToBinary(int num)
{
int temp=0;
while(num!=0)
{
binArray[temp]=num%2;
num=num/2;
temp+=1;
}
return temp-2;
}
int main()
{
struct node *root1 = NULL;
root1 = insert(root1,5,NumberToBinary(count));
root1 = insert(root1,14,NumberToBinary(count));
root1 = insert(root1,3,NumberToBinary(count));
root1 = insert(root1,2,NumberToBinary(count));
root1 = insert(root1,1,NumberToBinary(count));
root1 = extractMin(root1);
inorder(root1);
return 0;
}