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