Advertisement
cepxuozab

IterativeTree

Aug 4th, 2023
912
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.83 KB | None | 0 0
  1. TreeNode<int>* MakeTree(int value, TreeNode<int>* parent, int depth, int max_depth) {
  2.         auto new_node = new TreeNode<int>{value, parent, nullptr, nullptr};
  3.         if (depth <= max_depth) {
  4.             new_node->left = MakeTree(value - (1<<(max_depth - depth)), new_node, depth + 1, max_depth);
  5.             new_node->right = MakeTree(value + (1<<(max_depth - depth)), new_node, depth + 1, max_depth);
  6.         }
  7.         return new_node;
  8.     }
  9.  
  10.     void NextEffectiveness() {
  11.         using namespace std;
  12.         using T = TreeNode<int>;
  13.  
  14.         T* root = MakeTree(0, nullptr, 0, 20);
  15.         cout << root << endl;
  16.  
  17.         T* iter = begin(root);
  18.  
  19.         while (iter) {
  20.             cout << iter->value << " "s;
  21.             iter = next(iter);
  22.         }
  23.         cout << endl;  
  24.        
  25.         DeleteTree(root);    
  26.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement