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