Logo ROOT   6.07/09
Reference Guide
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 #ifndef ROOT_TMVA_Version
34 #include "TMVA/Version.h"
35 #endif
36 
37 //////////////////////////////////////////////////////////////////////////
38 // //
39 // BinaryTree //
40 // //
41 // Base class for BinarySearch and Decision Trees //
42 // //
43 //////////////////////////////////////////////////////////////////////////
44 
45 #include <iosfwd>
46 #ifndef ROOT_TROOT
47 #include "TROOT.h"
48 #endif
49 
50 #ifndef ROOT_TMVA_Node
51 #include "TMVA/Node.h"
52 #endif
53 
54 // -----------------------------------------------------------------------------
55 
56 // the actual tree class
57 // Handles allocation, deallocation, and sorting of nodes.
58 // the Tree consists of a "root-node" wich might have 0 to 2 daughther nodes
59 
60 namespace TMVA {
61 
62  class BinaryTree;
63  class MsgLogger;
64 
65  std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
66  std::istream& operator>> ( std::istream& istr, BinaryTree& tree );
67 
68  class BinaryTree {
69 
70  friend std::ostream& operator<< ( std::ostream& os, const BinaryTree& tree );
71  friend std::istream& operator>> ( std::istream& istr, BinaryTree& tree );
72 
73  public:
74 
75  // or a tree with Root node "n", any daughters of this node are automatically in the tree
76  BinaryTree( void );
77 
78  virtual ~BinaryTree();
79 
80  virtual Node* CreateNode(UInt_t size=0) const = 0;
81  virtual BinaryTree* CreateTree() const = 0;
82  // virtual BinaryTree* CreateFromXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE) = 0;
83  virtual const char* ClassName() const = 0;
84 
85  // set the root node of the tree
86  void SetRoot( Node* r ) { fRoot = r; }
87 
88  // Retrieves the address of the root node
89  virtual Node* GetRoot() const { return fRoot; }
90 
91  // get number of Nodes in the Tree as counted while booking the nodes;
92  UInt_t GetNNodes() const { return fNNodes; }
93 
94  // count the number of Nodes in the Tree by looping through the tree and updates
95  // the stored number. (e.g. useful when pruning, as the number count is updated when
96  // building the tree.
97  UInt_t CountNodes( Node* n = NULL );
98 
99  UInt_t GetTotalTreeDepth() const { return fDepth; }
100 
101  void SetTotalTreeDepth( Int_t depth ) { fDepth = depth; }
102  void SetTotalTreeDepth( Node* n = NULL );
103 
104  Node* GetLeftDaughter ( Node* n);
105  Node* GetRightDaughter( Node* n);
106 
107  virtual void Print( std::ostream& os ) const;
108  virtual void Read ( std::istream& istr, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
109  virtual void* AddXMLTo(void* parent) const;
110  virtual void ReadXML(void* node, UInt_t tmva_Version_Code = TMVA_VERSION_CODE );
111 
112  private:
113 
114 
115  protected:
116  Node* fRoot; //the root node of the tree
117  // the tree only has it's root node, the "daughters" are taken car
118  // of by the "node" properties of the "root"
119 
120  // delete a node (and the corresponding event if owned by the tree)
121  void DeleteNode( Node* );
122 
123  UInt_t fNNodes; // total number of nodes in the tree (counted)
124  UInt_t fDepth; // maximal depth in tree reached
125 
126  MsgLogger& Log() const;
127 
128  ClassDef(BinaryTree,0); // Base class for BinarySearch and Decision Trees
129  };
130 
131 } // namespace TMVA
132 
133 #endif
134 
#define TMVA_VERSION_CODE
Definition: Version.h:47
virtual void Read(std::istream &istr, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
Read the binary tree from an input stream.
Definition: BinaryTree.cxx:170
UInt_t GetTotalTreeDepth() const
Definition: BinaryTree.h:99
virtual ~BinaryTree()
destructor (deletes the nodes and "events" if owned by the tree
Definition: BinaryTree.cxx:66
int Int_t
Definition: RtypesCore.h:41
void DeleteNode(Node *)
protected, recursive, function used by the class destructor and when Pruning
Definition: BinaryTree.cxx:75
virtual Node * CreateNode(UInt_t size=0) const =0
#define ClassDef(name, id)
Definition: Rtypes.h:254
void SetTotalTreeDepth(Int_t depth)
Definition: BinaryTree.h:101
TRandom2 r(17)
virtual BinaryTree * CreateTree() const =0
unsigned int UInt_t
Definition: RtypesCore.h:42
friend std::istream & operator>>(std::istream &istr, BinaryTree &tree)
virtual Node * GetRoot() const
Definition: BinaryTree.h:89
virtual void ReadXML(void *node, UInt_t tmva_Version_Code=TMVA_VERSION_CODE)
read attributes from XML
Definition: BinaryTree.cxx:145
virtual void Print(std::ostream &os) const
recursively print the tree
Definition: BinaryTree.cxx:126
std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
print the tree recursinvely using the << operator
Definition: BinaryTree.cxx:159
std::istream & operator>>(std::istream &istr, BinaryTree &tree)
read the tree from an std::istream
Definition: BinaryTree.cxx:205
BinaryTree(void)
constructor for a yet "empty" tree. Needs to be filled afterwards
Definition: BinaryTree.cxx:56
UInt_t GetNNodes() const
Definition: BinaryTree.h:92
virtual void * AddXMLTo(void *parent) const
add attributes to XML
Definition: BinaryTree.cxx:135
friend std::ostream & operator<<(std::ostream &os, const BinaryTree &tree)
UInt_t CountNodes(Node *n=NULL)
return the number of nodes in the tree. (make a new count –> takes time)
Definition: BinaryTree.cxx:104
Abstract ClassifierFactory template that handles arbitrary types.
Node * GetRightDaughter(Node *n)
get right daughter node current node "n"
Definition: BinaryTree.cxx:96
#define NULL
Definition: Rtypes.h:82
virtual const char * ClassName() const =0
Node * GetLeftDaughter(Node *n)
get left daughter node current node "n"
Definition: BinaryTree.cxx:88
Definition: tree.py:1
void SetRoot(Node *r)
Definition: BinaryTree.h:86
MsgLogger & Log() const
Definition: BinaryTree.cxx:236
const Int_t n
Definition: legend1.C:16