-
Notifications
You must be signed in to change notification settings - Fork 0
/
minHeap.c
168 lines (151 loc) · 3.79 KB
/
minHeap.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct node
{
int nodeValue;
struct node *left;
struct node *right;
// int ind;
};
struct node* root=NULL;
int NumberOfNodes=1;
int binaryArray[10]={0,0,0,0,0,0,0,0,0,0};
struct node* newNode(int data)
{
struct node* node = (struct node*)malloc(sizeof(struct node));
node->nodeValue = data;
node->left = NULL;
node->right = NULL;
// node->ind=NumberOfNodes;
return(node);
}
int NumberToBinary(int num)
{
int counter =9;
while(num != 0){
binaryArray[counter]=num%2;
counter--;
num /= 2;
}
return counter+1;
}
//void swap(struct node* a,struct node* b)
//{
// int temp = a->nodeValue;
// a->nodeValue = b->nodeValue;
// b->nodeValue = temp;
//}
int HeightOfTree(int NumberOfNodes)
{
int temp=0;
temp=(int)(log(NumberOfNodes)/log(2));
return temp;
}
void heapify(struct node* root,in)
{
int temp;
if(root->left != NULL && root->right!=NULL && root->left->nodeValue < root->nodeValue && root->left->nodeValue < root->right->nodeValue)
{
temp = root->nodeValue;
root->nodeValue = root->left->nodeValue;
root->left->nodeValue = temp;
heapify(root->left);
}
else if(root->right != NULL && root->left !=NULL && root->right->nodeValue < root->nodeValue)
{
temp = root->nodeValue;
root->nodeValue = root->right->nodeValue;
root->right->nodeValue = temp;
heapify(root->right);
}
return;
}
//struct node* InsertNode(struct node *root, int NumberOfNodes, int key, int index)
void InsertNode(int key)
{
printf("\nInserting %d\n",key);
int k=0;
if(root == NULL)//only for first node
{
NumberOfNodes=2;
root=newNode(key);
return root;
}
k=NumberToBinary(NumberOfNodes);
struct node* Node= root;
struct node* temp= newNode(key);
// i=9-HeightOfTree(NumberOfNodes);
int i=k+1;//i points to the beginning of the binary part
int j=i;
while(i<9)
{
Node=(binaryArray[i]==0)?(Node->left):(Node->right);
i++;
}
struct node* ptr=Node;
if(binaryArray[9]==0)
{
Node->left=temp;
}
else
{
Node->right=temp;
}
NumberOfNodes++;
printf("\nNumber of nodes= %d\n",NumberOfNodes);
//heapify the above node
heapify(ptr);
printf("\n-------------------\n");
return;
}
int extract_min(struct node* root) //there might be some problem with extract_min
{
int lastVal=0;//variable to store the nodevalue of the last node
int min=root->nodeValue;
//swaping the last node and deleting it
// int* binaryArray=NumberToBinary(NumberOfNodes);
int i=NumberToBinary(NumberOfNodes)+1;
struct node* Node= root;
// int i=9-HeightOfTree(NumberOfNodes);//i points to the beginning of the binary part
while(i<9)
{
Node=(binaryArray[i]==0)?(Node->left):(Node->right);
i++;
}
if(binaryArray[9]==0)
{
lastVal=Node->left;
Node->left=NULL;
}
else
{
lastVal=Node->right;
Node->right=NULL;
}
NumberOfNodes--;
root->nodeValue=lastVal;
//heapify root
heapify(root);
return min;
}
int main()
{
root=InsertNode(root,5,HeightOfTree(NumberOfNodes));
printf("\nNow at root node: %d\n",root->nodeValue);
root=InsertNode(root,6,HeightOfTree(NumberOfNodes));
printf("now at root node: %d\n",root->nodeValue);
printf("now at root->left node: %d",root->left->nodeValue);
root=InsertNode(root,3,HeightOfTree(NumberOfNodes));
printf("now at root node: %d\n",root->nodeValue);
printf("now at root->left node: %d",root->left->nodeValue);
printf("now at root->right node: %d",root->right->nodeValue);
// InsertNode(1);
// InsertNode(6);
// InsertNode(62);
// InsertNode(63);InsertNode(43);InsertNode(621);
printf("\n Number of Nodes =%d",NumberOfNodes);
printf("\nRoot Node Value=%d\n",root->nodeValue);
// printf("\nExtract min Value = %d\n",extract_min(root));
return 0;
}