Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
BinaryTree.h
Go to the documentation of this file.
1 // @(#)root/tmva $Id$
2 // Author: Andreas Hoecker, Joerg Stelzer, Helge Voss, Kai Voss, Eckhard von Toerne
3 
4 /**********************************************************************************
5  * Project: TMVA - a Root-integrated toolkit for multivariate data analysis *
6  * Package: TMVA *
7  * Class : BinaryTree *
8  * Web : http://tmva.sourceforge.net *
9  * *
10  * Description: *
11  * BinaryTree: A base class for BinarySearch- or Decision-Trees *
12  * *
13  * Authors (alphabetical): *
14  * Andreas Hoecker <Andreas.Hocker@cern.ch> - CERN, Switzerland *
15  * Helge Voss <Helge.Voss@cern.ch> - MPI-K Heidelberg, Germany *
16  * Kai Voss <Kai.Voss@cern.ch> - U. of Victoria, Canada *
17  * Eckhard v. Toerne <evt@uni-bonn.de> - U of Bonn, Germany *
18  * *
19  * Copyright (c) 2005-2011: *
20  * CERN, Switzerland *
21  * U. of Victoria, Canada *
22  * MPI-K Heidelberg, Germany *
23  * U. of Bonn, Germany *
24  * *
25  * Redistribution and use in source and binary forms, with or without *
26  * modification, are permitted according to the terms listed in LICENSE *
27  * (http://tmva.sourceforge.net/LICENSE) *
28  **********************************************************************************/
29 
30 #ifndef ROOT_TMVA_BinaryTree
31 #define ROOT_TMVA_BinaryTree
32 
33 #include "TMVA/Version.h"
34 
35 //////////////////////////////////////////////////////////////////////////
36 // //
37 // BinaryTree //
38 // //
39 // Base class for BinarySearch and Decision Trees //
40 // //
41 //////////////////////////////////////////////////////////////////////////
42 
43 #include <iosfwd>
44 #include "TROOT.h"
45 
46 #include "TMVA/Node.h"
47 
48 // -----------------------------------------------------------------------------
49 
50 // the actual tree class
51 // Handles allocation, deallocation, and sorting of nodes.
52 // the Tree consists of a "root-node" wich might have 0 to 2 daughther nodes
53 
54 namespace TMVA {
55 
56  class BinaryTree;
57  class MsgLogger;
58 
59  std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
60  std::istream& operator>> ( std::istream& istr, BinaryTree& tree );
61 
62  class BinaryTree {
63 
64  friend std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
65  friend std::istream& operator>> ( std::istream& istr, BinaryTree& tree );
66 
67  public:
68 
69  // or a tree with Root node "n", any daughters of this node are automatically in the tree
70  BinaryTree( void );
71 
72  virtual ~BinaryTree();
73 
74  virtual Node* CreateNode(UInt_t size=0) const = 0;
75  virtual BinaryTree* CreateTree() const = 0;
76  // virtual BinaryTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE) = 0;
77  virtual const char* ClassName() const = 0;
78 
79  // set the root node of the tree
80  void SetRoot( Node* r ) { fRoot = r; }
81 
82  // Retrieves the address of the root node
83  virtual Node* GetRoot() const { return fRoot; }
84 
85  // get number of Nodes in the Tree as counted while booking the nodes;
86  UInt_t GetNNodes() const { return fNNodes; }
87 
88  // count the number of Nodes in the Tree by looping through the tree and updates
89  // the stored number. (e.g. useful when pruning, as the number count is updated when
90  // building the tree.
91  UInt_t CountNodes( Node* n = NULL );
92 
93  UInt_t GetTotalTreeDepth() const { return fDepth; }
94 
95  void SetTotalTreeDepth( Int_t depth ) { fDepth = depth; }
96  void SetTotalTreeDepth( Node* n = NULL );
97 
98  Node* GetLeftDaughter ( Node* n);
99  Node* GetRightDaughter( Node* n);
100 
101  virtual void Print( std::ostream& os ) const;
102  virtual void Read ( std::istream& istr, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
103  virtual void* AddXMLTo(void* parent) const;
104  virtual void ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
105 
106  private:
107 
108 
109  protected:
110  Node* fRoot; //the root node of the tree
111  // the tree only has it's root node, the "daughters" are taken car
112  // of by the "node" properties of the "root"
113 
114  // delete a node (and the corresponding event if owned by the tree)
115  void DeleteNode( Node* );
116 
117  UInt_t fNNodes; // total number of nodes in the tree (counted)
118  UInt_t fDepth; // maximal depth in tree reached
119 
120  MsgLogger& Log() const;
121 
122  ClassDef(BinaryTree,0); // Base class for BinarySearch and Decision Trees
123  };
124 
125 } // namespace TMVA
126 
127 #endif
128