Advertisement
MadCortez

Untitled

Nov 11th, 2021
1,096
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #define MIN_SIZE 0
  2. #define MAX_SIZE 20
  3. #define MAX_DEPTH 5
  4. #define OFFSET 30
  5. #define LEFT 0
  6. #define RIGHT 1
  7.  
  8. int foo(short n) {
  9.     switch (n) {
  10.     case 1:
  11.         return 8;
  12.         break;
  13.     case 2:
  14.         return 4;
  15.         break;
  16.     case 3:
  17.         return 2;
  18.         break;
  19.     case 4:
  20.         return 1;
  21.         break;
  22.     default: return -1;
  23.     }
  24. }
  25.  
  26. void printTree(Node* tree, int currentSet, short depth, int index, char** str) {
  27.     if (!tree || depth < 1 || depth > MAX_DEPTH)
  28.         return;
  29.     int x = currentSet;
  30.     int z = foo(depth);
  31.     char* s = str[index];
  32.     s += currentSet - 1;
  33.     int a = sprintf(s, "%2d", tree->field);
  34.     s[2] = ' ';
  35.     currentSet--;
  36.     index++;
  37.     for (int j = 0; j < z; j++) {
  38.         s = str[index];
  39.         s += currentSet;
  40.         if (tree->left)
  41.             s[0] = '/';
  42.         s += (x - currentSet) * 2;
  43.         if (tree->right)
  44.             s[0] = '\\';
  45.         index++;
  46.         currentSet--;
  47.     }
  48.     printTree(tree->left, currentSet, depth + 1, index, str);
  49.     printTree(tree->right, (x + z + 1), depth + 1, index, str);
  50. }
  51.  
  52. char** str = new char* [64];
  53.     for (int i = 0; i < 64; i++) {
  54.         str[i] = new char[64];
  55.         memset(str[i], ' ', 64);
  56.         str[i][63] = 0;
  57.     }
  58.     printTree(tree, OFFSET, 1, 0, str);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement