-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalueTree.cpp
More file actions
295 lines (282 loc) · 8.84 KB
/
valueTree.cpp
File metadata and controls
295 lines (282 loc) · 8.84 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//one more level (thus twice the size) of a BST but it also can use array indexing
//one can change the binary function to work with something other than natural numbers
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
//kinda classes
typedef struct Link{
int count;
int depth;
struct Link *next[2];
}Link;
Link *LINK(int count, int depth){
Link *part=(Link*)malloc(sizeof(Link));
part->count=count;
part->depth=depth;
part->next[0]=NULL;
part->next[1]=NULL;
return part;
}
typedef struct Binary{
int length;
bool *arr;
}Binary;
Binary *BINARY(int length, bool *arr){
Binary *num=(Binary*)malloc(sizeof(Binary));
num->length=length;
num->arr=arr;
return num;
}
//functions
Binary *binary(int num){
if(num==0){
bool *zero=(bool*)malloc(sizeof(bool));
zero[0]=false;
return BINARY(1,zero);
}
int length=log2( (float)num )+1, i=length-1;
bool *arr=(bool*)malloc(length*sizeof(bool));
while(num!=0){
arr[i--]=num%2;
num=num/2;
}
return BINARY(length,arr);
}
Link *addTo(Link *list, Binary *num){
Link *temp=list, *returnList=list; //LINKs
int i=0, count=temp->count; //integers
if(num->length>list->depth) returnList=LINK(count,num->length);
list=returnList;
if(temp->count==0){ //thus, temp->depth is 0
for(i=0;i<num->length;i++){
list->next[ num->arr[i] ]=LINK(count,num->length-(i+1));
list=list->next[ num->arr[i] ];
}
}
else{
for(i=0;i<num->length-temp->depth;i++){
list->next[ 0 ]=LINK(count,num->length-(i+1));
list=list->next[ 0 ];
}
}
list->next[0]=temp->next[0];
list->next[1]=temp->next[1];
returnList->count++;
temp=returnList;
for(i=0;i<returnList->depth-num->length;i++){
if(temp->next[ 0 ]==NULL) temp->next[ 0 ]=LINK(1,returnList->depth-(i+1));
else temp->next[0]->count++;
temp=temp->next[0];
}
for(i=0;i<num->length;i++){
if(temp->next[ num->arr[i] ]==NULL)
temp->next[ num->arr[i] ]=LINK(1,num->length-(i+1));
else
temp->next[ num->arr[i] ]->count++;
temp=temp->next[ num->arr[i] ];
}
return returnList;
}
Link *takeAway(Link *list, Binary *num){
if(num->length>list->depth || list->depth==0) return list;
bool PATH[list->depth], n; //bool
int i, depth=list->depth, diff=depth-num->length; //integers
Link *temp=list, *returnList=list, *number[depth+1]; //LINKs
for(i=0;i<depth;i++){
//diff is the list's depth (depth of its highest number) minus the depth of the number itself
//where "depth" is the amount of bits to express a number in binary
//thus, n encompasses the trailing 0s before the number starts based on the depth of the list
if(i<diff) n=0;
else n=num->arr[i-diff];
if(temp->next[n]==NULL) return returnList;
PATH[depth-1-i]=n;
temp=temp->next[n];
number[depth-1-i]=temp;
}
number[depth]=returnList;
number[0]->count--;
for(i=0;i<depth;i++){
number[i+1]->count--;
if(number[i]->count==0){
number[i+1]->next[ PATH[i] ]=NULL;
free(number[i]);
}
}
while(returnList->next[1]==NULL && returnList->next[0]!=NULL){
temp=returnList;
returnList=returnList->next[0];
free(temp);
}
if(returnList->count==0) returnList->depth=0;
return returnList;
}
int equalTo(Link *list, Binary *num){
if(num->length>list->depth || list->count==0) return 0;
Link *temp=list; //LINK
int i; //integer
for(i=0;i<list->depth-num->length;i++){
if(temp->next[0]==NULL) return 0;
temp=temp->next[ 0 ];
}
for(i=0;i<num->length;i++){
if(temp->next[ num->arr[i] ]==NULL) return 0;
temp=temp->next[ num->arr[i] ];
}
return temp->count;
}
int lessThan(Link *list, Binary *num){
Link *temp=list; //LINK
int i, count=0; //integers
if(num->length>list->depth) return list->count;
for(i=0;i<list->depth-num->length;i++){
if(temp->next[0]==NULL) return count;
temp=temp->next[0];
}
for(i=0;i<num->length;i++){
if(num->arr[i] && temp->next[0]!=NULL)
count+=temp->next[0]->count;
if(temp->next[ num->arr[i] ]==NULL) return count;
temp=temp->next[ num->arr[i] ];
}
return count;
}
int moreThan(Link *list, Binary *num){
Link *temp=list; //LINK
int i, count=0; //integers
if(num->length>list->depth) return count;
for(i=0;i<list->depth-num->length;i++){
if(temp->next[1]!=NULL) count+=temp->next[1]->count;
if(temp->next[0]==NULL) return count;
temp=temp->next[0];
}
for(i=0;i<num->length;i++){
if(!num->arr[i] && temp->next[1]!=NULL)
count+=temp->next[1]->count;
if(temp->next[ num->arr[i] ]==NULL) return count;
temp=temp->next[ num->arr[i] ];
}
return count;
}
int integer(Binary *num){
int p, j, i, n=0;
for(i=num->length-1;i>=0;i--){
p=1; //power
for(j=num->length-1;j>i;j--) p*=2;
n+=p*num->arr[i];
}
return n;
}
int atIndex(Link *list, int num){
if(num<0 || num>=list->count)
return -2147483647; //sign for it not existing
bool arr[list->depth], n, full; //bools
Binary *myNumber=BINARY(list->depth,arr); //BINARY
Link *temp=list; //LINK
int index=0; //int
//index is amount of locations in the list exceeded due to implied logic with the count attribute
while(temp->depth>0){
full=temp->next[0]!=NULL && temp->next[1]!=NULL;
if(!full) n=(temp->next[0]==NULL);
else n=(index+temp->next[0]->count<=num);
if(n&&full) index+=temp->next[0]->count;
arr[list->depth-temp->depth]=n;
temp=temp->next[n];
}
return integer(myNumber);
}
int indexAt(Link *list, Binary *num){
if(num->length>list->depth || list->count==0) return -1;
Link *temp=list; //LINK
int i, index=0; //integer
for(i=0;i<list->depth-num->length;i++){
if(temp->next[0]==NULL) return -1;
temp=temp->next[ 0 ];
}
for(i=0;i<num->length;i++){
if(temp->next[ num->arr[i] ]==NULL) return -1;
if(num->arr[i] && temp->next[0]) index+=temp->next[0]->count;
temp=temp->next[ num->arr[i] ];
}
return index;
}
void ascending(Link *list){
int i, result, intPtr=0, length=list->count; //integers
bool arr[list->depth], n; //bools
Binary *num=BINARY(list->depth,arr); //binary
Link *linkPtrs[list->depth], *temp=list; //LINKs
for(i=0;i<list->depth;i++) arr[i]=0;
while(length>0){
if(length<list->count) temp=linkPtrs[intPtr];
for(i=intPtr;i<list->depth;i++){
n=num->arr[i]; //1 or 0
if(temp->next[0]!=NULL && temp->next[1]!=NULL) intPtr=i;
if(temp->next[n]==NULL) n=!n;
linkPtrs[i]=temp;
temp=temp->next[n];
num->arr[i]=n;
}
result=integer(num);
for(i=0;i<temp->count;i++){
length--;
printf("%d\n",result);
}
while(intPtr>=0&& (!(linkPtrs[intPtr]->next[0]==NULL || linkPtrs[intPtr]->next[1]==NULL)? num->arr[intPtr]: true))
intPtr--; //keep going back up until another crossroad when all numbers from crossroad evaluated
if(!num->arr[intPtr]) num->arr[intPtr]=1;
for(i=intPtr+1;i<list->depth;i++) num->arr[i]=0;
}
}
void descending(Link *list){
int i, result, intPtr=0, length=list->count; //integers
bool arr[list->depth], n; //bools
Binary *num=BINARY(list->depth,arr); //binary
Link *linkPtrs[list->depth], *temp=list; //LINKs
for(i=0;i<list->depth;i++) arr[i]=1;
while(length>0){
if(length<list->count) temp=linkPtrs[intPtr];
for(i=intPtr;i<list->depth;i++){
n=num->arr[i]; //1 or 0
if(temp->next[0]!=NULL && temp->next[1]!=NULL) intPtr=i;
if(temp->next[n]==NULL) n=!n;
linkPtrs[i]=temp;
temp=temp->next[n];
num->arr[i]=n;
}
result=integer(num);
for(i=0;i<temp->count;i++){
length--;
printf("%d\n",result);
}
while(intPtr>=0&& (!(linkPtrs[intPtr]->next[0]==NULL || linkPtrs[intPtr]->next[1]==NULL)? !num->arr[intPtr]: true))
intPtr--; //keep going back up until another crossroad when all numbers from crossroad evaluated
if(num->arr[intPtr]) num->arr[intPtr]=0;
for(i=intPtr+1;i<list->depth;i++) num->arr[i]=1;
}
}
void recurse(Link *node, int N, int i){
if(node==NULL) return;
//if(node->next[0]==NULL && node->next[1]==NULL) return;
for(int c=0;c<N;c++) printf("- ");
printf("[%d] count: %d depth: %d\n",i,node->count,node->depth);
recurse(node->next[0],N+1,0);
recurse(node->next[1],N+1,1);
}
int main(){
Link *list=LINK(0,0);
list=addTo(list,binary(24));
list=addTo(list,binary(25));
list=addTo(list,binary(33));
list=addTo(list,binary(33));
list=addTo(list,binary(1));
ascending(list);
printf("\n");
descending(list);
printf("\n%d %d\n",indexAt(list,binary(24)),indexAt(list,binary(33)));
list=takeAway(list,binary(1));
printf("\n\n");
ascending(list);
printf("\n");
descending(list);
printf("\n%d %d\n",indexAt(list,binary(24)),indexAt(list,binary(33)));
return 0;
}