home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 25 / nopv25.iso / 040A / PBL30DEV.ZIP / BINTREE.H next >
Encoding:
C/C++ Source or Header  |  1997-03-28  |  2.6 KB  |  87 lines

  1. /*
  2.  * This file is part of PB-Lib v3.0 C++ Programming Library
  3.  *
  4.  * Copyright (c) 1995, 1997 by Branislav L. Slantchev
  5.  * A fine product of Silicon Creations, Inc. (gargoyle)
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the License which accompanies this
  9.  * software. This library is distributed in the hope that it will
  10.  * be useful, but without any warranty; without even the implied
  11.  * warranty of merchantability or fitness for a particular purpose.
  12.  *
  13.  * You should have received a copy of the License along with this
  14.  * library, in the file LICENSE.DOC; if not, write to the address
  15.  * below to receive a copy via electronic mail.
  16.  *
  17.  * You can reach Branislav L. Slantchev (Silicon Creations, Inc.)
  18.  * at bslantch@cs.angelo.edu. The file SUPPORT.DOC has the current
  19.  * telephone numbers and the postal address for contacts.
  20. */
  21.  
  22. #ifndef INCLUDED_BINTREE_H
  23. #define INCLUDED_BINTREE_H
  24.  
  25. #include "typedef.h"
  26.  
  27. /////////////////////////////////////////////////////////////////////////////
  28. // structure for the node of the binary tree
  29. /////////////////////////////////////////////////////////////////////////////
  30. class zBTNode
  31. {
  32.     friend class zBinTree;
  33.  
  34. public:
  35.     zBTNode(void *aData = 0): data(aData), left(0), right(0){}
  36.  
  37. private:
  38.     void    *data;
  39.     zBTNode *left;
  40.     zBTNode *right;
  41. };
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // the binary tree class
  45. /////////////////////////////////////////////////////////////////////////////
  46.  
  47. typedef void (*fpCleanupFunc)(void*);
  48.  
  49. class zBinTree
  50. {
  51. public:
  52.     enum tree_order { pre, post, in, rev };
  53.  
  54.     zBinTree(fpCompFunc compare, Boolean shouldPurge = False);
  55.     virtual ~zBinTree();
  56.  
  57.     Boolean insert(void *p);
  58.     void    remove(void *p);
  59.     void    remove();
  60.     int     size() const { return count; }
  61.     void*   get(){ return current ? current->data : 0; }
  62.     void    update(void *p);
  63.     void*   find(void *arg);
  64.     void    forEach(fpAppFunc app, void *arg = 0, tree_order order = in);
  65.     void*   operator()(){ return get(); }
  66.     void    setCleanup(fpCleanupFunc cf){ cleanup = cf; }
  67.     void    purge();
  68.  
  69. protected:
  70.     Boolean        purging;
  71.     int            count;
  72.     fpCompFunc     compare;
  73.     fpCleanupFunc  cleanup;
  74.     fpAppFunc      action;
  75.     void          *action_arg;
  76.     zBTNode       *current;
  77.     zBTNode       *parent;
  78.     zBTNode       *root;
  79.  
  80.     void destroy(zBTNode *root);
  81.     void in_order(zBTNode *root);
  82.     void pre_order(zBTNode *root);
  83.     void post_order(zBTNode *root);
  84.     void rev_order(zBTNode *root);
  85. };
  86.  
  87. #endif /* INCLUDED_BINTREE_H */