Advertisement
Guest User

pretty print

a guest
Oct 24th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cmath>
  4.  
  5. class PQ {
  6.     int* heap;
  7.     int num_items;
  8.     bool has_children(int i) {
  9.         return 2 * i + 1 <= num_items;
  10.     }
  11.     void write_pretty_print_lines(std::string* lines, int start_line, int end_line, int i) {
  12.         if (i > num_items) {
  13.             return;
  14.         }
  15.         int n_print_line = (end_line - start_line + 1) / 2 + start_line;
  16.         std::string& curr_line = lines[n_print_line];
  17.         curr_line += std::to_string(heap[i]);
  18.         if (has_children(i)) {
  19.             curr_line += " ";
  20.             int digits = (heap[i] < 1 ? 1 : log10(heap[i]) + 1);
  21.             curr_line += std::string(4 - digits, '-');
  22.             curr_line += "+";
  23.             int l_print_line = (end_line - start_line + 1) / 4 + start_line;
  24.             int r_print_line = (end_line - start_line + 1) / 4 * 3 + start_line;
  25.             for (int i = start_line; i < l_print_line; ++i) {
  26.                 lines[i] += "     ";
  27.             }
  28.             lines[l_print_line] += "    ";
  29.             for (int i = l_print_line + 1; i < n_print_line; ++i) {
  30.                 lines[i] += "    |";
  31.             }
  32.             for (int i = n_print_line + 1; i < r_print_line; ++i) {
  33.                 lines[i] += "    |";
  34.             }
  35.             lines[r_print_line] += "    ";
  36.             for (int i = r_print_line + 1; i <= end_line; ++i) {
  37.                 lines[i] += "     ";
  38.             }
  39.             write_pretty_print_lines(lines, start_line, n_print_line, 2 * i);
  40.             write_pretty_print_lines(lines, n_print_line, end_line, 2 * i + 1);
  41.         }
  42.     }
  43.     void bottomup_heapify(int i) {
  44.         //starts at node closer to bottom, moves it upward until heap condition satisfied for that node
  45.         while (i > 1 && heap[i] > heap[i / 2]) {
  46.             std::swap(heap[i], heap[i / 2]);
  47.             i /= 2;
  48.         }
  49.     }
  50.     void topdown_heapify(int i) {
  51.         //starts at node closer to top, moves down
  52.         while (2 * i < num_items) {
  53.             int j = 2 * i;
  54.             if (j < num_items && heap[j] < heap[j + 1])
  55.                 ++j;
  56.             if (heap[i] > heap[j])
  57.                 break;
  58.             std::swap(heap[i], heap[j]);
  59.             i = j;
  60.         }
  61.     }
  62.     void print_item_array() {
  63.         for (int i = 0; i <= num_items; ++i) {
  64.             std::cout << heap[i];
  65.             if (i != num_items)
  66.                 std::cout << " | ";
  67.         }
  68.         std::cout << std::endl;
  69.     }
  70. public:
  71.     PQ(int* i, int n) {
  72.         num_items = n;
  73.         heap = new int[num_items + 1];
  74.         heap[0] = 0;
  75.         for (int j = 0; j != num_items; ++j)
  76.             heap[j + 1] = i[j];
  77.         //starting with last parent of a child
  78.         for (int i = num_items / 2; i != 0; --i)
  79.             topdown_heapify(i);
  80.     }
  81.     ~PQ() {
  82.         delete heap;
  83.     }
  84.     std::ostream& pretty_print(std::ostream& out) {
  85.         int full_tree_num_scores = pow(2, ceil(log(num_items) / log(2)));
  86.         int num_lines = full_tree_num_scores * 2;
  87.         std::string* lines = new std::string[num_lines];
  88.         for (int i = 0; i != num_lines; ++i)
  89.             if (i != num_lines / 2)
  90.                 lines[i] = " ";
  91.         write_pretty_print_lines(lines, 0, num_lines - 1, 1);
  92.         for (int i = 0; i != num_lines; ++i)
  93.             out << lines[i] << std::endl;
  94.         delete[] lines;
  95.         return out;
  96.     }
  97.     void insert(int item) {
  98.  
  99.     }
  100. };
  101.  
  102. int main()
  103. {
  104.     int items[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14};
  105.     PQ pq(items, 15);
  106.     pq.pretty_print(std::cout);
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement