优先级队列  0.1
数据结构_第7章
ch7_7.cc
Go to the documentation of this file.
1 
14 #include "BinaryHeap.h"
15 #include <algorithm>
16 #include <climits>
17 #include <cstdlib> /* srand, rand */
18 #include <ctime>
19 #include <fstream>
20 #include <vector>
21 
22 namespace Queue
23 {
28  class priority_queue_int : public Queue::BinaryHeap<int, std::greater<int>>
29  {
30  public:
32  : BinaryHeap(items) {}
33 
40  size_type findMin(value_type x) const;
41 
48  void decreaseKey(size_type i, value_type value);
49 
56  void increaseKey(size_type i, value_type value);
57  };
58 
59 } // namespace Queue
60 
70 int main(int argc, char const *argv[])
71 {
72  // time
73  clock_t t = clock();
74  time_t rawtime;
75  struct tm *timeinfo;
76 
77  // 获取文件路径,参考:http://www.cplusplus.com/reference/string/string/find_last_of/
78  const std::string full_path_exec{argv[0]};
79  std::string::size_type found = full_path_exec.find_last_of("/\\", std::string::npos);
80  const std::string exec_path = full_path_exec.substr(0, found);
81  const std::string exec_filename = full_path_exec.substr(found + 1, std::string::npos);
82  const std::string data_file_name("\\ch7_7.result");
83 
84  // 打开测试数据文件
85  std::string data_file_full_path{exec_path + data_file_name}; // 数据文件的绝对地址
86  std::ofstream fout(data_file_full_path.c_str(), std::ios_base::app);
87  if (fout.fail())
88  {
89  std::cerr << "无写权限,测试数据文件生成失败!\n";
90 
91  system("pause");
92  return false;
93  }
94 
95  // output time information
96  time(&rawtime); // Get the current calendar time
97  timeinfo = localtime(&rawtime); // Convert time_t to tm as local time
98  // printf("Current local time and date: %s\n", asctime(timeinfo)); // Convert tm structure to string
99  std::cout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
100  fout << "\nCurrent local time and date: " << asctime(timeinfo) << '\n';
101 
102  // 开始测试
103  try
104  {
105  int low{10}, high{99}, num_of_points{18};
106  List::seqList<int> int_array(num_of_points);
107  /* initialize random seed: */
108  srand(time(NULL));
109 
110  /* generate secret number between 1 and 10: */
111  int iSecret;
112  for (int i = 0; i < num_of_points; ++i)
113  {
114  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
115  int_array.push_back(iSecret);
116  }
117 
118  int_array.traverse(std::cout);
119  int_array.traverse(fout);
120 
121  // 从源数组建立一个最大堆
122  Queue::priority_queue_int priority_queue(int_array);
123  priority_queue.print_heap(std::cout);
124  priority_queue.print_heap(fout);
125 
126  // 依次findMin
127  for (size_t i = 0; i < priority_queue.size(); ++i)
128  {
129  iSecret = low + (high - low + 1) * rand() / (RAND_MAX + 1);
130  std::cout << "findMin(" << iSecret << ") = " << priority_queue.findMin(iSecret) << '\n';
131  fout << "findMin(" << iSecret << ") = " << priority_queue.findMin(iSecret) << '\n';
132  }
133 
134  // 全部结点加减随机数
135  for (int i = 1; i <= num_of_points; ++i)
136  {
137  iSecret = -low + (high + low + 1) * rand() / (RAND_MAX + 1);
138  priority_queue.increaseKey(i, iSecret);
139  }
140  std::cout << "\n全部结点加减随机数后: " << priority_queue << '\n';
141  fout << "\n全部结点加减随机数后: " << priority_queue << '\n';
142 
143  // 依次出队
144  std::cout << "\n依次出队: \n";
145  fout << "\n依次出队: \n";
146  for (int i = 0; i < num_of_points; ++i)
147  {
148  auto obj = priority_queue.top();
149  priority_queue.pop();
150  std::cout << obj << ", ";
151  fout << obj << ", ";
152  }
153  }
154  catch (const std::string &e)
155  {
156  std::cerr << e << '\n';
157  fout << e << '\n';
158  }
159 
160  // output time information
161  t = clock() - t;
162  // printf("\nIt took me %ld clicks (%f seconds).\n", t, ((float)t) / CLOCKS_PER_SEC);
163  std::cout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
164  fout << "\nIt took me " << t << " clicks (" << ((float)t) / CLOCKS_PER_SEC << " seconds).\n";
165 
166  // 测试结束
167  fout.close();
168  system("pause");
169  return 0;
170 } // main
171 
173 {
174  size_type current_index{0};
175  int current_min{INT_MAX};
176  for (size_type i = 1; i <= current_size_; ++i)
177  {
178  if (array_[i] >= x && array_[i] <= current_min)
179  {
180  current_min = array_[current_index = i];
181  }
182  }
183 
184  return current_index;
185 }
186 
187 void Queue::priority_queue_int::decreaseKey(size_type i, value_type value)
188 {
189  if (!value)
190  return;
191 
192  array_[i] = array_[i] - value;
193  if (value > 0)
194  percolateUp(i);
195  else
196  percolateDown(i);
197 }
198 
199 void Queue::priority_queue_int::increaseKey(size_type i, value_type value)
200 {
201  decreaseKey(i, -value);
202 }
Queue::BinaryHeap< int, std::greater< int > >::current_size_
size_type current_size_
Number of elements in heap.
Definition: BinaryHeap.h:55
Queue
队列
Definition: BinaryHeap.h:22
Queue::BinaryHeap< int, std::greater< int > >::value_type
int value_type
Type of the elements.
Definition: BinaryHeap.h:45
Queue::priority_queue_int
基于最小堆的整型的优先级队列类
Definition: ch7_7.cc:28
Queue::BinaryHeap::print_heap
void print_heap(std::ostream &out) const
将二叉堆中元素顺序输出.
Definition: BinaryHeap.h:181
Queue::BinaryHeap< int, std::greater< int > >::size_type
size_t size_type
an unsigned integral type
Definition: BinaryHeap.h:44
Queue::BinaryHeap::size
size_type size() const
Return size.
Definition: BinaryHeap.h:322
Queue::priority_queue_int::priority_queue_int
priority_queue_int(const List::seqList< value_type > &items)
Definition: ch7_7.cc:31
main
int main(int argc, char const *argv[])
测试程序
Definition: ch7_7.cc:70
Queue::priority_queue_int::findMin
size_type findMin(value_type x) const
找出优先级值大于等于指定值x的最小元素,返回其下标
Definition: ch7_7.cc:172
List::seqList< value_type >
Queue::priority_queue_int::decreaseKey
void decreaseKey(size_type i, value_type value)
将第i个结点的优先级值减小value
Definition: ch7_7.cc:187
Queue::BinaryHeap< int, std::greater< int > >::array_
List::seqList< value_type > array_
The heap array.
Definition: BinaryHeap.h:62
List::seqList::push_back
void push_back(const T &x)
在表尾插入新元素
Definition: seqList.h:439
Queue::BinaryHeap
Priority queue.
Definition: BinaryHeap.h:40
BinaryHeap.h
优先级队列的接口和实现
List::seqList::traverse
virtual void traverse(std::ostream &out=std::cout) const
Definition: seqList.h:322
Queue::BinaryHeap::top
const_reference top() const
返回队头元素值
Definition: BinaryHeap.h:215
Queue::priority_queue_int::increaseKey
void increaseKey(size_type i, value_type value)
将第i个结点的优先级值增加value
Definition: ch7_7.cc:199