BinaryTree.h

Go to the documentation of this file.
00001 // @(#)root/tmva $Id: BinaryTree.h 37986 2011-02-04 21:42:15Z pcanal $    
00002 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss 
00003 
00004 /**********************************************************************************
00005  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
00006  * Package: TMVA                                                                  *
00007  * Class  : BinaryTree                                                            *
00008  * Web    : http://tmva.sourceforge.net                                           *
00009  *                                                                                *
00010  * Description:                                                                   *
00011  *      BinaryTree: A base class for BinarySearch- or Decision-Trees              *
00012  *                                                                                *
00013  * Authors (alphabetical):                                                        *
00014  *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
00015  *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
00016  *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
00017  *                                                                                *
00018  * Copyright (c) 2005:                                                            *
00019  *      CERN, Switzerland                                                         * 
00020  *      U. of Victoria, Canada                                                    * 
00021  *      MPI-K Heidelberg, Germany                                                 * 
00022  *                                                                                *
00023  * Redistribution and use in source and binary forms, with or without             *
00024  * modification, are permitted according to the terms listed in LICENSE           *
00025  * (http://tmva.sourceforge.net/LICENSE)                                          *
00026  **********************************************************************************/
00027 
00028 #ifndef ROOT_TMVA_BinaryTree
00029 #define ROOT_TMVA_BinaryTree
00030 
00031 #ifndef ROOT_TMVA_Version
00032 #include "TMVA/Version.h"
00033 #endif
00034 
00035 //////////////////////////////////////////////////////////////////////////
00036 //                                                                      //
00037 // BinaryTree                                                           //
00038 //                                                                      //
00039 // Base class for BinarySearch and Decision Trees                       //
00040 //                                                                      //
00041 //////////////////////////////////////////////////////////////////////////
00042 
00043 #include <iosfwd>
00044 #ifndef ROOT_TROOT
00045 #include "TROOT.h"
00046 #endif
00047 
00048 #ifndef ROOT_TMVA_Node
00049 #include "TMVA/Node.h"
00050 #endif
00051 
00052 // -----------------------------------------------------------------------------
00053 
00054 // the actual tree class
00055 // Handles allocation, deallocation, and sorting of nodes.
00056 // the Tree consists of a "root-node" wich might have  0 to 2 daughther nodes
00057 
00058 namespace TMVA {
00059    
00060    class BinaryTree;
00061    class MsgLogger;
00062 
00063    ostream& operator<< ( ostream& os, const BinaryTree& tree );
00064    istream& operator>> ( istream& istr,     BinaryTree& tree );
00065    
00066    class BinaryTree {
00067       
00068       friend ostream& operator<< ( ostream& os, const BinaryTree& tree );
00069       friend istream& operator>> ( istream& istr,     BinaryTree& tree );
00070       
00071    public:
00072       
00073       // or a tree with Root node "n", any daughters of this node are automatically in the tree
00074       BinaryTree( void );
00075 
00076       virtual ~BinaryTree();
00077 
00078       virtual Node* CreateNode(UInt_t size=0) const = 0;
00079       virtual BinaryTree* CreateTree() const = 0;
00080       //      virtual BinaryTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE) = 0;
00081       virtual const char* ClassName() const = 0;
00082 
00083       // set the root node of the tree
00084       void SetRoot( Node* r ) { fRoot = r; }
00085     
00086       // Retrieves the address of the root node
00087       virtual Node* GetRoot() const { return fRoot; }
00088     
00089       // get number of Nodes in the Tree as counted while booking the nodes;
00090       UInt_t GetNNodes() const { return fNNodes; }
00091 
00092       // count the number of Nodes in the Tree by looping through the tree and updates
00093       // the stored number. (e.g. useful when pruning, as the number count is updated when
00094       // building the tree.
00095       UInt_t CountNodes( Node* n = NULL );
00096 
00097       UInt_t GetTotalTreeDepth() const { return fDepth; }
00098 
00099       void SetTotalTreeDepth( Int_t depth ) { fDepth = depth; }
00100       void SetTotalTreeDepth( Node* n = NULL );
00101 
00102       Node* GetLeftDaughter ( Node* n);    
00103       Node* GetRightDaughter( Node* n);
00104 
00105       virtual void Print( ostream& os ) const;
00106       virtual void Read ( istream& istr, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
00107       virtual void* AddXMLTo(void* parent) const;
00108       virtual void  ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
00109 
00110    private:
00111   
00112   
00113    protected:
00114       Node*      fRoot;                //the root node of the tree
00115       // the tree only has it's root node, the "daughters" are taken car 
00116       // of by the "node" properties of the "root"
00117 
00118       // delete a node (and the corresponding event if owned by the tree)
00119       void       DeleteNode( Node* );
00120 
00121       UInt_t     fNNodes;           // total number of nodes in the tree (counted)
00122       UInt_t     fDepth;            // maximal depth in tree reached
00123 
00124       static MsgLogger* fgLogger;   // message logger, static to save resources    
00125       MsgLogger& Log() const { return *fgLogger; }
00126 
00127       ClassDef(BinaryTree,0) // Base class for BinarySearch and Decision Trees
00128    };  
00129 
00130 } // namespace TMVA
00131 
00132 #endif
00133 

Generated on Tue Jul 5 14:27:18 2011 for ROOT_528-00b_version by  doxygen 1.5.1