Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
testArrayHeap.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 <cstdlib>
34# include <iostream>
35# include <ctime>
36# include <string>
37# include <tpl_arrayHeap.H>
38
39int keys [] = { 36, 32, 4, 12, 52, 59, 2, 2, 26, 1 };
40
41int * keys_ptr = keys;
42
43using namespace std;
44using namespace Aleph;
45
46
47# define NIL (-1)
48# define LLINK(i, n) (2*i)
49# define RLINK(i, n) (2*i + 1)
50
51
52void preorder(int v[], int n, int i)
53{
54 if (i > n)
55 return;
56
57 cout << " " << v[i];
58
59 preorder(v, n, LLINK(i, n));
60 preorder(v, n, RLINK(i, n));
61}
62
63
64void inorder(int v[], int n, int i)
65{
66 if (i > n)
67 return;
68
69 inorder(v, n, LLINK(i, n));
70 cout << " " << v[i];
71 inorder(v, n, RLINK(i, n));
72}
73
74
75
76
77int main(int argc, char* argv[])
78{
79 srand(std::time(0));
80 int n = 10;
81
82 if (argc > 1)
83 {
84 try
85 {
86 n = stoi(argv[1]);
87 }
88 catch (...)
89 {
90 n = 10;
91 }
92 }
93
94 if (n <= 0)
95 {
96 cerr << "Error: n must be a positive integer." << endl;
97 return 1;
98 }
99
100 {
101 // ArrayHeap<int*> heap(n, [] (int * ptr1, int * ptr2)
102 // {
103 // return *ptr1 < *ptr2;
104 // });
105 }
106
107 {
108 ArrayHeap<int> heap(n);
109 unsigned int i, value;
110
111 for (i = 0; i < n; i++)
112 {
113 value = 1 + (int) (100.0*rand()/(RAND_MAX+1.0));
114 // value = keys[i];
115 cout << value << " ";
116 heap.insert(value);
117 }
118
119 cout << endl
120 << "Preorder ";
121
122 preorder(&heap[0], heap.size(), 1);
123
124 cout << "\n\n";
125
126 cout << endl
127 << "inorder ";
128
129 inorder(&heap[0], heap.size(), 1);
130
131 cout << "\n\n";
132
133 for (i = 1; i <= heap.size(); i++)
134 cout << heap[i] << " ";
135
136 cout << "\n\n";
137
138 for (i = 0; i < n; i++)
139 {
140 value = heap.getMin();
141 cout << value << " ";
142 }
143
144 cout << "\n\n";
145 }
146
147 {
148 int * array1 = new int [n];
149
150 int * array2 = new int [n];
151
152 for (int i = 0; i < n; i++)
153 {
154 int value = 1 + (int) (100.0*rand()/(RAND_MAX+1.0));
155 array1[i] = array2[i] = value;
156 }
157 heapsort(array1, n);
158
159 for (int i = 0; i < n; i++)
160 cout << array1[i] << " ";
161
162 cout << endl;
163
164 delete [] array1;
165
167
168 for (int i = 0; i < n; i++)
169 cout << array2[i] << " ";
170
171 cout << endl;
172
173 delete [] array2;
174 }
175}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Fixed-capacity binary heap backed by a raw array.
T & insert(const T &key)
Insert an element into the heap.
T getMin()
Remove the smallest element in the heap and return a copy of its value.
constexpr size_t size() const noexcept
Return the number of elements currently stored.
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
void heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Sort an array using the heapsort algorithm.
void faster_heapsort(T *array, const size_t n, const Compare &cmp=Compare())
Optimized version of heapsort.
STL namespace.
#define RLINK(i, n)
int keys[]
#define LLINK(i, n)
int * keys_ptr
DynArray< int > preorder
DynArray< int > inorder
Fixed-capacity binary heap and heapsort algorithms.