version 2.6, 1993/05/05 14:22:16
|
version 2.7, 1993/05/06 15:08:59
|
Line 9
|
Line 9
|
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
|
|
#define MAX(a,b) ((a)>(b)?(a):(b)) |
#define MAXIMUM(a,b) ((a)>(b)?(a):(b)) |
|
|
|
|
|
|
Line 33 PUBLIC HTBTree * HTBTree_new ARGS1(HTCom
|
Line 33 PUBLIC HTBTree * HTBTree_new ARGS1(HTCom
|
|
|
|
|
|
|
|
PRIVATE void HTBTElement_free ARGS1(HTBTElement*, element) |
|
/********************************************************** |
|
** This void will free the memory allocated for one element |
|
*/ |
|
{ |
|
if (element->left != NULL) HTBTElement_free(element->left); |
|
if (element->right != NULL) HTBTElement_free(element->right); |
|
free(element); |
|
} |
|
|
PUBLIC void HTBTree_free ARGS1(HTBTree*, tree) |
PUBLIC void HTBTree_free ARGS1(HTBTree*, tree) |
/************************************************************** |
/************************************************************** |
Line 46 PUBLIC void HTBTree_free ARGS1(HTBTree*,
|
Line 55 PUBLIC void HTBTree_free ARGS1(HTBTree*,
|
|
|
|
|
|
|
|
PRIVATE void HTBTElementAndObject_free ARGS1(HTBTElement*, element) |
PRIVATE void HTBTElement_free ARGS1(HTBTElement*, element) |
|
/********************************************************** |
/********************************************************** |
** This void will free the memory allocated for one element |
** This void will free the memory allocated for one element |
*/ |
*/ |
{ |
{ |
if (element->left != NULL) HTBTElement_free(element->left); |
if (element->left != NULL) HTBTElementAndObject_free(element->left); |
if (element->right != NULL) HTBTElement_free(element->right); |
if (element->right != NULL) HTBTElementAndObject_free(element->right); |
free (element); |
free(element->object); |
|
free(element); |
|
} |
|
|
|
PUBLIC void HTBTreeAndObject_free ARGS1(HTBTree*, tree) |
|
/************************************************************** |
|
** This void will free the memory allocated for the whole tree |
|
*/ |
|
{ |
|
HTBTElementAndObject_free(tree->top); |
|
free(tree); |
} |
} |
|
|
|
|
Line 83 PUBLIC void HTBTree_add ARGS2(
|
Line 101 PUBLIC void HTBTree_add ARGS2(
|
** father_of_forefather and forefather_of_element are pointers that are used |
** father_of_forefather and forefather_of_element are pointers that are used |
** to modify the depths of upper elements, when needed. |
** to modify the depths of upper elements, when needed. |
** |
** |
** father_found indicates by a value NO when the future father of "object" is |
** father_found indicates by a value NO when the future father of "object" |
** found. |
** is found. |
** top_found indicates by a value NO when, in case of a difference of depths |
** top_found indicates by a value NO when, in case of a difference of depths |
** < 2, the top of the tree is encountered and forbids any further try to |
** < 2, the top of the tree is encountered and forbids any further try to |
** balance the tree. |
** balance the tree. |
Line 174 PUBLIC void HTBTree_add ARGS2(
|
Line 192 PUBLIC void HTBTree_add ARGS2(
|
{ |
{ |
depth = father_of_forefather->left_depth; |
depth = father_of_forefather->left_depth; |
father_of_forefather->left_depth = 1 |
father_of_forefather->left_depth = 1 |
+ MAX(forefather_of_element->right_depth, |
+ MAXIMUM(forefather_of_element->right_depth, |
forefather_of_element->left_depth); |
forefather_of_element->left_depth); |
depth2 = father_of_forefather->left_depth; |
depth2 = father_of_forefather->left_depth; |
} |
} |
Line 182 PUBLIC void HTBTree_add ARGS2(
|
Line 200 PUBLIC void HTBTree_add ARGS2(
|
{ |
{ |
depth = father_of_forefather->right_depth; |
depth = father_of_forefather->right_depth; |
father_of_forefather->right_depth = 1 |
father_of_forefather->right_depth = 1 |
+ MAX(forefather_of_element->right_depth, |
+ MAXIMUM(forefather_of_element->right_depth, |
forefather_of_element->left_depth); |
forefather_of_element->left_depth); |
depth2 = father_of_forefather->right_depth; |
depth2 = father_of_forefather->right_depth; |
} |
} |
Line 226 PUBLIC void HTBTree_add ARGS2(
|
Line 244 PUBLIC void HTBTree_add ARGS2(
|
added_element = father_of_element->left; |
added_element = father_of_element->left; |
father_of_element->left_depth = added_element->right_depth; |
father_of_element->left_depth = added_element->right_depth; |
added_element->right_depth = 1 |
added_element->right_depth = 1 |
+ MAX(father_of_element->right_depth, |
+ MAXIMUM(father_of_element->right_depth, |
father_of_element->left_depth); |
father_of_element->left_depth); |
if (father_of_element->up != NULL) |
if (father_of_element->up != NULL) |
{ |
{ |
Line 239 PUBLIC void HTBTree_add ARGS2(
|
Line 257 PUBLIC void HTBTree_add ARGS2(
|
{ |
{ |
depth = father_of_forefather->left_depth; |
depth = father_of_forefather->left_depth; |
father_of_forefather->left_depth = 1 |
father_of_forefather->left_depth = 1 |
+ MAX(forefather_of_element->left_depth, |
+ MAXIMUM(forefather_of_element->left_depth, |
forefather_of_element->right_depth); |
forefather_of_element->right_depth); |
depth2 = father_of_forefather->left_depth; |
depth2 = father_of_forefather->left_depth; |
} |
} |
Line 247 PUBLIC void HTBTree_add ARGS2(
|
Line 265 PUBLIC void HTBTree_add ARGS2(
|
{ |
{ |
depth = father_of_forefather->right_depth; |
depth = father_of_forefather->right_depth; |
father_of_forefather->right_depth = 1 |
father_of_forefather->right_depth = 1 |
+ MAX(forefather_of_element->left_depth, |
+ MAXIMUM(forefather_of_element->left_depth, |
forefather_of_element->right_depth); |
forefather_of_element->right_depth); |
depth2 = father_of_forefather->right_depth; |
depth2 = father_of_forefather->right_depth; |
} |
} |
forefather_of_element = father_of_forefather; |
forefather_of_element = father_of_forefather; |
father_of_forefather = father_of_forefather->up; |
father_of_forefather = father_of_forefather->up; |
} while ((depth != depth2) && (father_of_forefather != NULL)); |
} while ((depth != depth2) && |
|
(father_of_forefather != NULL)); |
father_of_forefather = father_of_element->up; |
father_of_forefather = father_of_element->up; |
if (father_of_forefather->left == father_of_element) |
if (father_of_forefather->left == father_of_element) |
{ |
{ |
Line 310 PUBLIC void HTBTree_add ARGS2(
|
Line 329 PUBLIC void HTBTree_add ARGS2(
|
added_element = father_of_element->right; |
added_element = father_of_element->right; |
father_of_element->right_depth = added_element->left_depth; |
father_of_element->right_depth = added_element->left_depth; |
added_element->left_depth = 1 + |
added_element->left_depth = 1 + |
MAX(father_of_element->right_depth, |
MAXIMUM(father_of_element->right_depth, |
father_of_element->left_depth); |
father_of_element->left_depth); |
if (father_of_element->up != NULL) |
if (father_of_element->up != NULL) |
{ |
{ |
Line 322 PUBLIC void HTBTree_add ARGS2(
|
Line 341 PUBLIC void HTBTree_add ARGS2(
|
{ |
{ |
depth = father_of_forefather->left_depth; |
depth = father_of_forefather->left_depth; |
father_of_forefather->left_depth = 1 |
father_of_forefather->left_depth = 1 |
+ MAX(added_element->left_depth, |
+ MAXIMUM(added_element->left_depth, |
added_element->right_depth); |
added_element->right_depth); |
depth2 = father_of_forefather->left_depth; |
depth2 = father_of_forefather->left_depth; |
} |
} |
else |
else |
{ |
{ |
depth = father_of_forefather->right_depth; |
depth = father_of_forefather->right_depth; |
father_of_forefather->right_depth = 1 |
father_of_forefather->right_depth = 1 |
+ MAX(added_element->left_depth, |
+ MAXIMUM(added_element->left_depth, |
added_element->right_depth); |
added_element->right_depth); |
depth2 = father_of_forefather->right_depth; |
depth2 = father_of_forefather->right_depth; |
} |
} |
father_of_forefather = father_of_forefather->up; |
father_of_forefather = father_of_forefather->up; |
forefather_of_element = father_of_forefather; |
forefather_of_element = father_of_forefather; |
} while ((depth != depth2) && (father_of_forefather != NULL)); |
} while ((depth != depth2) && |
|
(father_of_forefather != NULL)); |
father_of_forefather = father_of_element->up; |
father_of_forefather = father_of_element->up; |
if (father_of_forefather->left == father_of_element) |
if (father_of_forefather->left == father_of_element) |
{ |
{ |
Line 430 PUBLIC HTBTElement * HTBTree_next ARGS2(
|
Line 450 PUBLIC HTBTElement * HTBTree_next ARGS2(
|
else |
else |
{ |
{ |
father_of_forefather = father_of_element->up; |
father_of_forefather = father_of_element->up; |
while (father_of_forefather->right == father_of_element) |
while (father_of_forefather && |
{ |
(father_of_forefather->right == father_of_element)) |
father_of_element = father_of_forefather; |
{ |
father_of_forefather = father_of_element->up; |
father_of_element = father_of_forefather; |
} |
father_of_forefather = father_of_element->up; |
|
} |
father_of_element = father_of_forefather; |
father_of_element = father_of_forefather; |
} |
} |
} |
} |
Line 444 PUBLIC HTBTElement * HTBTree_next ARGS2(
|
Line 465 PUBLIC HTBTElement * HTBTree_next ARGS2(
|
*/ |
*/ |
if (father_of_element != NULL) |
if (father_of_element != NULL) |
{ |
{ |
printf("\nObject = %s\t",father_of_element->object); |
printf("\nObject = %s\t",(char *)father_of_element->object); |
if (father_of_element->up != NULL) |
if (father_of_element->up != NULL) |
printf("Objet du pere = %s\n",father_of_element->up->object); |
printf("Objet du pere = %s\n", |
|
(char *)father_of_element->up->object); |
else printf("Pas de Pere\n"); |
else printf("Pas de Pere\n"); |
if (father_of_element->left != NULL) |
if (father_of_element->left != NULL) |
printf("Objet du fils gauche = %s\t",father_of_element->left->object); |
printf("Objet du fils gauche = %s\t", |
|
(char *)father_of_element->left->object); |
else printf("Pas de fils gauche\t"); |
else printf("Pas de fils gauche\t"); |
if (father_of_element->right != NULL) |
if (father_of_element->right != NULL) |
printf("Objet du fils droit = %s\n",father_of_element->right->object); |
printf("Objet du fils droit = %s\n", |
|
(char *)father_of_element->right->object); |
else printf("Pas de fils droit\n"); |
else printf("Pas de fils droit\n"); |
printf("Profondeur gauche = %i\t",father_of_element->left_depth); |
printf("Profondeur gauche = %i\t",father_of_element->left_depth); |
printf("Profondeur droite = %i\n",father_of_element->right_depth); |
printf("Profondeur droite = %i\n",father_of_element->right_depth); |