Back to index

courier  0.68.2
tsearch.h
Go to the documentation of this file.
00001 /* Binary tree data structure.
00002    Copyright (C) 2006 Free Software Foundation, Inc.
00003 
00004    This program is free software; you can redistribute it and/or modify it
00005    under the terms of the GNU Library General Public License as published
00006    by the Free Software Foundation; either version 2, or (at your option)
00007    any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public
00015    License along with this program; if not, write to the Free Software
00016    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00017    USA.  */
00018 
00019 #ifndef _TSEARCH_H
00020 #define _TSEARCH_H
00021 
00022 #if HAVE_TSEARCH
00023 
00024 /* Get tseach(), tfind(), tdelete(), twalk() declarations.  */
00025 #include <search.h>
00026 
00027 #else
00028 
00029 #ifdef __cplusplus
00030 extern "C" {
00031 #endif
00032 
00033 /* See <http://www.opengroup.org/susv3xbd/search.h.html>,
00034        <http://www.opengroup.org/susv3xsh/tsearch.html>
00035    for details.  */
00036 
00037 typedef enum
00038 { 
00039   preorder,
00040   postorder, 
00041   endorder,
00042   leaf
00043 }
00044 VISIT;
00045 
00046 /* Searches an element in the tree *VROOTP that compares equal to KEY.
00047    If one is found, it is returned.  Otherwise, a new element equal to KEY
00048    is inserted in the tree and is returned.  */
00049 extern void * tsearch (const void *key, void **vrootp,
00050                        int (*compar) (const void *, const void *));
00051 
00052 /* Searches an element in the tree *VROOTP that compares equal to KEY.
00053    If one is found, it is returned.  Otherwise, NULL is returned.  */
00054 extern void * tfind (const void *key, void *const *vrootp,
00055                      int (*compar) (const void *, const void *));
00056 
00057 /* Searches an element in the tree *VROOTP that compares equal to KEY.
00058    If one is found, it is removed from the tree, and its parent node is
00059    returned.  Otherwise, NULL is returned.  */
00060 extern void * tdelete (const void *key, void **vrootp,
00061                        int (*compar) (const void *, const void *));
00062 
00063 /* Perform a depth-first, left-to-right traversal of the tree VROOT.
00064    The ACTION function is called:
00065      - for non-leaf nodes: 3 times, before the left subtree traversal,
00066        after the left subtree traversal but before the right subtree traversal,
00067        and after the right subtree traversal,
00068      - for leaf nodes: once.
00069    The arguments passed to ACTION are:
00070      1. the node; it can be casted to a 'const void * const *', i.e. into a
00071         pointer to the key,
00072      2. an indicator which visit of the node this is,
00073      3. the level of the node in the tree (0 for the root).  */
00074 extern void twalk (const void *vroot,
00075                    void (*action) (const void *, VISIT, int));
00076 
00077 #ifdef __cplusplus
00078 }
00079 #endif
00080 
00081 #endif
00082 
00083 #endif /* _TSEARCH_H */