Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <cmath>
- class PQ {
- int* heap;
- int num_items;
- bool has_children(int i) {
- return 2 * i + 1 <= num_items;
- }
- void write_pretty_print_lines(std::string* lines, int start_line, int end_line, int i) {
- if (i > num_items) {
- return;
- }
- int n_print_line = (end_line - start_line + 1) / 2 + start_line;
- std::string& curr_line = lines[n_print_line];
- curr_line += std::to_string(heap[i]);
- if (has_children(i)) {
- curr_line += " ";
- int digits = (heap[i] < 1 ? 1 : log10(heap[i]) + 1);
- curr_line += std::string(4 - digits, '-');
- curr_line += "+";
- int l_print_line = (end_line - start_line + 1) / 4 + start_line;
- int r_print_line = (end_line - start_line + 1) / 4 * 3 + start_line;
- for (int i = start_line; i < l_print_line; ++i) {
- lines[i] += " ";
- }
- lines[l_print_line] += " ";
- for (int i = l_print_line + 1; i < n_print_line; ++i) {
- lines[i] += " |";
- }
- for (int i = n_print_line + 1; i < r_print_line; ++i) {
- lines[i] += " |";
- }
- lines[r_print_line] += " ";
- for (int i = r_print_line + 1; i <= end_line; ++i) {
- lines[i] += " ";
- }
- write_pretty_print_lines(lines, start_line, n_print_line, 2 * i);
- write_pretty_print_lines(lines, n_print_line, end_line, 2 * i + 1);
- }
- }
- void bottomup_heapify(int i) {
- //starts at node closer to bottom, moves it upward until heap condition satisfied for that node
- while (i > 1 && heap[i] > heap[i / 2]) {
- std::swap(heap[i], heap[i / 2]);
- i /= 2;
- }
- }
- void topdown_heapify(int i) {
- //starts at node closer to top, moves down
- while (2 * i < num_items) {
- int j = 2 * i;
- if (j < num_items && heap[j] < heap[j + 1])
- ++j;
- if (heap[i] > heap[j])
- break;
- std::swap(heap[i], heap[j]);
- i = j;
- }
- }
- void print_item_array() {
- for (int i = 0; i <= num_items; ++i) {
- std::cout << heap[i];
- if (i != num_items)
- std::cout << " | ";
- }
- std::cout << std::endl;
- }
- public:
- PQ(int* i, int n) {
- num_items = n;
- heap = new int[num_items + 1];
- heap[0] = 0;
- for (int j = 0; j != num_items; ++j)
- heap[j + 1] = i[j];
- //starting with last parent of a child
- for (int i = num_items / 2; i != 0; --i)
- topdown_heapify(i);
- }
- ~PQ() {
- delete heap;
- }
- std::ostream& pretty_print(std::ostream& out) {
- int full_tree_num_scores = pow(2, ceil(log(num_items) / log(2)));
- int num_lines = full_tree_num_scores * 2;
- std::string* lines = new std::string[num_lines];
- for (int i = 0; i != num_lines; ++i)
- if (i != num_lines / 2)
- lines[i] = " ";
- write_pretty_print_lines(lines, 0, num_lines - 1, 1);
- for (int i = 0; i != num_lines; ++i)
- out << lines[i] << std::endl;
- delete[] lines;
- return out;
- }
- void insert(int item) {
- }
- };
- int main()
- {
- int items[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
- PQ pq(items, 15);
- pq.pretty_print(std::cout);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement