-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path046-assign08.c
230 lines (221 loc) · 5.11 KB
/
046-assign08.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/*NAME: KRISHNA KUMAR DEY
ROLL:201601046
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
typedef struct BSTREE
{
int nID; /*Node Identifier - [1, 2000]*/
char proname[16]; /* it can hold a maximum of 16 characters*/
int size; /*size [480, 3375] – measured in square feet*/
float nbeds; /*[1, 5] - increment can be in steps of 0.5*/
float base; /*[2499.0, 4850.0] – base price per square feet*/
float charges; /*represents percentage of the base ranging from [2.14, 8.23]*/
int ptype; /*ranges in {0, 1, 2}: 0 – basement parking;1- covered parking; and 2 – open parking*/
struct BSTREE *left,*right;
}BSTREE;
BSTREE *root=NULL,*ARRAY[100];
char proname1[][100]={
{"abcd"},
{"abcde"},
{"avgsxg"},
{"akjhaa"},
{"kajgajvga"},
{"jdasfzaya"},
{"javdsgfrasdt"},
};
int n;
void createbinarytree(BSTREE *bstree,BSTREE *temp)
{
if(root==NULL)
{
root=bstree;
return;
}
if(bstree->size<temp->size)
{
if(temp->left==NULL)
{
temp->left=bstree;
return;
}
else
createbinarytree(bstree,temp->left);
}
else
{
if(temp->right==NULL)
{
temp->right=bstree;
return;
}
else
createbinarytree(bstree,temp->right);
}
}
BSTREE *genFlatsDataset (BSTREE *bstree, int n)
{
int i;
for(i=0;i<n;i++)
{
bstree=(BSTREE *)malloc(sizeof(BSTREE));
bstree->nID=(rand()%2000)+1;
strcpy(bstree->proname,proname1[rand()%7]);
bstree->size=(rand()%2996)+480;
bstree->nbeds=((rand()%9)+2)/2.0;
bstree->base=(rand()%23520)/10.0+2499.0;
bstree->charges=((rand()%610)+214)/100.0;
bstree->ptype=rand()%3;
bstree->left=NULL;
bstree->right=NULL;
ARRAY[i]=bstree;
createbinarytree(bstree,root);
}
return root;
}
void printFlatDetails(BSTREE *bstree)
{
if(bstree==NULL)
return;
printFlatDetails(bstree->left);
printf("ID: %d NAME: %s SIZE: %d BEDS: %0.2f ",bstree->nID,bstree->proname,bstree->size,bstree->nbeds);
printf("BASE: %0.3f CHARGES: %0.3f PARKING: %d\n",bstree->base,bstree->charges,bstree->ptype);
printFlatDetails(bstree->right);
}
void FindFlatsByNBeds(BSTREE *bstree, float n)
{
if(bstree==NULL)
return;
FindFlatsByNBeds(bstree->left,n);
if(bstree->nbeds==n)
{
printf("ID: %d NAME: %s SIZE: %d BEDS: %0.2f ",bstree->nID,bstree->proname,bstree->size,bstree->nbeds);
printf("BASE: %0.3f CHARGES: %0.3f PARKING: %d\n",bstree->base,bstree->charges,bstree->ptype);
}
FindFlatsByNBeds(bstree->right,n);
}
void ModifyParking(BSTREE *bstree, int oldPark, int newpark)
{
if(bstree==NULL)
return;
ModifyParking(bstree->left,oldPark,newpark);
if(bstree->ptype==oldPark)
{
bstree->ptype=newpark;
}
ModifyParking(bstree->right,oldPark,newpark);
}
void makingnull()
{
int i;
for(i=0;i<n;i++)
{
ARRAY[i]->left=NULL;
ARRAY[i]->right=NULL;
}
}
BSTREE *BalanceBST(BSTREE *bstree)
{
int i=0,p=1;
makingnull();
BSTREE *a,*b;
a=ARRAY[i];
while(p!=n)
{
a=ARRAY[i];
b=ARRAY[p];
a->left=b;//ASSIGNING IN LEFT HALF
p++;
if(p==n)
break;
b=ARRAY[p];
a->right=b;// ASSIGNING IN RIGHT HALF
i++;
p++;
}
return ARRAY[0];
}
int c=0;
BSTREE *array[100];
void makingnull1()
{
int i;
for(i=0;i<c;i++)
{
array[i]->left=NULL;
array[i]->right=NULL;
}
}
void noofnodes(BSTREE *btnode, int min,int max)
{
if(btnode==NULL)
return;
noofnodes(btnode->left,min,max);
if(btnode->charges>min && btnode->charges<max)
{
array[c]=btnode;
//printf("%d\n",btnode->level );
c++;
}
noofnodes(btnode->right,min,max);
}
BSTREE *deleteElements(BSTREE *bstree, int min, int max)
{
noofnodes(bstree,min,max);
int i=0,p=1;
makingnull1();
BSTREE *a,*b;
a=array[i];
while(p!=c)
{
a=array[i];
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];
}
int main(int argc,char *argv[])
{
time_t t;
srand((unsigned)time(&t));(rand()%9000)+1000;
n=(argc>1)?atoi(argv[1]):20;
BSTREE *bstree;
bstree=genFlatsDataset(bstree,n); /*generating BINARY TREE */
/*--------------------printing the TREE after creating----------------------------- */
printf("\n");
printFlatDetails(bstree);
/*-----------------to search and print the details of the flats by specific number of bedrooms---*/
float a=((rand()%9)+2)/2.0;
printf("\n");
printf("----------------------------to search and print the details of the flats by specific number of bedrooms-%0.1f-------------\n",a);
FindFlatsByNBeds(bstree,a);
int newpark=rand()%3;
int oldPark=rand()%3;
printf("\n");
printf("--------------------printing the TREE after updating from %d to %d----------------------------\n",oldPark,newpark);
ModifyParking(bstree,oldPark,newpark);
/*--------------------printing the TREE after updating----------------------------- */
printFlatDetails(bstree);
printf("\n");
printf("----------------------------printing tree after making it complete binary tree-------------\n");
bstree=BalanceBST(bstree);
printFlatDetails(bstree);
printf("\n");
float min=((rand()%610)+214)/100.0;
float max=min+1.0;
bstree=deleteElements(bstree,min,max);
printFlatDetails(bstree);
printf("---------------------");
printf("\n");
return 0;
}