// @(#)root/tmva $Id$    
// Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne 

/**********************************************************************************
 * Project: TMVA - a Root-integrated toolkit for multivariate data analysis       *
 * Package: TMVA                                                                  *
 * Class  : BinaryTree                                                            *
 * Web    : http://tmva.sourceforge.net                                           *
 *                                                                                *
 * Description:                                                                   *
 *      BinaryTree: A base class for BinarySearch- or Decision-Trees              *
 *                                                                                *
 * Authors (alphabetical):                                                        *
 *      Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland              *
 *      Helge Voss      <Helge.Voss@cern.ch>     - MPI-K Heidelberg, Germany      *
 *      Kai Voss        <Kai.Voss@cern.ch>       - U. of Victoria, Canada         *
 *      Eckhard v. Toerne  <evt@uni-bonn.de>     - U of Bonn, Germany             *
 *                                                                                *
 * Copyright (c) 2005-2011:                                                       *
 *      CERN, Switzerland                                                         *
 *      U. of Victoria, Canada                                                    *
 *      MPI-K Heidelberg, Germany                                                 *
 *      U. of Bonn, Germany                                                       *
 *                                                                                *
 * Redistribution and use in source and binary forms, with or without             *
 * modification, are permitted according to the terms listed in LICENSE           *
 * (http://tmva.sourceforge.net/LICENSE)                                          *
 **********************************************************************************/

#ifndef ROOT_TMVA_BinaryTree
#define ROOT_TMVA_BinaryTree

#ifndef ROOT_TMVA_Version
#include "TMVA/Version.h"
#endif

//////////////////////////////////////////////////////////////////////////
//                                                                      //
// BinaryTree                                                           //
//                                                                      //
// Base class for BinarySearch and Decision Trees                       //
//                                                                      //
//////////////////////////////////////////////////////////////////////////

#include <iosfwd>
#ifndef ROOT_TROOT
#include "TROOT.h"
#endif

#ifndef ROOT_TMVA_Node
#include "TMVA/Node.h"
#endif

// -----------------------------------------------------------------------------

// the actual tree class
// Handles allocation, deallocation, and sorting of nodes.
// the Tree consists of a "root-node" wich might have  0 to 2 daughther nodes

namespace TMVA {
   
   class BinaryTree;
   class MsgLogger;

   std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
   std::istream& operator>> ( std::istream& istr,     BinaryTree& tree );
   
   class BinaryTree {
      
      friend std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
      friend std::istream& operator>> ( std::istream& istr,     BinaryTree& tree );
      
   public:
      
      // or a tree with Root node "n", any daughters of this node are automatically in the tree
      BinaryTree( void );

      virtual ~BinaryTree();

      virtual Node* CreateNode(UInt_t size=0) const = 0;
      virtual BinaryTree* CreateTree() const = 0;
      //      virtual BinaryTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE) = 0;
      virtual const char* ClassName() const = 0;

      // set the root node of the tree
      void SetRoot( Node* r ) { fRoot = r; }
    
      // Retrieves the address of the root node
      virtual Node* GetRoot() const { return fRoot; }
    
      // get number of Nodes in the Tree as counted while booking the nodes;
      UInt_t GetNNodes() const { return fNNodes; }

      // count the number of Nodes in the Tree by looping through the tree and updates
      // the stored number. (e.g. useful when pruning, as the number count is updated when
      // building the tree.
      UInt_t CountNodes( Node* n = NULL );

      UInt_t GetTotalTreeDepth() const { return fDepth; }

      void SetTotalTreeDepth( Int_t depth ) { fDepth = depth; }
      void SetTotalTreeDepth( Node* n = NULL );

      Node* GetLeftDaughter ( Node* n);    
      Node* GetRightDaughter( Node* n);

      virtual void Print( std::ostream& os ) const;
      virtual void Read ( std::istream& istr, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
      virtual void* AddXMLTo(void* parent) const;
      virtual void  ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );

   private:
  
  
   protected:
      Node*      fRoot;                //the root node of the tree
      // the tree only has it's root node, the "daughters" are taken car 
      // of by the "node" properties of the "root"

      // delete a node (and the corresponding event if owned by the tree)
      void       DeleteNode( Node* );

      UInt_t     fNNodes;           // total number of nodes in the tree (counted)
      UInt_t     fDepth;            // maximal depth in tree reached

      MsgLogger& Log() const;

      ClassDef(BinaryTree,0) // Base class for BinarySearch and Decision Trees
   };  

} // namespace TMVA

#endif

 BinaryTree.h:1
 BinaryTree.h:2
 BinaryTree.h:3
 BinaryTree.h:4
 BinaryTree.h:5
 BinaryTree.h:6
 BinaryTree.h:7
 BinaryTree.h:8
 BinaryTree.h:9
 BinaryTree.h:10
 BinaryTree.h:11
 BinaryTree.h:12
 BinaryTree.h:13
 BinaryTree.h:14
 BinaryTree.h:15
 BinaryTree.h:16
 BinaryTree.h:17
 BinaryTree.h:18
 BinaryTree.h:19
 BinaryTree.h:20
 BinaryTree.h:21
 BinaryTree.h:22
 BinaryTree.h:23
 BinaryTree.h:24
 BinaryTree.h:25
 BinaryTree.h:26
 BinaryTree.h:27
 BinaryTree.h:28
 BinaryTree.h:29
 BinaryTree.h:30
 BinaryTree.h:31
 BinaryTree.h:32
 BinaryTree.h:33
 BinaryTree.h:34
 BinaryTree.h:35
 BinaryTree.h:36
 BinaryTree.h:37
 BinaryTree.h:38
 BinaryTree.h:39
 BinaryTree.h:40
 BinaryTree.h:41
 BinaryTree.h:42
 BinaryTree.h:43
 BinaryTree.h:44
 BinaryTree.h:45
 BinaryTree.h:46
 BinaryTree.h:47
 BinaryTree.h:48
 BinaryTree.h:49
 BinaryTree.h:50
 BinaryTree.h:51
 BinaryTree.h:52
 BinaryTree.h:53
 BinaryTree.h:54
 BinaryTree.h:55
 BinaryTree.h:56
 BinaryTree.h:57
 BinaryTree.h:58
 BinaryTree.h:59
 BinaryTree.h:60
 BinaryTree.h:61
 BinaryTree.h:62
 BinaryTree.h:63
 BinaryTree.h:64
 BinaryTree.h:65
 BinaryTree.h:66
 BinaryTree.h:67
 BinaryTree.h:68
 BinaryTree.h:69
 BinaryTree.h:70
 BinaryTree.h:71
 BinaryTree.h:72
 BinaryTree.h:73
 BinaryTree.h:74
 BinaryTree.h:75
 BinaryTree.h:76
 BinaryTree.h:77
 BinaryTree.h:78
 BinaryTree.h:79
 BinaryTree.h:80
 BinaryTree.h:81
 BinaryTree.h:82
 BinaryTree.h:83
 BinaryTree.h:84
 BinaryTree.h:85
 BinaryTree.h:86
 BinaryTree.h:87
 BinaryTree.h:88
 BinaryTree.h:89
 BinaryTree.h:90
 BinaryTree.h:91
 BinaryTree.h:92
 BinaryTree.h:93
 BinaryTree.h:94
 BinaryTree.h:95
 BinaryTree.h:96
 BinaryTree.h:97
 BinaryTree.h:98
 BinaryTree.h:99
 BinaryTree.h:100
 BinaryTree.h:101
 BinaryTree.h:102
 BinaryTree.h:103
 BinaryTree.h:104
 BinaryTree.h:105
 BinaryTree.h:106
 BinaryTree.h:107
 BinaryTree.h:108
 BinaryTree.h:109
 BinaryTree.h:110
 BinaryTree.h:111
 BinaryTree.h:112
 BinaryTree.h:113
 BinaryTree.h:114
 BinaryTree.h:115
 BinaryTree.h:116
 BinaryTree.h:117
 BinaryTree.h:118
 BinaryTree.h:119
 BinaryTree.h:120
 BinaryTree.h:121
 BinaryTree.h:122
 BinaryTree.h:123
 BinaryTree.h:124
 BinaryTree.h:125
 BinaryTree.h:126
 BinaryTree.h:127
 BinaryTree.h:128
 BinaryTree.h:129
 BinaryTree.h:130
 BinaryTree.h:131
 BinaryTree.h:132
 BinaryTree.h:133
 BinaryTree.h:134