Projet Informatique Théorique 2016
Instructions et Documentation
avl.c
1 /* libavl - library for manipulation of binary trees.
2  Copyright (C) 1998-2002 Free Software Foundation, Inc.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License as
6  published by the Free Software Foundation; either version 2 of the
7  License, or (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful, but
10  WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  See the GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program; if not, write to the Free Software
16  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  02111-1307, USA.
18 
19  The author may be contacted at <blp@gnu.org> on the Internet, or
20  write to Ben Pfaff, Stanford University, Computer Science Dept., 353
21  Serra Mall, Stanford CA 94305, USA.
22 */
23 
24 #include <assert.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include "avl.h"
29 
30 /* Creates and returns a new table
31  with comparison function |compare| using parameter |param|
32  and memory allocator |allocator|.
33  Returns |NULL| if memory allocation failed. */
34 struct avl_table *
35 avl_create (avl_comparison_func *compare, void *param,
36  struct libavl_allocator *allocator)
37 {
38  struct avl_table *tree;
39 
40  assert (compare != NULL);
41 
42  if (allocator == NULL)
43  allocator = &avl_allocator_default;
44 
45  tree = allocator->libavl_malloc (allocator, sizeof *tree);
46  if (tree == NULL)
47  return NULL;
48 
49  tree->avl_root = NULL;
50  tree->avl_compare = compare;
51  tree->avl_param = param;
52  tree->avl_alloc = allocator;
53  tree->avl_count = 0;
54  tree->avl_generation = 0;
55 
56  return tree;
57 }
58 
59 /* Search |tree| for an item matching |item|, and return it if found.
60  Otherwise return |NULL|. */
61 void *
62 avl_find (const struct avl_table *tree, const void *item)
63 {
64  const struct avl_node *p;
65 
66  assert (tree != NULL && item != NULL);
67  for (p = tree->avl_root; p != NULL; )
68  {
69  int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
70 
71  if (cmp < 0)
72  p = p->avl_link[0];
73  else if (cmp > 0)
74  p = p->avl_link[1];
75  else /* |cmp == 0| */
76  return p->avl_data;
77  }
78 
79  return NULL;
80 }
81 
82 /* Inserts |item| into |tree| and returns a pointer to |item|'s address.
83  If a duplicate item is found in the tree,
84  returns a pointer to the duplicate without inserting |item|.
85  Returns |NULL| in case of memory allocation failure. */
86 void **
87 avl_probe (struct avl_table *tree, void *item)
88 {
89  struct avl_node *y, *z; /* Top node to update balance factor, and parent. */
90  struct avl_node *p, *q; /* Iterator, and parent. */
91  struct avl_node *n; /* Newly inserted node. */
92  struct avl_node *w; /* New root of rebalanced subtree. */
93  int dir; /* Direction to descend. */
94 
95  unsigned char da[AVL_MAX_HEIGHT]; /* Cached comparison results. */
96  int k = 0; /* Number of cached results. */
97 
98  assert (tree != NULL && item != NULL);
99 
100  z = (struct avl_node *) &tree->avl_root;
101  y = tree->avl_root;
102  dir = 0;
103  for (q = z, p = y; p != NULL; q = p, p = p->avl_link[dir])
104  {
105  int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
106  if (cmp == 0)
107  return &p->avl_data;
108 
109  if (p->avl_balance != 0)
110  z = q, y = p, k = 0;
111  da[k++] = dir = cmp > 0;
112  }
113 
114  n = q->avl_link[dir] =
115  tree->avl_alloc->libavl_malloc (tree->avl_alloc, sizeof *n);
116  if (n == NULL)
117  return NULL;
118 
119  tree->avl_count++;
120  n->avl_data = item;
121  n->avl_link[0] = n->avl_link[1] = NULL;
122  n->avl_balance = 0;
123  if (y == NULL)
124  return &n->avl_data;
125 
126  for (p = y, k = 0; p != n; p = p->avl_link[da[k]], k++)
127  if (da[k] == 0)
128  p->avl_balance--;
129  else
130  p->avl_balance++;
131 
132  if (y->avl_balance == -2)
133  {
134  struct avl_node *x = y->avl_link[0];
135  if (x->avl_balance == -1)
136  {
137  w = x;
138  y->avl_link[0] = x->avl_link[1];
139  x->avl_link[1] = y;
140  x->avl_balance = y->avl_balance = 0;
141  }
142  else
143  {
144  assert (x->avl_balance == +1);
145  w = x->avl_link[1];
146  x->avl_link[1] = w->avl_link[0];
147  w->avl_link[0] = x;
148  y->avl_link[0] = w->avl_link[1];
149  w->avl_link[1] = y;
150  if (w->avl_balance == -1)
151  x->avl_balance = 0, y->avl_balance = +1;
152  else if (w->avl_balance == 0)
153  x->avl_balance = y->avl_balance = 0;
154  else /* |w->avl_balance == +1| */
155  x->avl_balance = -1, y->avl_balance = 0;
156  w->avl_balance = 0;
157  }
158  }
159  else if (y->avl_balance == +2)
160  {
161  struct avl_node *x = y->avl_link[1];
162  if (x->avl_balance == +1)
163  {
164  w = x;
165  y->avl_link[1] = x->avl_link[0];
166  x->avl_link[0] = y;
167  x->avl_balance = y->avl_balance = 0;
168  }
169  else
170  {
171  assert (x->avl_balance == -1);
172  w = x->avl_link[0];
173  x->avl_link[0] = w->avl_link[1];
174  w->avl_link[1] = x;
175  y->avl_link[1] = w->avl_link[0];
176  w->avl_link[0] = y;
177  if (w->avl_balance == +1)
178  x->avl_balance = 0, y->avl_balance = -1;
179  else if (w->avl_balance == 0)
180  x->avl_balance = y->avl_balance = 0;
181  else /* |w->avl_balance == -1| */
182  x->avl_balance = +1, y->avl_balance = 0;
183  w->avl_balance = 0;
184  }
185  }
186  else
187  return &n->avl_data;
188  z->avl_link[y != z->avl_link[0]] = w;
189 
190  tree->avl_generation++;
191  return &n->avl_data;
192 }
193 
194 /* Inserts |item| into |table|.
195  Returns |NULL| if |item| was successfully inserted
196  or if a memory allocation error occurred.
197  Otherwise, returns the duplicate item. */
198 void *
199 avl_insert (struct avl_table *table, void *item)
200 {
201  void **p = avl_probe (table, item);
202  return p == NULL || *p == item ? NULL : *p;
203 }
204 
205 /* Inserts |item| into |table|, replacing any duplicate item.
206  Returns |NULL| if |item| was inserted without replacing a duplicate,
207  or if a memory allocation error occurred.
208  Otherwise, returns the item that was replaced. */
209 void *
210 avl_replace (struct avl_table *table, void *item)
211 {
212  void **p = avl_probe (table, item);
213  if (p == NULL || *p == item)
214  return NULL;
215  else
216  {
217  void *r = *p;
218  *p = item;
219  return r;
220  }
221 }
222 
223 /* Deletes from |tree| and returns an item matching |item|.
224  Returns a null pointer if no matching item found. */
225 void *
226 avl_delete (struct avl_table *tree, const void *item)
227 {
228  /* Stack of nodes. */
229  struct avl_node *pa[AVL_MAX_HEIGHT]; /* Nodes. */
230  unsigned char da[AVL_MAX_HEIGHT]; /* |avl_link[]| indexes. */
231  int k; /* Stack pointer. */
232 
233  struct avl_node *p; /* Traverses tree to find node to delete. */
234  int cmp; /* Result of comparison between |item| and |p|. */
235 
236  assert (tree != NULL && item != NULL);
237 
238  k = 0;
239  p = (struct avl_node *) &tree->avl_root;
240  for (cmp = -1; cmp != 0;
241  cmp = tree->avl_compare (item, p->avl_data, tree->avl_param))
242  {
243  int dir = cmp > 0;
244 
245  pa[k] = p;
246  da[k++] = dir;
247 
248  p = p->avl_link[dir];
249  if (p == NULL)
250  return NULL;
251  }
252  item = p->avl_data;
253 
254  if (p->avl_link[1] == NULL)
255  pa[k - 1]->avl_link[da[k - 1]] = p->avl_link[0];
256  else
257  {
258  struct avl_node *r = p->avl_link[1];
259  if (r->avl_link[0] == NULL)
260  {
261  r->avl_link[0] = p->avl_link[0];
262  r->avl_balance = p->avl_balance;
263  pa[k - 1]->avl_link[da[k - 1]] = r;
264  da[k] = 1;
265  pa[k++] = r;
266  }
267  else
268  {
269  struct avl_node *s;
270  int j = k++;
271 
272  for (;;)
273  {
274  da[k] = 0;
275  pa[k++] = r;
276  s = r->avl_link[0];
277  if (s->avl_link[0] == NULL)
278  break;
279 
280  r = s;
281  }
282 
283  s->avl_link[0] = p->avl_link[0];
284  r->avl_link[0] = s->avl_link[1];
285  s->avl_link[1] = p->avl_link[1];
286  s->avl_balance = p->avl_balance;
287 
288  pa[j - 1]->avl_link[da[j - 1]] = s;
289  da[j] = 1;
290  pa[j] = s;
291  }
292  }
293 
294  tree->avl_alloc->libavl_free (tree->avl_alloc, p);
295 
296  assert (k > 0);
297  while (--k > 0)
298  {
299  struct avl_node *y = pa[k];
300 
301  if (da[k] == 0)
302  {
303  y->avl_balance++;
304  if (y->avl_balance == +1)
305  break;
306  else if (y->avl_balance == +2)
307  {
308  struct avl_node *x = y->avl_link[1];
309  if (x->avl_balance == -1)
310  {
311  struct avl_node *w;
312  assert (x->avl_balance == -1);
313  w = x->avl_link[0];
314  x->avl_link[0] = w->avl_link[1];
315  w->avl_link[1] = x;
316  y->avl_link[1] = w->avl_link[0];
317  w->avl_link[0] = y;
318  if (w->avl_balance == +1)
319  x->avl_balance = 0, y->avl_balance = -1;
320  else if (w->avl_balance == 0)
321  x->avl_balance = y->avl_balance = 0;
322  else /* |w->avl_balance == -1| */
323  x->avl_balance = +1, y->avl_balance = 0;
324  w->avl_balance = 0;
325  pa[k - 1]->avl_link[da[k - 1]] = w;
326  }
327  else
328  {
329  y->avl_link[1] = x->avl_link[0];
330  x->avl_link[0] = y;
331  pa[k - 1]->avl_link[da[k - 1]] = x;
332  if (x->avl_balance == 0)
333  {
334  x->avl_balance = -1;
335  y->avl_balance = +1;
336  break;
337  }
338  else
339  x->avl_balance = y->avl_balance = 0;
340  }
341  }
342  }
343  else
344  {
345  y->avl_balance--;
346  if (y->avl_balance == -1)
347  break;
348  else if (y->avl_balance == -2)
349  {
350  struct avl_node *x = y->avl_link[0];
351  if (x->avl_balance == +1)
352  {
353  struct avl_node *w;
354  assert (x->avl_balance == +1);
355  w = x->avl_link[1];
356  x->avl_link[1] = w->avl_link[0];
357  w->avl_link[0] = x;
358  y->avl_link[0] = w->avl_link[1];
359  w->avl_link[1] = y;
360  if (w->avl_balance == -1)
361  x->avl_balance = 0, y->avl_balance = +1;
362  else if (w->avl_balance == 0)
363  x->avl_balance = y->avl_balance = 0;
364  else /* |w->avl_balance == +1| */
365  x->avl_balance = -1, y->avl_balance = 0;
366  w->avl_balance = 0;
367  pa[k - 1]->avl_link[da[k - 1]] = w;
368  }
369  else
370  {
371  y->avl_link[0] = x->avl_link[1];
372  x->avl_link[1] = y;
373  pa[k - 1]->avl_link[da[k - 1]] = x;
374  if (x->avl_balance == 0)
375  {
376  x->avl_balance = +1;
377  y->avl_balance = -1;
378  break;
379  }
380  else
381  x->avl_balance = y->avl_balance = 0;
382  }
383  }
384  }
385  }
386 
387  tree->avl_count--;
388  tree->avl_generation++;
389  return (void *) item;
390 }
391 
392 /* Refreshes the stack of parent pointers in |trav|
393  and updates its generation number. */
394 static void
395 trav_refresh (struct avl_traverser *trav)
396 {
397  assert (trav != NULL);
398 
399  trav->avl_generation = trav->avl_table->avl_generation;
400 
401  if (trav->avl_node != NULL)
402  {
403  avl_comparison_func *cmp = trav->avl_table->avl_compare;
404  void *param = trav->avl_table->avl_param;
405  struct avl_node *node = trav->avl_node;
406  struct avl_node *i;
407 
408  trav->avl_height = 0;
409  for (i = trav->avl_table->avl_root; i != node; )
410  {
411  assert (trav->avl_height < AVL_MAX_HEIGHT);
412  assert (i != NULL);
413 
414  trav->avl_stack[trav->avl_height++] = i;
415  i = i->avl_link[cmp (node->avl_data, i->avl_data, param) > 0];
416  }
417  }
418 }
419 
420 /* Initializes |trav| for use with |tree|
421  and selects the null node. */
422 void
423 avl_t_init (struct avl_traverser *trav, struct avl_table *tree)
424 {
425  trav->avl_table = tree;
426  trav->avl_node = NULL;
427  trav->avl_height = 0;
428  trav->avl_generation = tree->avl_generation;
429 }
430 
431 /* Initializes |trav| for |tree|
432  and selects and returns a pointer to its least-valued item.
433  Returns |NULL| if |tree| contains no nodes. */
434 void *
435 avl_t_first (struct avl_traverser *trav, struct avl_table *tree)
436 {
437  struct avl_node *x;
438 
439  assert (tree != NULL && trav != NULL);
440 
441  trav->avl_table = tree;
442  trav->avl_height = 0;
443  trav->avl_generation = tree->avl_generation;
444 
445  x = tree->avl_root;
446  if (x != NULL)
447  while (x->avl_link[0] != NULL)
448  {
449  assert (trav->avl_height < AVL_MAX_HEIGHT);
450  trav->avl_stack[trav->avl_height++] = x;
451  x = x->avl_link[0];
452  }
453  trav->avl_node = x;
454 
455  return x != NULL ? x->avl_data : NULL;
456 }
457 
458 /* Initializes |trav| for |tree|
459  and selects and returns a pointer to its greatest-valued item.
460  Returns |NULL| if |tree| contains no nodes. */
461 void *
462 avl_t_last (struct avl_traverser *trav, struct avl_table *tree)
463 {
464  struct avl_node *x;
465 
466  assert (tree != NULL && trav != NULL);
467 
468  trav->avl_table = tree;
469  trav->avl_height = 0;
470  trav->avl_generation = tree->avl_generation;
471 
472  x = tree->avl_root;
473  if (x != NULL)
474  while (x->avl_link[1] != NULL)
475  {
476  assert (trav->avl_height < AVL_MAX_HEIGHT);
477  trav->avl_stack[trav->avl_height++] = x;
478  x = x->avl_link[1];
479  }
480  trav->avl_node = x;
481 
482  return x != NULL ? x->avl_data : NULL;
483 }
484 
485 /* Searches for |item| in |tree|.
486  If found, initializes |trav| to the item found and returns the item
487  as well.
488  If there is no matching item, initializes |trav| to the null item
489  and returns |NULL|. */
490 void *
491 avl_t_find (struct avl_traverser *trav, struct avl_table *tree, void *item)
492 {
493  struct avl_node *p, *q;
494 
495  assert (trav != NULL && tree != NULL && item != NULL);
496  trav->avl_table = tree;
497  trav->avl_height = 0;
498  trav->avl_generation = tree->avl_generation;
499  for (p = tree->avl_root; p != NULL; p = q)
500  {
501  int cmp = tree->avl_compare (item, p->avl_data, tree->avl_param);
502 
503  if (cmp < 0)
504  q = p->avl_link[0];
505  else if (cmp > 0)
506  q = p->avl_link[1];
507  else /* |cmp == 0| */
508  {
509  trav->avl_node = p;
510  return p->avl_data;
511  }
512 
513  assert (trav->avl_height < AVL_MAX_HEIGHT);
514  trav->avl_stack[trav->avl_height++] = p;
515  }
516 
517  trav->avl_height = 0;
518  trav->avl_node = NULL;
519  return NULL;
520 }
521 
522 /* Attempts to insert |item| into |tree|.
523  If |item| is inserted successfully, it is returned and |trav| is
524  initialized to its location.
525  If a duplicate is found, it is returned and |trav| is initialized to
526  its location. No replacement of the item occurs.
527  If a memory allocation failure occurs, |NULL| is returned and |trav|
528  is initialized to the null item. */
529 void *
530 avl_t_insert (struct avl_traverser *trav, struct avl_table *tree, void *item)
531 {
532  void **p;
533 
534  assert (trav != NULL && tree != NULL && item != NULL);
535 
536  p = avl_probe (tree, item);
537  if (p != NULL)
538  {
539  trav->avl_table = tree;
540  trav->avl_node =
541  ((struct avl_node *)
542  ((char *) p - offsetof (struct avl_node, avl_data)));
543  trav->avl_generation = tree->avl_generation - 1;
544  return *p;
545  }
546  else
547  {
548  avl_t_init (trav, tree);
549  return NULL;
550  }
551 }
552 
553 /* Initializes |trav| to have the same current node as |src|. */
554 void *
555 avl_t_copy (struct avl_traverser *trav, const struct avl_traverser *src)
556 {
557  assert (trav != NULL && src != NULL);
558 
559  if (trav != src)
560  {
561  trav->avl_table = src->avl_table;
562  trav->avl_node = src->avl_node;
563  trav->avl_generation = src->avl_generation;
564  if (trav->avl_generation == trav->avl_table->avl_generation)
565  {
566  trav->avl_height = src->avl_height;
567  memcpy (trav->avl_stack, (const void *) src->avl_stack,
568  sizeof *trav->avl_stack * trav->avl_height);
569  }
570  }
571 
572  return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
573 }
574 
575 /* Returns the next data item in inorder
576  within the tree being traversed with |trav|,
577  or if there are no more data items returns |NULL|. */
578 void *
579 avl_t_next (struct avl_traverser *trav)
580 {
581  struct avl_node *x;
582 
583  assert (trav != NULL);
584 
585  if (trav->avl_generation != trav->avl_table->avl_generation)
586  trav_refresh (trav);
587 
588  x = trav->avl_node;
589  if (x == NULL)
590  {
591  return avl_t_first (trav, trav->avl_table);
592  }
593  else if (x->avl_link[1] != NULL)
594  {
595  assert (trav->avl_height < AVL_MAX_HEIGHT);
596  trav->avl_stack[trav->avl_height++] = x;
597  x = x->avl_link[1];
598 
599  while (x->avl_link[0] != NULL)
600  {
601  assert (trav->avl_height < AVL_MAX_HEIGHT);
602  trav->avl_stack[trav->avl_height++] = x;
603  x = x->avl_link[0];
604  }
605  }
606  else
607  {
608  struct avl_node *y;
609 
610  do
611  {
612  if (trav->avl_height == 0)
613  {
614  trav->avl_node = NULL;
615  return NULL;
616  }
617 
618  y = x;
619  x = trav->avl_stack[--trav->avl_height];
620  }
621  while (y == x->avl_link[1]);
622  }
623  trav->avl_node = x;
624 
625  return x->avl_data;
626 }
627 
628 /* Returns the previous data item in inorder
629  within the tree being traversed with |trav|,
630  or if there are no more data items returns |NULL|. */
631 void *
632 avl_t_prev (struct avl_traverser *trav)
633 {
634  struct avl_node *x;
635 
636  assert (trav != NULL);
637 
638  if (trav->avl_generation != trav->avl_table->avl_generation)
639  trav_refresh (trav);
640 
641  x = trav->avl_node;
642  if (x == NULL)
643  {
644  return avl_t_last (trav, trav->avl_table);
645  }
646  else if (x->avl_link[0] != NULL)
647  {
648  assert (trav->avl_height < AVL_MAX_HEIGHT);
649  trav->avl_stack[trav->avl_height++] = x;
650  x = x->avl_link[0];
651 
652  while (x->avl_link[1] != NULL)
653  {
654  assert (trav->avl_height < AVL_MAX_HEIGHT);
655  trav->avl_stack[trav->avl_height++] = x;
656  x = x->avl_link[1];
657  }
658  }
659  else
660  {
661  struct avl_node *y;
662 
663  do
664  {
665  if (trav->avl_height == 0)
666  {
667  trav->avl_node = NULL;
668  return NULL;
669  }
670 
671  y = x;
672  x = trav->avl_stack[--trav->avl_height];
673  }
674  while (y == x->avl_link[0]);
675  }
676  trav->avl_node = x;
677 
678  return x->avl_data;
679 }
680 
681 /* Returns |trav|'s current item. */
682 void *
683 avl_t_cur ( struct avl_traverser *trav)
684 {
685  assert (trav != NULL);
686 
687  return trav->avl_node != NULL ? trav->avl_node->avl_data : NULL;
688 }
689 
690 /* Replaces the current item in |trav| by |new| and returns the item replaced.
691  |trav| must not have the null item selected.
692  The new item must not upset the ordering of the tree. */
693 void *
694 avl_t_replace (struct avl_traverser *trav, void *new)
695 {
696  void *old;
697 
698  assert (trav != NULL && trav->avl_node != NULL && new != NULL);
699  old = trav->avl_node->avl_data;
700  trav->avl_node->avl_data = new;
701  return old;
702 }
703 
704 static void
705 copy_error_recovery (struct avl_node **stack, int height,
706  struct avl_table *new, avl_item_func *destroy)
707 {
708  assert (stack != NULL && height >= 0 && new != NULL);
709 
710  for (; height > 2; height -= 2)
711  stack[height - 1]->avl_link[1] = NULL;
712  avl_destroy (new, destroy);
713 }
714 
715 /* Copies |org| to a newly created tree, which is returned.
716  If |copy != NULL|, each data item in |org| is first passed to |copy|,
717  and the return values are inserted into the tree,
718  with |NULL| return values taken as indications of failure.
719  On failure, destroys the partially created new tree,
720  applying |destroy|, if non-null, to each item in the new tree so far,
721  and returns |NULL|.
722  If |allocator != NULL|, it is used for allocation in the new tree.
723  Otherwise, the same allocator used for |org| is used. */
724 struct avl_table *
725 avl_copy (const struct avl_table *org, avl_copy_func *copy,
726  avl_item_func *destroy, struct libavl_allocator *allocator)
727 {
728  struct avl_node *stack[2 * (AVL_MAX_HEIGHT + 1)];
729  int height = 0;
730 
731  struct avl_table *new;
732  const struct avl_node *x;
733  struct avl_node *y;
734 
735  assert (org != NULL);
736  new = avl_create (org->avl_compare, org->avl_param,
737  allocator != NULL ? allocator : org->avl_alloc);
738  if (new == NULL)
739  return NULL;
740  new->avl_count = org->avl_count;
741  if (new->avl_count == 0)
742  return new;
743 
744  x = (const struct avl_node *) &org->avl_root;
745  y = (struct avl_node *) &new->avl_root;
746  for (;;)
747  {
748  while (x->avl_link[0] != NULL)
749  {
750  assert (height < 2 * (AVL_MAX_HEIGHT + 1));
751 
752  y->avl_link[0] =
753  new->avl_alloc->libavl_malloc (new->avl_alloc,
754  sizeof *y->avl_link[0]);
755  if (y->avl_link[0] == NULL)
756  {
757  if (y != (struct avl_node *) &new->avl_root)
758  {
759  y->avl_data = NULL;
760  y->avl_link[1] = NULL;
761  }
762 
763  copy_error_recovery (stack, height, new, destroy);
764  return NULL;
765  }
766 
767  stack[height++] = (struct avl_node *) x;
768  stack[height++] = y;
769  x = x->avl_link[0];
770  y = y->avl_link[0];
771  }
772  y->avl_link[0] = NULL;
773 
774  for (;;)
775  {
776  y->avl_balance = x->avl_balance;
777  if (copy == NULL)
778  y->avl_data = x->avl_data;
779  else
780  {
781  y->avl_data = copy (x->avl_data, org->avl_param);
782  if (y->avl_data == NULL)
783  {
784  y->avl_link[1] = NULL;
785  copy_error_recovery (stack, height, new, destroy);
786  return NULL;
787  }
788  }
789 
790  if (x->avl_link[1] != NULL)
791  {
792  y->avl_link[1] =
793  new->avl_alloc->libavl_malloc (new->avl_alloc,
794  sizeof *y->avl_link[1]);
795  if (y->avl_link[1] == NULL)
796  {
797  copy_error_recovery (stack, height, new, destroy);
798  return NULL;
799  }
800 
801  x = x->avl_link[1];
802  y = y->avl_link[1];
803  break;
804  }
805  else
806  y->avl_link[1] = NULL;
807 
808  if (height <= 2)
809  return new;
810 
811  y = stack[--height];
812  x = stack[--height];
813  }
814  }
815 }
816 
817 /* Frees storage allocated for |tree|.
818  If |destroy != NULL|, applies it to each data item in inorder. */
819 void
820 avl_destroy (struct avl_table *tree, avl_item_func *destroy)
821 {
822  struct avl_node *p, *q;
823 
824  assert (tree != NULL);
825 
826  for (p = tree->avl_root; p != NULL; p = q)
827  if (p->avl_link[0] == NULL)
828  {
829  q = p->avl_link[1];
830  if (destroy != NULL && p->avl_data != NULL)
831  destroy (p->avl_data, tree->avl_param);
832  tree->avl_alloc->libavl_free (tree->avl_alloc, p);
833  }
834  else
835  {
836  q = p->avl_link[0];
837  p->avl_link[0] = q->avl_link[1];
838  q->avl_link[1] = p;
839  }
840 
841  tree->avl_alloc->libavl_free (tree->avl_alloc, tree);
842 }
843 
844 /* Allocates |size| bytes of space using |malloc()|.
845  Returns a null pointer if allocation fails. */
846 void *
847 avl_malloc (struct libavl_allocator *allocator, size_t size)
848 {
849  assert (allocator != NULL && size > 0);
850  return malloc (size);
851 }
852 
853 /* Frees |block|. */
854 void
855 avl_free (struct libavl_allocator *allocator, void *block)
856 {
857  assert (allocator != NULL && block != NULL);
858  free (block);
859 }
860 
861 /* Default memory allocator that uses |malloc()| and |free()|. */
862 struct libavl_allocator avl_allocator_default =
863  {
864  avl_malloc,
865  avl_free
866  };
867 
868 #undef NDEBUG
869 #include <assert.h>
870 
871 /* Asserts that |avl_insert()| succeeds at inserting |item| into |table|. */
872 void
873 (avl_assert_insert) (struct avl_table *table, void *item)
874 {
875  void **p = avl_probe (table, item);
876  assert (p != NULL && *p == item);
877 }
878 
879 /* Asserts that |avl_delete()| really removes |item| from |table|,
880  and returns the removed item. */
881 void *
882 (avl_assert_delete) (struct avl_table *table, void *item)
883 {
884  void *p = avl_delete (table, item);
885  assert (p != NULL);
886  return p;
887 }
888 
889 int avl_t_is_null(struct avl_traverser * t){
890  return t->avl_node == NULL;
891 }
Definition: avl.h:67
Definition: avl.h:56