-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytree.sol
More file actions
242 lines (238 loc) · 5.33 KB
/
binarytree.sol
File metadata and controls
242 lines (238 loc) · 5.33 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
pragma solidity ^0.4.0;
contract Coins
{
function Coins()
{
head=0;
ind_xy=0;
ind=1;
Btree[0].left=0;
Btree[0].right=0;
}
struct Node
{
uint key;
uint left;
uint right;
uint[10] y_arr;
uint index;
}
uint[100] public x;
uint ind_xy;
uint[100] public y;
Node[100] public Btree;
uint public head;
uint public ind;
function max(int a, int b) returns (int)
{
return (a > b)? a : b;
}
function search_x(uint r, uint key,uint key_y) returns (uint)
{
if(Btree[r].key==key)
{
for(uint i=0;i<Btree[r].index;i++)
{
if(Btree[r].y_arr[i]==key_y)
return r;
}
return 0;
}
else if(Btree[r].key>key)
{
if(Btree[r].left!=0)
return search_x(Btree[r].left,key,key_y);
else return 0;
}
else
{
if(Btree[r].right!=0)
return search_x(Btree[r].right,key,key_y);
else return 0;
}
}
function search_y(uint r, uint key,uint key_y) returns (uint)
{
if(Btree[r].key==key)
{
for(uint i=0;i<Btree[r].index;i++)
{
if(Btree[r].y_arr[i]==key_y)
return i;
}
return 0;
}
else if(Btree[r].key>key)
{
if(Btree[r].left!=0)
return search_x(Btree[r].left,key,key_y);
else return 0;
}
else
{
if(Btree[r].right!=0)
return search_y(Btree[r].right,key,key_y);
else return 0;
}
}
function sear(uint r,uint key) returns (uint)
{
for(;;)
{
if (key < Btree[r].key)
{
if(Btree[r].left==0)
{
return r;
}
else
r=Btree[r].left;
}
else if (key > Btree[r].key)
{
if(Btree[r].right==0)
{
return r;
}
else
r=Btree[r].right;
}
else {
return r;
}
}
}
function insert(uint r, uint key,uint key_y)
{
if (head==0)
{
Btree[ind].key=key;
Btree[ind].left=0;
Btree[ind].right=0;
Btree[ind].y_arr[Btree[ind].index]=key_y;
Btree[ind].index=Btree[ind].index+1;
head=1;
}
else
{
if (key < Btree[r].key)
{
ind=ind+1;
Btree[r].left=ind;
Btree[ind].left=0;
Btree[ind].right=0;
Btree[ind].key=key;
Btree[ind].y_arr[Btree[ind].index]=key_y;
Btree[ind].index=Btree[ind].index+1;
}
else if (key > Btree[r].key)
{
ind=ind+1;
Btree[r].right=ind;
Btree[ind].left=0;
Btree[ind].right=0;
Btree[Btree[r].right].key=key;
Btree[ind].y_arr[Btree[ind].index]=key_y;
Btree[ind].index=Btree[ind].index+1;
}
else {
Btree[r].y_arr[Btree[r].index]=key_y;
Btree[r].index=Btree[r].index+1;
}
}
return;
}
function push_y(uint x_x,uint y1,uint y2)
{
for(uint i=0;i<Btree[x_x].index;i++)
{
if(Btree[x_x].y_arr[i]<=y2&& Btree[x_x].y_arr[i]>=y1)
{
x[ind_xy]=Btree[x_x].key;
y[ind_xy++]=Btree[x_x].y_arr[i];
}
}
}
function searchlist(uint root,uint x1,uint x2,uint y1,uint y2)
{
uint r_k=Btree[root].key;
if(Btree[root].left==0 &&Btree[root].right==0&& r_k==0)
return;
if(r_k>x2 && r_k>x1)
searchlist(Btree[root].left,x1,x2,y1,y2);
else if(r_k<x2 && r_k<x1)
searchlist(Btree[root].right,x1,x2,y1,y2);
else if(r_k>=x1&& r_k<=x2)
{
push_y(root,y1,y2);
searchlist(Btree[root].left,x1,x2,y1,y2);
searchlist(Btree[root].right,x1,x2,y1,y2);
}
}
function deletenode(uint root,uint key) returns (uint)
{
if(Btree[root].key==key)
{
uint x=root;
if(Btree[x].left==0&&Btree[x].right==0)
{
Btree[x].key=0;
return 0;
}
else if(Btree[x].left==0)
{
Btree[x].key=Btree[Btree[x].right].key;
Btree[x].index=Btree[Btree[x].right].index;
for(uint i=0;i<Btree[Btree[x].right].index;i++)
{
Btree[x].y_arr[i]= Btree[Btree[x].right].y_arr[i];
}
Btree[x].right=deletenode(Btree[x].right,Btree[Btree[x].right].key);
}
else
{
Btree[x].key=Btree[Btree[x].left].key;
Btree[x].index=Btree[Btree[x].left].index;
for(uint j=0;j<Btree[Btree[x].left].index;j++)
{
Btree[x].y_arr[j]= Btree[Btree[x].left].y_arr[j];
}
Btree[x].left=deletenode(Btree[x].left,Btree[Btree[x].left].key);
}
}
else if(Btree[root].key>key)
Btree[root].left=deletenode(Btree[root].left,key);
else
Btree[root].right=deletenode(Btree[root].right,key);
}
function deletekey(uint root,uint key,uint key_y) returns(uint)
{
uint x=search_x(root,key,key_y);
uint y=search_y(root,key,key_y);
if(x==0)
return 0;
else
{
if(Btree[x].index==1)
{
deletenode(root,key);
}
else
{
for(uint i=y;i<Btree[x].index-1;i++)
{
Btree[x].y_arr[i]=Btree[x].y_arr[i+1];
}
Btree[x].index--;
}
return x;
}
}
function minValueNode(uint root) returns (uint)
{
uint current = root;
while (Btree[current].left != 0)
current =Btree[current].left;
return Btree[current].key;
}
}