Main Page   Modules   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages   Examples  

CTBbtree.hxx

Go to the documentation of this file.
00001 #ifndef _CTBbtree_HXX
00002 #define _CTBbtree_HXX 1
00003 
00008 /*----------------------------------------------------------------------------*/
00009 /* C Tool Box: Designed and implemented by:                                   */
00010 /*    Walter F.J. Mueller   Gesellschaft fuer Schwerionenforschung (GSI)      */
00011 /*                          Planckstrasse 1, D-64291 Darmstadt, Germany       */
00012 /*                  Email:  W.F.J.Mueller@gsi.de                              */
00013 /*                  WWW:    http://www-kp3.gsi.de/www/kp3/people/mueller.html */
00014 /*------------------------------------------+---------------------------------*/
00015 
00016 #include "CTBclone.hxx"
00017 
00018 class CTBbtreeNode;                         // forward dcl of node class
00019 
00020 class CTBbtree {
00021 
00022   friend class CTBbtreeNode;
00023 
00024   public:
00025 
00026                     CTBbtree();
00027                     CTBbtree(const CTBbtree& rhs);
00028 
00029     virtual         ~CTBbtree();
00030 
00031     CTBbtreeNode*   RNode() const;
00032     CTBbtreeNode*   First() const;
00033     CTBbtreeNode*   Last() const;
00034     CTBsize         Size() const;
00035 
00036     void            Clear();
00037 
00038     void            TreeHeight(int& i_minh,int& i_maxh,float& f_avrp) const;
00039     bool            TreeCheck(ostream* p_os = 0) const;
00040   
00041     void            Dump(int i_indent=0, ostream& os=cout,
00042                          const char* p_text=0) const;
00043   
00044     CTBbtreeNode*   operator[](CTBint i_ind) const;
00045 
00046     CTBbtree&       operator=(const CTBbtree& rhs);
00047 
00048   private:
00049 
00050     CTBbtreeNode*   mp_rnode;               
00051     CTBbtreeNode*   mp_first;               
00052     CTBbtreeNode*   mp_last;                
00053     CTBint          mi_size;                
00054 };
00055 
00056 class CTBbtreeNode {
00057 
00058   friend class CTBbtree;
00059 
00060   public:
00061 
00062                     CTBbtreeNode();
00063     virtual         ~CTBbtreeNode();
00064 
00065     virtual CTBbtreeNode* Clone() const;
00066 
00067     CTBbtree*       Head() const;
00068     CTBbtreeNode*   Left() const;
00069     CTBbtreeNode*   Right() const;
00070     CTBbtreeNode*   Up() const;
00071     CTBbtreeNode*   Next() const;
00072     CTBbtreeNode*   Prev() const;
00073 
00074     CTBbtreeNode*   Skip(CTBint i_offset) const;
00075 
00076     CTBint          Rank() const;
00077 
00078     CTBint          LWeight() const;
00079 
00080     bool            IsRNode() const;
00081     bool            IsLeftChild() const;
00082     bool            IsRightChild() const;
00083 
00084     bool            IsBalanced() const;
00085     bool            IsLeftHeavy() const;
00086     bool            IsRightHeavy() const;
00087 
00088     void            Insert(CTBbtree& head,CTBbtreeNode* p_parent,int i_ctype);
00089 
00090     void            Remove();
00091 
00092   private:
00093 
00094     void            LWeight(                // set left tree weight
00095                       CTBint i_weight);
00096     void            IncLWeight();           // increment left tree weight
00097     void            DecLWeight();           // decrement left tree weight
00098 
00099     void            SetRNode();             // set child status to rnode
00100     void            SetLeftChild();         // set child status to left
00101     void            SetRightChild();        // set child status to right
00102 
00103     void            SetBalanced();          // set bfactor to balanced
00104     void            SetLeftHeavy();         // set bfactor to left heavy
00105     void            SetRightHeavy();        // set bfactor to right heavy
00106 
00107     void            RotateLeft();           // single rotation, left
00108     void            RotateRight();          // single rotation, right
00109     void            RotateLeftRight();      // double rotation, left-right
00110     void            RotateRightLeft();      // double rotation, right-left
00111   
00112     void            InsertFixup();          // all fixup's after insertion
00113     void            RemoveFixup();          // all fixup's before removal
00114 
00115     void            SubTreeHeight(          // determine subtree properties
00116                       int& i_minh,          //   minimal height to leave node
00117                       int& i_maxh,          //   maximal height to leave node
00118                       double& d_nnode,      //   number of nodes
00119                       double& d_ncomp) const; //   number of compares
00120     bool            SubTreeCheck(           // check whether subtree is balanced
00121                       ostream* p_os,
00122                       int& i_maxh) const;
00123 
00124                     CTBbtreeNode(           // copy constructor DUMMY
00125                       const CTBbtreeNode& rhs);
00126     CTBbtreeNode&   operator=(              // assignment operator DUMMY
00127                       const CTBbtreeNode& rhs);
00128 
00129   private:
00130 
00131     static const CTBuint32 status_lchild    = 0x2;
00132     static const CTBuint32 status_rchild    = 0x1;
00133     static const CTBuint32 status_childmask = status_lchild | status_rchild;
00134     static const CTBuint32 status_lheavy    = 0x8;
00135     static const CTBuint32 status_rheavy    = 0x4;
00136     static const CTBuint32 status_heavymask = status_lheavy | status_rheavy;
00137     static const CTBuint32 status_bitmask   = status_childmask|status_heavymask;
00138     static const CTBuint32 status_deltaw    = 16;
00139   
00140     CTBbtree*       mp_head;                
00141     CTBbtreeNode*   mp_up;                  
00142     CTBbtreeNode*   mp_left;                
00143     CTBbtreeNode*   mp_right;               
00144     CTBuint32       mi_status;              
00145 };
00146 
00147 class CTBclone<CTBbtreeNode> : public CTBcloneable<CTBbtreeNode> {};
00148 
00149 // Notes:
00150 // 1. The mi_combo member variable holds 3 different pieces of status info:
00151 //      child mode bits:        bit 0: right child;   bit 1: left child
00152 //      balance factor bits     bit 2: right heavy;   bit 3: left heavy
00153 //      left tree weight (rank) is stored in the bits 4,...,31
00154 //
00155 // 2. The maximum number of nodes is limited to about 2^28 because the left
00156 //    tree weight is stored in only 28 bits. 
00157 //
00158 // 3. The Next() and Prev() member functions clearly allow to break const-ness
00159 //    of a pointer. This is behaviour is uncritical because the functions are
00160 //    only indirectly called through Iterators and Browsers which take care
00161 //    of proper const-ness.
00162 
00163 #if !(defined(CTB__OutLine) || defined(CTBbtree__OutLine))
00164 #include "CTBbtree.icc"
00165 #endif
00166 
00167 #endif

Generated at Fri Oct 24 18:11:27 2003 for CTBbase by doxygen1.2.9-20010812 written by Dimitri van Heesch, © 1997-2001