Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tree_node_graphviz_example.cc
Go to the documentation of this file.
1#include <iostream>
2#include <fstream>
3#include <string>
4#include "tpl_tree_node.H"
5#include "generate_tree.H"
6
7using namespace Aleph;
8using namespace std;
9
10// Estructura de ejemplo para almacenar en los nodos
12 int pid;
13 string name;
14 string user;
15
16 ProcessInfo() : pid(0), name(""), user("") {}
17 ProcessInfo(int p, const string& n, const string& u) : pid(p), name(n), user(u) {}
18};
19
20// Functor personalizado para decirle a generate_tree_graphviz cómo formatear la etiqueta del nodo
22 string operator()(Tree_Node<ProcessInfo>* node) const {
23 const ProcessInfo& info = node->get_key();
24 return info.name + "\\nPID: " + to_str(info.pid) + "\\nUser: " + info.user;
25 }
26};
27
28int main() {
29 cout << "Generando un Tree_Node complejo (arbol de procesos)..." << endl;
30
31 // 1. Crear la raiz (systemd)
32 auto root = new Tree_Node<ProcessInfo>(ProcessInfo(1, "systemd", "root"));
33
34 // 2. Crear hijos principales
35 auto bash_login = new Tree_Node<ProcessInfo>(ProcessInfo(1024, "bash", "lrleon"));
36 auto cron_daemon = new Tree_Node<ProcessInfo>(ProcessInfo(401, "cron", "root"));
37 auto xorg_server = new Tree_Node<ProcessInfo>(ProcessInfo(850, "Xorg", "root"));
38 auto dbus_daemon = new Tree_Node<ProcessInfo>(ProcessInfo(900, "dbus-daemon", "messagebus"));
39
40 // Insertar hijos de root
41 root->insert_rightmost_child(bash_login);
42 root->insert_rightmost_child(cron_daemon);
43 root->insert_rightmost_child(xorg_server);
44 root->insert_rightmost_child(dbus_daemon);
45
46 // 3. Crear nietos (procesos bajo bash)
47 auto htop = new Tree_Node<ProcessInfo>(ProcessInfo(2048, "htop", "lrleon"));
48 auto nvim = new Tree_Node<ProcessInfo>(ProcessInfo(2050, "nvim", "lrleon"));
49 auto gcc_comp = new Tree_Node<ProcessInfo>(ProcessInfo(2055, "g++", "lrleon"));
50
51 bash_login->insert_rightmost_child(htop);
52 bash_login->insert_rightmost_child(nvim);
53 bash_login->insert_rightmost_child(gcc_comp);
54
55 // 4. Crear bisnietos (procesos lanzados por el compilador gcc)
56 auto cc1plus = new Tree_Node<ProcessInfo>(ProcessInfo(2056, "cc1plus", "lrleon"));
57 auto as = new Tree_Node<ProcessInfo>(ProcessInfo(2057, "as", "lrleon"));
58
59 gcc_comp->insert_rightmost_child(cc1plus);
60 gcc_comp->insert_rightmost_child(as);
61
62 // 5. Mas hijos bajo otros nodos (tareas de cron)
63 auto daily_backup = new Tree_Node<ProcessInfo>(ProcessInfo(3001, "tar", "root"));
64 auto log_rotate = new Tree_Node<ProcessInfo>(ProcessInfo(3002, "logrotate", "root"));
65
66 cron_daemon->insert_rightmost_child(daily_backup);
67 cron_daemon->insert_rightmost_child(log_rotate);
68
69 // 6. Procesos graficos
70 auto wm = new Tree_Node<ProcessInfo>(ProcessInfo(910, "gnome-shell", "lrleon"));
71 auto terminal = new Tree_Node<ProcessInfo>(ProcessInfo(950, "gnome-terminal", "lrleon"));
72 auto browser = new Tree_Node<ProcessInfo>(ProcessInfo(980, "firefox", "lrleon"));
73
74 xorg_server->insert_rightmost_child(wm);
75 wm->insert_rightmost_child(terminal);
76 wm->insert_rightmost_child(browser);
77
78 // 7. Generar el archivo DOT usando nuestra funcion y el functor personalizado
79 string filename = "process_tree.dot";
80 ofstream out(filename);
81 if (!out) {
82 cerr << "Error al abrir el archivo " << filename << " para escritura." << endl;
83 return 1;
84 }
85
86 // Llamada con el Functor de personalizacion
88 out.close();
89
90 cout << "Archivo '" << filename << "' generado exitosamente." << endl;
91 cout << "Puedes visualizarlo ejecutando:" << endl;
92 cout << " dot -Tpng " << filename << " -o process_tree.png" << endl;
93
94 // Limpiar memoria
96
97 return 0;
98}
Generic m-ary trees.
T & get_key() noexcept
Returns a modifiable reference to the node contents.
Tree visualization and output generation.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
Definition gmpfrxx.h:4060
void destroy_tree(Node *root)
Destroys (frees memory) the tree whose root is root.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::string to_str(const double d)
Convert double to a std::string with maximum round-trip precision.
STL namespace.
ProcessInfo(int p, const string &n, const string &u)
string operator()(Tree_Node< ProcessInfo > *node) const
General tree (n-ary tree) node.