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