generate_tree.H

00001 
00002 /*
00003   This file is part of Aleph system
00004 
00005   Copyright (c) 2002, 2003, 2004, 2005, 2006
00006   UNIVERSITY LOS ANDES (ULA) Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA  
00007 
00008   - Center of Studies in Microelectronics & Distributed Systems (CEMISID) 
00009   - ULA Computer Science Department
00010   - FUNDACITE Mérida - Ministerio de Ciencia y Tecnología
00011 
00012   PERMISSION TO USE, COPY, MODIFY AND DISTRIBUTE THIS SOFTWARE AND ITS
00013   DOCUMENTATION IS HEREBY GRANTED, PROVIDED THAT BOTH THE COPYRIGHT
00014   NOTICE AND THIS PERMISSION NOTICE APPEAR IN ALL COPIES OF THE
00015   SOFTWARE, DERIVATIVE WORKS OR MODIFIED VERSIONS, AND ANY PORTIONS
00016   THEREOF, AND THAT BOTH NOTICES APPEAR IN SUPPORTING DOCUMENTATION.
00017 
00018   Aleph is distributed in the hope that it will be useful, but WITHOUT
00019   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00020   or FITNESS FOR A PARTICULAR PURPOSE.
00021 
00022   UNIVERSIDAD DE LOS ANDES requests users of this software to return to 
00023 
00024   Leandro Leon
00025   CEMISID 
00026   Ed La Hechicera 
00027   3er piso, ala sur
00028   Facultad de Ingenieria 
00029   Universidad de Los Andes 
00030   Merida - REPÚBLICA BOLIVARIANA DE VENEZUELA    or
00031 
00032   lrleon@ula.ve
00033 
00034   any improvements or extensions that they make and grant Universidad 
00035   de Los Andes (ULA) the rights to redistribute these changes. 
00036 
00037   Aleph is (or was) granted by: 
00038   - Consejo de Desarrollo Cientifico, Humanistico, Tecnico de la ULA 
00039     (CDCHT)
00040   - Fundacite Mérida
00041 */
00042 
00043 # ifndef GENERATE_TREE_H
00044 # define GENERATE_TREE_H
00045 
00046 # include <fstream>
00047 # include <tpl_tree_node.H>
00048 
00049 
00050 namespace Aleph
00051 {
00052 
00053 
00054 /*
00055   Write debe exportar const string Write::operator () (Node *)
00056 
00057   Que se encarga de convertir el valor de clave contenido en el nodo a
00058   un string imprimible 
00059 */
00060 
00061     template <typename Node, class Write> static
00062 void __generate_tree(Node *          node, 
00063                int             deway [], 
00064                const size_t &  current_level, 
00065                const size_t &  size,
00066                std::ofstream & output)
00067 { 
00068   if (current_level >= size)
00069     throw std::overflow_error("Allocated size for deway array has been "
00070                      "exceeded");
00071 
00072       // imprimir número de Deway del padre
00073   output << "Node ";
00074   for (int i = 0; i < current_level; ++i)
00075     {
00076       output << deway[i];
00077 
00078       if (i < current_level - 1)
00079      output << ".";
00080     }
00081   output << " \"" << Write () (node) << "\" " << std::endl;
00082 
00083   Node * child = node->get_left_child(); 
00084   for (int i = 0; child != NULL; i++, child = child->get_right_sibling())
00085     {
00086       deway[current_level + 1] = i;
00087       __generate_tree<Node, Write>
00088      (child, deway, current_level + 1, size, output);
00089     }
00090 }
00091 
00092 
00093 const size_t Max_Tree_Node_Depth = 1024;
00094 
00115     template <typename Node, class Write>
00116 void generate_tree(Node *           root, 
00117              std::ofstream &  out, 
00118              const int &      tree_number = 0)
00119 {                               
00120   out << "Root \"" << Write () (root) << "\" " << std::endl;
00121 
00122   int * deway = new int [Max_Tree_Node_Depth];
00123 
00124   const int level = 0; // Este es el nivel de partida
00125 
00126   deway[level] = tree_number; // nivel 0 en el número del árbol
00127 
00128   Node * child = root->get_left_child(); 
00129   for (int i = 0; child != NULL; i++, child = child->get_right_sibling())
00130     {
00131       deway[1] = i;
00132       __generate_tree<Node, Write>(child, deway, level + 1, 
00133                        Max_Tree_Node_Depth, out);
00134     }
00135 
00136   delete deway;
00137 }
00138 
00139 
00160     template <typename Node, class Write>
00161 void generate_forest(Node * root, std::ofstream & out = std::cout)
00162 {                               
00163   Node * tree = root; 
00164 
00165   for (int i = 0; tree != NULL; i++, tree = tree->get_right_sibling())
00166     generate_tree<Node, Write>(tree, out, i);
00167 }
00168 
00169 } // end namespace Aleph
00170 
00171 # endif // GENERATE_TREE_H
00172 

Leandro R. León