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 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 <cstdlib>
29# include <iostream>
30# include <ctime>
31# include <string>
32# include <tpl_arrayHeap.H>
33
34int keys [] = { 36, 32, 4, 12, 52, 59, 2, 2, 26, 1 };
35
36int * keys_ptr = keys;
37
38using namespace std;
39using namespace Aleph;
40
41
42# define NIL (-1)
43# define LLINK(i, n) (2*i)
44# define RLINK(i, n) (2*i + 1)
45
46
47void preorder(int v[], int n, int i)
48{
49 if (i > n)
50 return;
51
52 cout << " " << v[i];
53
54 preorder(v, n, LLINK(i, n));
55 preorder(v, n, RLINK(i, n));
56}
57
58
59void inorder(int v[], int n, int i)
60{
61 if (i > n)
62 return;
63
64 inorder(v, n, LLINK(i, n));
65 cout << " " << v[i];
66 inorder(v, n, RLINK(i, n));
67}
68
69
70
71
72int main(int argc, char* argv[])
73{
74 srand(std::time(0));
75 int n = 10;
76
77 if (argc > 1)
78 {
79 try
80 {
81 n = stoi(argv[1]);
82 }
83 catch (...)
84 {
85 n = 10;
86 }
87 }
88
89 if (n <= 0)
90 {
91 cerr << "Error: n must be a positive integer." << endl;
92 return 1;
93 }
94
95 {
96 // ArrayHeap<int*> heap(n, [] (int * ptr1, int * ptr2)
97 // {
98 // return *ptr1 < *ptr2;
99 // });
100 }
101
102 {
103 ArrayHeap<int> heap(n);
104 unsigned int i, value;
105
106 for (i = 0; i < n; i++)
107 {
108 value = 1 + (int) (100.0*rand()/(RAND_MAX+1.0));
109 // value = keys[i];
110 cout << value << " ";
111 heap.insert(value);
112 }
113
114 cout << endl
115 << "Preorder ";
116
117 preorder(&heap[0], heap.size(), 1);
118
119 cout << "\n\n";
120
121 cout << endl
122 << "inorder ";
123
124 inorder(&heap[0], heap.size(), 1);
125
126 cout << "\n\n";
127
128 for (i = 1; i <= heap.size(); i++)
129 cout << heap[i] << " ";
130
131 cout << "\n\n";
132
133 for (i = 0; i < n; i++)
134 {
135 value = heap.getMin();
136 cout << value << " ";
137 }
138
139 cout << "\n\n";
140 }
141
142 {
143 int * array1 = new int [n];
144
145 int * array2 = new int [n];
146
147 for (int i = 0; i < n; i++)
148 {
149 int value = 1 + (int) (100.0*rand()/(RAND_MAX+1.0));
150 array1[i] = array2[i] = value;
151 }
152 heapsort(array1, n);
153
154 for (int i = 0; i < n; i++)
155 cout << array1[i] << " ";
156
157 cout << endl;
158
159 delete [] array1;
160
162
163 for (int i = 0; i < n; i++)
164 cout << array2[i] << " ";
165
166 cout << endl;
167
168 delete [] array2;
169 }
170}
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.
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.
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.
#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.