Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testBinHeap.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# include <ctime>
28# include <cstdlib>
29# include <cassert>
30
31# include <iostream>
32# include <string>
33# include <aleph.H>
34# include <tpl_dynArray.H>
35# include <tpl_binHeap.H>
36# include <tpl_binNodeUtils.H>
37
38using namespace std;
39using namespace Aleph;
40
41static void printNode(BinHeap<int>::Node* node, int, int)
42{
43 cout << node->get_key() << " ";
44}
45
46
47int main(int argc, char *argv[])
48{
49 int n = 1000;
50 if (argc > 1)
51 {
52 try { n = stoi(argv[1]); } catch (...) { n = 1000; }
53 }
54
55 if (n <= 0)
56 {
57 cerr << "n must be positive" << endl;
58 return 1;
59 }
60
61 unsigned int t = std::time(0);
62 if (argc > 2)
63 {
64 try { t = stoul(argv[2]); } catch (...) { t = std::time(0); }
65 }
66
67 srand(t);
68
69 cout << "testBinHeap " << n << " " << t << endl;
70
71 BinHeap<int> heap;
73 int i;
74
75 for (i = n - 1; i >= 0; i--)
76 {
77 node = new BinHeap<int>::Node (i);
78 heap.insert(node);
79 }
80
81 assert(heap.verify_heap());
82
83 for (i = 0; i < n; i++)
84 {
85 node = heap.getMin();
86 delete node;
87 }
88
89 assert(heap.verify_heap());
90
91 int value;
92
93 for (i = n - 1; i >= 0; i--)
94 {
95 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
96 node = new BinHeap<int>::Node (value);
97 heap.insert(node);
98 cout << value << " ";
99 }
100 cout << endl;
101
102 assert(heap.verify_heap());
103
104 for (i = 0; i < n; i++)
105 {
106 node = heap.getMin();
107 delete node;
108 }
109
110 assert(heap.verify_heap());
111 assert(heap.size() == 0);
112
113 for (i = n - 1; i >= 0; i--)
114 {
115 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
116 node = new BinHeap<int>::Node (value);
117 heap.insert(node);
118 }
119
120 assert(heap.verify_heap());
121
122 for (i = 0; i < n/2; i++)
123 {
124 node = heap.getMin();
125 delete node;
126 }
127 assert(heap.verify_heap());
128
129 for (i = n - 1; i >= 0; i--)
130 {
131 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
132 node = new BinHeap<int>::Node (value);
133 heap.insert(node);
134 }
135 assert(heap.verify_heap());
136
137 for (i = 0; i <= n + n/2; i++)
138 {
139 try
140 {
141 node = heap.getMin();
142 delete node;
143 }
144 catch (exception & e)
145 {
146 cout << e.what() << endl;
147 }
148 }
149 assert(heap.verify_heap());
150 assert(heap.size() == 0);
151
152
153 n = 4;
155 // BinHeap<int>::Node* nodes[2*n];
156 for (i = 2*n - 1; i >= 0; i--)
157 {
158 node = new BinHeap<int>::Node (i);
159 heap.insert(node);
160 nodes[i] = node;
161 }
162
163 assert(heap.verify_heap());
164
165 for (i = 0; i < n/2; i++)
166 {
167 value = (int) (1.0*n*rand()/(RAND_MAX+1.0));
168 if (nodes[value] != NULL)
169 {
170 node = heap.remove(nodes[value]);
171 delete node;
172 nodes[value] = NULL;
173 }
174 }
175
176 assert(heap.verify_heap());
177
179
180 for (i = 0; i < n/2; i++)
181 {
182 value = (int) (n*100.0*rand()/(RAND_MAX+1.0));
183 node = new BinHeap<int>::Node (value);
184 heap.insert(node);
185 }
186 assert(heap.verify_heap());
187
188
189
190 while (heap.size() > 0)
191 {
192 node = heap.getMin();
193 delete node;
194 }
195
196 assert(heap.verify_heap());
197 assert(heap.size() == 0);
198
199 for (i = n - 1; i >= 0; i--)
200 {
201 node = new BinHeap<int>::Node (i);
202 heap.insert(node);
203 }
204 assert(heap.verify_heap());
205
206 for (i = 0; i < n; i++)
207 {
208 node = heap.getMin();
209 delete node;
210 }
211 assert(heap.verify_heap());
212 assert(heap.size() == 0);
213
214 cout << "End" << endl;
215}
Core header for the Aleph-w library.
Key & get_key() noexcept
virtual bool verify_heap(Node *p) const
Node * getMin()
Elimina del heap el nodo de menor prioridad.
Node * remove(Node *node)
Elimina del heap el nodo node.
void remove_all_and_delete() noexcept
Borra todos los nodos del heap, invoca a los destructores de los nodos eliminados y libera toda la me...
Node * insert(Node *p) noexcept
Inserta un nodo en un heap.
const size_t & size() const noexcept
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
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.
Node heap without virtual destructor.
BinHeapNode< Key > Node
El tipo de nodo del heap.
static void printNode(BinHeap< int >::Node *node, int, int)
Definition testBinHeap.C:41
Binary heap implementation using tree structure.
Utility functions for binary tree operations.
Lazy and scalable dynamic array implementation.