alpar@9: /* glpavl.h (binary search tree) */ alpar@9: alpar@9: /*********************************************************************** alpar@9: * This code is part of GLPK (GNU Linear Programming Kit). alpar@9: * alpar@9: * Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, alpar@9: * 2009, 2010, 2011 Andrew Makhorin, Department for Applied Informatics, alpar@9: * Moscow Aviation Institute, Moscow, Russia. All rights reserved. alpar@9: * E-mail: . alpar@9: * alpar@9: * GLPK is free software: you can redistribute it and/or modify it alpar@9: * under the terms of the GNU General Public License as published by alpar@9: * the Free Software Foundation, either version 3 of the License, or alpar@9: * (at your option) any later version. alpar@9: * alpar@9: * GLPK is distributed in the hope that it will be useful, but WITHOUT alpar@9: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY alpar@9: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public alpar@9: * License for more details. alpar@9: * alpar@9: * You should have received a copy of the GNU General Public License alpar@9: * along with GLPK. If not, see . alpar@9: ***********************************************************************/ alpar@9: alpar@9: #ifndef GLPAVL_H alpar@9: #define GLPAVL_H alpar@9: alpar@9: #include "glpdmp.h" alpar@9: alpar@9: typedef struct AVL AVL; alpar@9: typedef struct AVLNODE AVLNODE; alpar@9: alpar@9: struct AVL alpar@9: { /* AVL tree (Adelson-Velsky & Landis binary search tree) */ alpar@9: DMP *pool; alpar@9: /* memory pool for allocating nodes */ alpar@9: AVLNODE *root; alpar@9: /* pointer to the root node */ alpar@9: int (*fcmp)(void *info, const void *key1, const void *key2); alpar@9: /* application-defined key comparison routine */ alpar@9: void *info; alpar@9: /* transit pointer passed to the routine fcmp */ alpar@9: int size; alpar@9: /* the tree size (the total number of nodes) */ alpar@9: int height; alpar@9: /* the tree height */ alpar@9: }; alpar@9: alpar@9: struct AVLNODE alpar@9: { /* node of AVL tree */ alpar@9: const void *key; alpar@9: /* pointer to the node key (data structure for representing keys alpar@9: is supplied by the application) */ alpar@9: int rank; alpar@9: /* node rank = relative position of the node in its own subtree = alpar@9: the number of nodes in the left subtree plus one */ alpar@9: int type; alpar@9: /* reserved for the application specific information */ alpar@9: void *link; alpar@9: /* reserved for the application specific information */ alpar@9: AVLNODE *up; alpar@9: /* pointer to the parent node */ alpar@9: short int flag; alpar@9: /* node flag: alpar@9: 0 - this node is the left child of its parent (or this node is alpar@9: the root of the tree and has no parent) alpar@9: 1 - this node is the right child of its parent */ alpar@9: short int bal; alpar@9: /* node balance = the difference between heights of the right and alpar@9: left subtrees: alpar@9: -1 - the left subtree is higher than the right one; alpar@9: 0 - the left and right subtrees have the same height; alpar@9: +1 - the left subtree is lower than the right one */ alpar@9: AVLNODE *left; alpar@9: /* pointer to the root of the left subtree */ alpar@9: AVLNODE *right; alpar@9: /* pointer to the root of the right subtree */ alpar@9: }; alpar@9: alpar@9: #define avl_create_tree _glp_avl_create_tree alpar@9: AVL *avl_create_tree(int (*fcmp)(void *info, const void *key1, alpar@9: const void *key2), void *info); alpar@9: /* create AVL tree */ alpar@9: alpar@9: #define avl_strcmp _glp_avl_strcmp alpar@9: int avl_strcmp(void *info, const void *key1, const void *key2); alpar@9: /* compare character string keys */ alpar@9: alpar@9: #define avl_insert_node _glp_avl_insert_node alpar@9: AVLNODE *avl_insert_node(AVL *tree, const void *key); alpar@9: /* insert new node into AVL tree */ alpar@9: alpar@9: #define avl_set_node_type _glp_avl_set_node_type alpar@9: void avl_set_node_type(AVLNODE *node, int type); alpar@9: /* assign the type field of specified node */ alpar@9: alpar@9: #define avl_set_node_link _glp_avl_set_node_link alpar@9: void avl_set_node_link(AVLNODE *node, void *link); alpar@9: /* assign the link field of specified node */ alpar@9: alpar@9: #define avl_find_node _glp_avl_find_node alpar@9: AVLNODE *avl_find_node(AVL *tree, const void *key); alpar@9: /* find node in AVL tree */ alpar@9: alpar@9: #define avl_get_node_type _glp_avl_get_node_type alpar@9: int avl_get_node_type(AVLNODE *node); alpar@9: /* retrieve the type field of specified node */ alpar@9: alpar@9: #define avl_get_node_link _glp_avl_get_node_link alpar@9: void *avl_get_node_link(AVLNODE *node); alpar@9: /* retrieve the link field of specified node */ alpar@9: alpar@9: #define avl_delete_node _glp_avl_delete_node alpar@9: void avl_delete_node(AVL *tree, AVLNODE *node); alpar@9: /* delete specified node from AVL tree */ alpar@9: alpar@9: #define avl_delete_tree _glp_avl_delete_tree alpar@9: void avl_delete_tree(AVL *tree); alpar@9: /* delete AVL tree */ alpar@9: alpar@9: #endif alpar@9: alpar@9: /* eof */