egogoboy

k-left-tree

May 5th, 2025
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.33 KB | None | 0 0
  1. #include <iostream>                                                                                                                                                                                        
  2. #include <iomanip>                                                                                                                                                                                          
  3. #include "Node.h"                                                                                                                                                                                          
  4.                                                                                                                                                                                                            
  5. int getMin(Node* node);                                                                                                                                                                                    
  6. bool isLeaf(Node* node);                                                                                                                                                                                    
  7.                                                                                                                                                                                                            
  8. Node* makeTree(int k, int h, int& count) {                                                                                                                                                                  
  9.     if (h == 1 || (h > 1 && k == 0)) {                                                                                                                                                                      
  10.         ++count;                                                                                                                                                                                            
  11.         return new Node();                                                                                                                                                                                  
  12.     }                                                                                                                                                                                                      
  13.                                                                                                                                                                                                            
  14.     return new Node(makeTree(k - 1, h - 1, count), makeTree(k, h - 1, count));                                                                                                                              
  15. }                                                                                                                                                                                                          
  16.                                                                                                                                                                                                            
  17. Node* growTree(Node* node, int k, int max_k, int& count, int max_n) {                                                                                                                                      
  18.     if (k == max_k || count == max_n) {                                                                                                                                                                    
  19.         return node;
  20. }                                                                                                                                                                                                      
  21.     if (isLeaf(node)) {                                                                                                                                                                                    
  22.         node->left = new Node();                                                                                                                                                                            
  23.         node->right = new Node();                                                                                                                                                                          
  24.         count++;                                                                                                                                                                                            
  25.         return node;                                                                                                                                                                                        
  26.     }                                                                                                                                                                                                      
  27.                                                                                                                                                                                                            
  28.     node->left = growTree(node->left, k + 1, max_k, count, max_n);                                                                                                                                          
  29.     node->right = growTree(node->right, k, max_k, count, max_n);                                                                                                                                            
  30.                                                                                                                                                                                                            
  31.     return node;                                                                                                                                                                                            
  32. }                                                                                                                                                                                                          
  33.                                                                                                                                                                                                            
  34. Node* fillTree(Node* node, int& depth) {                                                                                                                                                                    
  35.     if (node == nullptr) {                                                                                                                                                                                  
  36.         return nullptr;                                                                                                                                                                                    
  37.     }                                                                                                                                                                                                      
  38.  
  39.     node->left = fillTree(node->left, depth);
  40.     node->right = fillTree(node->right, depth);
  41.     if (isLeaf(node)) {
  42.         node->key = depth++;
  43.     } else {
  44.         node->key = getMin(node->right);
  45.     }
  46.  
  47.     return node;
  48. }
  49. void printTree(Node* node, int width) {
  50.     if (node == nullptr) {
  51.         return;
  52.     }
  53.     printTree(node->right, width + 5);
  54.     std::cout << std::string(width, ' ') << node->key << std::endl;
  55.     printTree(node->left, width + 5);
  56. }
  57.  
  58. int main() {
  59.     int k, n;
  60.     std::cin >> k >> n;
  61.    
  62.     int h = 1;
  63.     int count = 0;
  64.     Node* head = makeTree(k, h, count);
  65.     while (count != n + 1) {
  66.         head = growTree(head, 0, k, count, n + 1);
  67.     }
  68.  
  69.     int depth = 0;
  70.     head = fillTree(head, depth);
  71.  
  72.     printTree(head, 0);
  73.  
  74.  
  75.     return 0;
  76. }
  77.  
  78. bool isLeaf(Node* node) {
  79.     return node->left == nullptr && node->right == nullptr;
  80. }
  81.  
  82. int getMin(Node* node) {
  83.     if (node == nullptr) {
  84.         return 0;
  85.     }
  86.  
  87.     if (node->left == nullptr) {
  88.         return node->key;
  89.     }
  90.  
  91.     return getMin(node->left);
  92. }
Advertisement
Add Comment
Please, Sign In to add comment