-
Notifications
You must be signed in to change notification settings - Fork 1
/
046-assign07.c
200 lines (184 loc) · 3.96 KB
/
046-assign07.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*NAME:KRISHNA KUMAR DEY
ROLL NO:201601046
BATCH:A
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
//creating structure BTNODE
typedef struct BTNODE
{
int proID;
int rank;
int level;
float cost;
float discount;
float scost;
struct BTNODE *left;
struct BTNODE *right;
}BTNODE;
BTNODE *A[1000], *mini;
int n,c=0;
float min,a=0.0;
//GENERATING BINARY TREE
BTNODE *genBinaryTree(BTNODE *btnode, int n)
{
int i,p;
BTNODE *a,*b;
time_t t;
srand((unsigned)time(&t));
for(i=0;i<n;i++)
{
btnode=(BTNODE *)malloc(sizeof(BTNODE));
btnode->proID=(rand()%9000)+1000;
btnode->rank=(rand()%9000)+1;
btnode->cost=(rand()%4000)/10.0+99.0;
btnode->discount=(rand()%30000)/10000.0+2.0;
btnode->left=NULL;
btnode->right=NULL;
A[i]=btnode;//STORING THE ADDRESS OF NODES IN ARRAY 'A'
}
i=0,p=1;
a=A[i];
a->level=i;
while(p!=n)
{
a=A[i];
b=A[p];
b->level=(a->level)+1;
a->left=b;//ASSIGNING IN LEFT HALF
p++;
if(p==n)
break;
b=A[p];
b->level=(a->level)+1;
a->right=b;// ASSIGNING IN RIGHT HALF
i++;
p++;
}
return A[0];
}
//PRINTRING THROUGH INORDER TRAVERSAL
void printElements(BTNODE *btnode)
{
if(btnode==NULL)
return;
printElements(btnode->left);
printf("%d %d %d %0.2f %0.2f\n",btnode->proID,btnode->rank,btnode->level,btnode->cost,btnode->discount);
printElements(btnode->right);
}
//printing those values in between 129.0 to 399.95
void RangeSearchByCost(BTNODE *item, float scost)
{
//printf("%f\n",scost );
if(scost>=129.0 && scost<=399.95)
{
printf("%d %d %d %0.2f %0.2f \n",item->proID,item->rank,item->level,item->cost,scost);
}
}
//to check which values are inbetween 129.0-399.95
void inbet(BTNODE *btnode)
{
if(btnode==NULL)
return;
inbet(btnode->left);
btnode->scost=btnode->cost-(btnode->discount*btnode->cost)/100;
RangeSearchByCost(btnode,btnode->scost);
inbet(btnode->right);
}
//to search min cost
void SearchMinCost(BTNODE *btnode)
{
if(btnode==NULL)
return;
SearchMinCost(btnode->left);
if(btnode->cost<=min && btnode->cost>a)
{
min=btnode->cost;
mini=btnode;
}
SearchMinCost(btnode->right);
}
//funtion to store whose level is not to be deleted
void noofnodes(BTNODE *btnode, int level,BTNODE *array[])
{
if(btnode==NULL)
return;
noofnodes(btnode->left,level,array);
if(btnode->level!=level)
{
array[c]=btnode;
//printf("%d\n",btnode->level );
c++;
}
noofnodes(btnode->right,level,array);
}
//derefrencing
void makingnull()
{
int i=0;
for(i=0;i<n;i++)
{
A[i]->left=NULL;
A[i]->right=NULL;
}
}
//deleting level
BTNODE *deleteElements(BTNODE *btnode, int level)
{
BTNODE *array[1000],*a,*b;
noofnodes(btnode,level,array);
int i=0;
int p=1;
//printf("%d\n",c );
makingnull();
while(p!=c)
{
a=array[i];
//printf("%d ",a->level);
b=array[p];
a->left=b;//ASSIGNING IN LEFT HALF
p++;
if(p==c)
break;
b=array[p];
a->right=b;// ASSIGNING IN RIGHT HALF
i++;
p++;
}
return array[0];
}
//main function
int main(int args, char const *argv[])
{
int i,level;
if(argv[1]==NULL)
n=4;
else
n=atoi(argv[1]);
level=(args>2)?atoi(argv[2]):0;
BTNODE *btnode,*item;
btnode=genBinaryTree(btnode,n);
printf("------------------------------------------------generatingtree---------------------------------------\n");
printElements(btnode);
printf("\n");
printf("-----------------RangeSearchByCost---------------------------------------------------------------------\n");
inbet(btnode);
printf("------------------------min cost and second min--------------------------------------------\n");
min=btnode->cost;
printf("\n");
SearchMinCost(btnode);
printf("%d %d %d %0.2f\n",mini->proID,mini->rank,mini->level,mini->cost);
a=min;
min=0.0;
mini=btnode;
SearchMinCost(btnode);
printf("%d %d %d %0.2f\n",mini->proID,mini->rank,mini->level,mini->cost);
printf("\n");
btnode=deleteElements(btnode,level);
printf("-------------------printing after deleting level-----------------------------------------------------\n");
printElements(btnode);
0
return 0;
}