Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testOpBinTree.C
Go to the documentation of this file.
1
2/* Aleph-w
3
4 / \ | | ___ _ __ | |__ __ __
5 / _ \ | |/ _ \ '_ \| '_ \ ____\ \ /\ / / Data structures & Algorithms
6 / ___ \| | __/ |_) | | | |_____\ V V / version 1.9c
7 /_/ \_\_|\___| .__/|_| |_| \_/\_/ https://github.com/lrleon/Aleph-w
8 |_|
9
10 This file is part of Aleph-w library
11
12 Copyright (c) 2002-2018 Leandro Rabindranath Leon
13
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <https://www.gnu.org/licenses/>.
26*/
27
28# include <iostream>
29# include <vector>
30# include <string>
31# include <cmath>
32# include <ctime>
33# include <tpl_binTree.H>
34# include <tpl_binNodeUtils.H>
35# include <opBinTree.H>
36
37# include <cstdlib>
38using namespace std;
39# include <cstdlib>
40using namespace Aleph;
41
42
43void print(BinNode<int>* node, int, int)
44{
45 cout << node->get_key() << " ";
46}
47
48
49static int * k;
50
51void assign(BinTree<int>::Node* node, int, int)
52{
53 *k++ = node->get_key();
54}
55
56
57double bin_coef(int n, int k)
58{
59 if (k < 0 or k > n)
60 return 0;
61
62 if (k == 0 or k == n)
63 return 1;
64
65 if (k > n / 2)
66 k = n - k;
67
68 double result = 1;
69 int min = k;
70 while (k > 0)
71 {
72 result *= n;
73 n--;
74 k--;
75 }
76
77 while (min > 1)
78 {
79 result /= min;
80 min--;
81 }
82
83 return result;
84}
85
86
87int main(int argc, char * argv[])
88{
89 int n = 10;
90 if (argc > 1)
91 {
92 try { n = std::stoi(argv[1]); }
93 catch (...) { n = 10; }
94 }
95
96 if (n <= 0)
97 {
98 cerr << "n must be greater than 0" << endl;
99 return 1;
100 }
101
102 double prob = 0.5;
103 if (argc > 2)
104 {
105 try { prob = std::stod(argv[2]); }
106 catch (...) { prob = 0.5; }
107 }
108
109 unsigned int t = static_cast<unsigned int>(time(nullptr));
110 if (argc > 3)
111 {
112 try { t = static_cast<unsigned int>(std::stoul(argv[3])); }
113 catch (...) { t = static_cast<unsigned int>(time(nullptr)); }
114 }
115
116 srand(t);
117
118 cout << argv[0] << " " << n << " " << prob << " " << t << " " << endl;
119
120 std::vector<int> keys(static_cast<size_t>(n));
121
122 BinTree<int> tree;
123 BinTree<int>::Node * node;
124 int value;
125
126 for (int i = 0; i < n; i++)
127 {
128 do
129 {
130 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
131 }
132 while (tree.search(value) not_eq nullptr);
133
134 node = new BinTree<int>::Node (value);
135 tree.insert(node);
136 }
137
138 /* Coloca las claves del arbol en el arreglo keys */
139 k = keys.data();
140 inOrderRec(tree.getRoot(), assign);
141 cout << endl;
142
143 /* construye arreglo de probabilidades segun dist binomial */
144 std::vector<double> p(static_cast<size_t>(n));
145 for (int i = 0; i < n; i++)
146 {
147 p[static_cast<size_t>(i)] = bin_coef(n, i) * pow(prob, i) * pow((1 - prob), n - i);
148 printf("%.5f ", p[static_cast<size_t>(i)]);
149 }
150
152 build_optimal_tree<BinNode<int>, int>(keys.data(), p.data(), static_cast<size_t>(n));
153
154 cout << endl;
156
157 destroyRec(tree.getRoot());
159
160 return 0;
161}
Node for binary search tree.
Key & get_key() noexcept
Node *& getRoot() noexcept
Return the root of tree.
Node * insert(Node *p) noexcept
Insert a node in the tree.
Node * search(const Key &key) const noexcept
Search a key.
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_min_function > > min(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4111
__gmp_expr< typename __gmp_resolve_expr< T, V >::value_type, __gmp_binary_expr< __gmp_expr< T, U >, __gmp_expr< V, W >, __gmp_pow_function > > pow(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Definition gmpfrxx.h:4061
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively in preorder a binary tree.
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Traverse recursively inorder a binary tree.
void destroyRec(Node *&root) noexcept
Free recursively all the memory occupied by the tree 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.
STL namespace.
Optimal Binary Search Tree construction using dynamic programming.
Binary search tree with nodes without virtual destructors,.
int keys[]
void print(BinNode< int > *node, int, int)
void assign(BinTree< int >::Node *node, int, int)
double bin_coef(int n, int k)
static int * k
Utility functions for binary tree operations.
Generic unbalanced binary search tree.