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 Permission is hereby granted, free of charge, to any person obtaining a copy
15 of this software and associated documentation files (the "Software"), to deal
16 in the Software without restriction, including without limitation the rights
17 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18 copies of the Software, and to permit persons to whom the Software is
19 furnished to do so, subject to the following conditions:
20
21 The above copyright notice and this permission notice shall be included in all
22 copies or substantial portions of the Software.
23
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 SOFTWARE.
31*/
32
33# include <iostream>
34# include <vector>
35# include <string>
36# include <cmath>
37# include <ctime>
38# include <tpl_binTree.H>
39# include <tpl_binNodeUtils.H>
40# include <opBinTree.H>
41
42# include <cstdlib>
43using namespace std;
44# include <cstdlib>
45using namespace Aleph;
46
47
48void print(BinNode<int>* node, int, int)
49{
50 cout << node->get_key() << " ";
51}
52
53
54static int * k;
55
56void assign(BinTree<int>::Node* node, int, int)
57{
58 *k++ = node->get_key();
59}
60
61
62double bin_coef(int n, int k)
63{
64 if (k < 0 or k > n)
65 return 0;
66
67 if (k == 0 or k == n)
68 return 1;
69
70 if (k > n / 2)
71 k = n - k;
72
73 double result = 1;
74 int min = k;
75 while (k > 0)
76 {
77 result *= n;
78 n--;
79 k--;
80 }
81
82 while (min > 1)
83 {
84 result /= min;
85 min--;
86 }
87
88 return result;
89}
90
91
92int main(int argc, char * argv[])
93{
94 int n = 10;
95 if (argc > 1)
96 {
97 try { n = std::stoi(argv[1]); }
98 catch (...) { n = 10; }
99 }
100
101 if (n <= 0)
102 {
103 cerr << "n must be greater than 0" << endl;
104 return 1;
105 }
106
107 double prob = 0.5;
108 if (argc > 2)
109 {
110 try { prob = std::stod(argv[2]); }
111 catch (...) { prob = 0.5; }
112 }
113
114 unsigned int t = static_cast<unsigned int>(time(nullptr));
115 if (argc > 3)
116 {
117 try { t = static_cast<unsigned int>(std::stoul(argv[3])); }
118 catch (...) { t = static_cast<unsigned int>(time(nullptr)); }
119 }
120
121 srand(t);
122
123 cout << argv[0] << " " << n << " " << prob << " " << t << " " << endl;
124
125 std::vector<int> keys(static_cast<size_t>(n));
126
127 BinTree<int> tree;
128 BinTree<int>::Node * node;
129 int value;
130
131 for (int i = 0; i < n; i++)
132 {
133 do
134 {
135 value = static_cast<int>(10.0 * n * rand() / (RAND_MAX + 1.0));
136 }
137 while (tree.search(value) not_eq nullptr);
138
139 node = new BinTree<int>::Node (value);
140 tree.insert(node);
141 }
142
143 /* Coloca las claves del arbol en el arreglo keys */
144 k = keys.data();
145 inOrderRec(tree.getRoot(), assign);
146 cout << endl;
147
148 /* construye arreglo de probabilidades segun dist binomial */
149 std::vector<double> p(static_cast<size_t>(n));
150 for (int i = 0; i < n; i++)
151 {
152 p[static_cast<size_t>(i)] = bin_coef(n, i) * pow(prob, i) * pow((1 - prob), n - i);
153 printf("%.5f ", p[static_cast<size_t>(i)]);
154 }
155
157 build_optimal_tree<BinNode<int>, int>(keys.data(), p.data(), static_cast<size_t>(n));
158
159 cout << endl;
161
162 destroyRec(tree.getRoot());
164
165 return 0;
166}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
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:4122
__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:4072
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
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
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.