glpavl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* glpavl.h (binary search tree) */
  2. /***********************************************************************
  3. * This code is part of GLPK (GNU Linear Programming Kit).
  4. *
  5. * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
  6. * 2009, 2010 Andrew Makhorin, Department for Applied Informatics,
  7. * Moscow Aviation Institute, Moscow, Russia. All rights reserved.
  8. * E-mail: <mao@gnu.org>.
  9. *
  10. * GLPK is free software: you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * GLPK is distributed in the hope that it will be useful, but WITHOUT
  16. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  17. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  18. * License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GLPK. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************/
  23. #ifndef GLPAVL_H
  24. #define GLPAVL_H
  25. #include "glpdmp.h"
  26. typedef struct AVL AVL;
  27. typedef struct AVLNODE AVLNODE;
  28. struct AVL
  29. { /* AVL tree (Adelson-Velsky & Landis binary search tree) */
  30. DMP *pool;
  31. /* memory pool for allocating nodes */
  32. AVLNODE *root;
  33. /* pointer to the root node */
  34. int (*fcmp)(void *info, const void *key1, const void *key2);
  35. /* application-defined key comparison routine */
  36. void *info;
  37. /* transit pointer passed to the routine fcmp */
  38. int size;
  39. /* the tree size (the total number of nodes) */
  40. int height;
  41. /* the tree height */
  42. };
  43. struct AVLNODE
  44. { /* node of AVL tree */
  45. const void *key;
  46. /* pointer to the node key (data structure for representing keys
  47. is supplied by the application) */
  48. int rank;
  49. /* node rank = relative position of the node in its own subtree =
  50. the number of nodes in the left subtree plus one */
  51. int type;
  52. /* reserved for the application specific information */
  53. void *link;
  54. /* reserved for the application specific information */
  55. AVLNODE *up;
  56. /* pointer to the parent node */
  57. short int flag;
  58. /* node flag:
  59. 0 - this node is the left child of its parent (or this node is
  60. the root of the tree and has no parent)
  61. 1 - this node is the right child of its parent */
  62. short int bal;
  63. /* node balance = the difference between heights of the right and
  64. left subtrees:
  65. -1 - the left subtree is higher than the right one;
  66. 0 - the left and right subtrees have the same height;
  67. +1 - the left subtree is lower than the right one */
  68. AVLNODE *left;
  69. /* pointer to the root of the left subtree */
  70. AVLNODE *right;
  71. /* pointer to the root of the right subtree */
  72. };
  73. #define avl_create_tree _glp_avl_create_tree
  74. AVL *avl_create_tree(int (*fcmp)(void *info, const void *key1,
  75. const void *key2), void *info);
  76. /* create AVL tree */
  77. #define avl_strcmp _glp_avl_strcmp
  78. int avl_strcmp(void *info, const void *key1, const void *key2);
  79. /* compare character string keys */
  80. #define avl_insert_node _glp_avl_insert_node
  81. AVLNODE *avl_insert_node(AVL *tree, const void *key);
  82. /* insert new node into AVL tree */
  83. #define avl_set_node_type _glp_avl_set_node_type
  84. void avl_set_node_type(AVLNODE *node, int type);
  85. /* assign the type field of specified node */
  86. #define avl_set_node_link _glp_avl_set_node_link
  87. void avl_set_node_link(AVLNODE *node, void *link);
  88. /* assign the link field of specified node */
  89. #define avl_find_node _glp_avl_find_node
  90. AVLNODE *avl_find_node(AVL *tree, const void *key);
  91. /* find node in AVL tree */
  92. #define avl_get_node_type _glp_avl_get_node_type
  93. int avl_get_node_type(AVLNODE *node);
  94. /* retrieve the type field of specified node */
  95. #define avl_get_node_link _glp_avl_get_node_link
  96. void *avl_get_node_link(AVLNODE *node);
  97. /* retrieve the link field of specified node */
  98. #define avl_delete_node _glp_avl_delete_node
  99. void avl_delete_node(AVL *tree, AVLNODE *node);
  100. /* delete specified node from AVL tree */
  101. #define avl_delete_tree _glp_avl_delete_tree
  102. void avl_delete_tree(AVL *tree);
  103. /* delete AVL tree */
  104. #endif
  105. /* eof */