9 #ifndef __H_SPEEDYPAIR_CONTAINER_TEMPLATE__
10 #define __H_SPEEDYPAIR_CONTAINER_TEMPLATE__
16 #define NULL ((void *)0)
20 #define max(a,b) ( ((a)>(b)) ? (a) : (b) )
24 template <
class KEY,
class VALUE>
33 template <
class MYVAL,
class TOVAL>
39 HSpeedypairNode<MYVAL,TOVAL> *left,*right;
40 HSpeedypairNode<TOVAL,MYVAL> *pair;
42 HSpeedypairNode(MYVAL d)
50 ~HSpeedypairNode(
void)
69 static void inorder(HSpeedypairNode<MYVAL,TOVAL> *here,
int *seq,
void *array,
int mode = 0);
71 static void insertTo(HSpeedypairNode<MYVAL,TOVAL> * &here, HSpeedypairNode<MYVAL,TOVAL> *e);
74 static void rleft (HSpeedypairNode<MYVAL,TOVAL> * &here);
75 static void rright (HSpeedypairNode<MYVAL,TOVAL> * &here);
76 static void drleft (HSpeedypairNode<MYVAL,TOVAL> * &here);
77 static void drright(HSpeedypairNode<MYVAL,TOVAL> * &here);
79 static int height(HSpeedypairNode<MYVAL,TOVAL> *t);
80 static HSpeedypairNode* find_min(HSpeedypairNode<MYVAL,TOVAL> *t);
87 template <
class KEY,
class VALUE>
92 HSpeedypairNode<KEY,VALUE> *root_key;
95 HSpeedypairNode<VALUE,KEY> *root_value;
116 void addPair(KEY k,VALUE v);
117 VALUE getValue(KEY key );
119 #ifndef ONE_SIDE_ONLY
120 KEY getKey (VALUE value);
123 int num(
void) {
return nume; }
124 int heightKTree(
void) {
return HSpeedypairNode<KEY,VALUE>::height(root_key); }
126 #ifndef ONE_SIDE_ONLY
127 int heightVTree(
void) {
return HSpeedypairNode<VALUE,KEY>::height(root_value); }
130 bool found(
void) {
return f; }
132 KEY *getKArray(
void);
134 #ifndef ONE_SIDE_ONLY
135 VALUE *getVArray(
void);
144 template <
class KEY,
class VALUE>
149 #ifndef ONE_SIDE_ONLY
156 template <
class KEY,
class VALUE>
161 #ifndef ONE_SIDE_ONLY
166 key_notfound_data = kn;
167 value_notfound_data = vn;
170 template <
class KEY,
class VALUE>
176 template <
class KEY,
class VALUE>
183 #ifndef ONE_SIDE_ONLY
184 if(root_value != NULL)
192 template <
class KEY,
class VALUE>
196 HSpeedypairNode<KEY,VALUE>* nk=
new HSpeedypairNode<KEY,VALUE>(k);
197 HSpeedypairNode<VALUE,KEY>* nv=
new HSpeedypairNode<VALUE,KEY>(v);
203 HSpeedypairNode<KEY,VALUE>::insertTo(root_key ,nk);
205 #ifndef ONE_SIDE_ONLY
206 HSpeedypairNode<VALUE,KEY>::insertTo(root_value,nv);
210 template <
class KEY,
class VALUE>
213 HSpeedypairNode<KEY,VALUE> *t = root_key;
221 return value_notfound_data;
233 return t->pair->data;
237 #ifndef ONE_SIDE_ONLY
238 template <
class KEY,
class VALUE>
241 HSpeedypairNode<VALUE,KEY> *t = root_value;
249 return key_notfound_data;
261 return t->pair->data;
266 template <
class MYVAL,
class TOVAL>
267 void HSpeedypairNode<MYVAL,TOVAL>::insertTo(HSpeedypairNode<MYVAL,TOVAL> * &here,
268 HSpeedypairNode<MYVAL,TOVAL> * e)
275 if(here->data < e->data)
277 insertTo(here->right,e);
278 if(HSpeedypairNode<MYVAL,TOVAL>::height(here->right) -
279 HSpeedypairNode<MYVAL,TOVAL>::height(here->left) == 2)
281 if(e->data > here->right->data)
282 HSpeedypairNode<MYVAL,TOVAL>::rleft(here);
284 HSpeedypairNode<MYVAL,TOVAL>::drright(here);
287 else if(here->data > e->data)
289 insertTo(here->left,e);
290 if(HSpeedypairNode<MYVAL,TOVAL>::height(here->left) -
291 HSpeedypairNode<MYVAL,TOVAL>::height(here->right) == 2)
293 if(e->data < here->left->data)
294 HSpeedypairNode<MYVAL,TOVAL>::rright(here);
296 HSpeedypairNode<MYVAL,TOVAL>::drleft(here);
302 template <
class MYVAL,
class TOVAL>
303 void HSpeedypairNode<MYVAL,TOVAL>::rleft(HSpeedypairNode<MYVAL,TOVAL> * &here)
305 HSpeedypairNode<MYVAL,TOVAL> *tmp = here->right;
306 here->right = tmp->left;
311 template <
class MYVAL,
class TOVAL>
312 void HSpeedypairNode<MYVAL,TOVAL>::rright(HSpeedypairNode<MYVAL,TOVAL> * &here)
314 HSpeedypairNode<MYVAL,TOVAL> *tmp = here->left;
315 here->left = tmp->right;
320 template <
class MYVAL,
class TOVAL>
321 void HSpeedypairNode<MYVAL,TOVAL>::drleft(HSpeedypairNode<MYVAL,TOVAL> * &here)
323 HSpeedypairNode<MYVAL,TOVAL> *tmp2;
324 HSpeedypairNode<MYVAL,TOVAL> *tmp1;
328 tmp2->right = tmp1->left;
331 HSpeedypairNode<MYVAL,TOVAL> * tmp4 = here->left;
332 here->left = tmp4->right;
337 template <
class MYVAL,
class TOVAL>
338 void HSpeedypairNode<MYVAL,TOVAL>::drright(HSpeedypairNode<MYVAL,TOVAL> * &here)
340 HSpeedypairNode<MYVAL,TOVAL> *tmp2;
341 HSpeedypairNode<MYVAL,TOVAL> *tmp1;
345 tmp2->left = tmp1->right;
348 HSpeedypairNode<MYVAL,TOVAL> *tmp4 = here->right;
349 here->right = tmp4->left;
355 template <
class MYVAL,
class TOVAL>
356 int HSpeedypairNode<MYVAL,TOVAL>::height(HSpeedypairNode<MYVAL,TOVAL> *t)
363 return 1 + max((HSpeedypairNode<MYVAL,TOVAL>::height(t->left)),
364 (HSpeedypairNode<MYVAL,TOVAL>::height(t->right)));
367 template <
class MYVAL,
class TOVAL>
368 HSpeedypairNode<MYVAL,TOVAL> * HSpeedypairNode<MYVAL,TOVAL>::find_min(HSpeedypairNode<MYVAL,TOVAL> *t)
376 return(HSpeedypairNode<MYVAL,TOVAL>::find_min(t->left));
379 template <
class MYVAL,
class TOVAL>
380 void HSpeedypairNode<MYVAL,TOVAL>::inorder(HSpeedypairNode<MYVAL,TOVAL> *here,
int *seq,
void *array,
int mode)
382 if(here->left != NULL)
383 HSpeedypairNode<MYVAL,TOVAL>::inorder(here->left ,seq,array,mode);
391 ((MYVAL *)array)[(*seq)++] = here->data;
393 if(here->right != NULL)
394 HSpeedypairNode<MYVAL,TOVAL>::inorder(here->right,seq,array,mode);
397 template <
class KEY,
class VALUE>
400 arrayk =
new KEY[num()];
402 HSpeedypairNode<KEY,VALUE>::inorder(root_key,&seq,arrayk);
406 #ifndef ONE_SIDE_ONLY
407 template <
class KEY,
class VALUE>
410 arrayv =
new VALUE[num()];
412 HSpeedypairNode<VALUE,KEY>::inorder(root_value,&seq,arrayv);
417 template <
class KEY,
class VALUE>
422 HSpeedypairNode<KEY,VALUE>::inorder(root_key,&seq,arrayp,1);
VALUE value_notfound_data