Advertisement
kxcoze

cringeTrees

Nov 4th, 2020 (edited)
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct TreeNode {
  6.     int data;
  7.     TreeNode *left;
  8.     TreeNode *right;
  9. };
  10.  
  11. TreeNode *root1 = NULL, *root2 = NULL;
  12.  
  13. void buildTree(); // Функция постройки дерева
  14. void initCopyTree();
  15. void insertFromChild(TreeNode*, int);
  16. void insertFromRoot(int);
  17. void destroyTree(TreeNode*);
  18. void preorderPrint(TreeNode*);
  19. void copyTree(TreeNode*, TreeNode*);
  20.  
  21. int main() {
  22.     setlocale(LC_ALL, "rus");
  23.  
  24.     buildTree();
  25.    
  26.     initCopyTree();
  27.     preorderPrint(root2);
  28.  
  29.  
  30.     destroyTree(root2);
  31.     destroyTree(root1);
  32.     return 0;
  33. }
  34.  
  35. void insertFromChild(TreeNode *leaf, int key) {
  36.     if (key < leaf->data) {
  37.         if (leaf->left != NULL)
  38.             insertFromChild(leaf->left, key);
  39.         else {
  40.             leaf->left = new TreeNode;
  41.             leaf->left->data = key;
  42.             leaf->left->left = NULL;
  43.             leaf->left->right = NULL;
  44.         }
  45.     }
  46.     else {
  47.         if (leaf->right != NULL)
  48.             insertFromChild(leaf->right, key);
  49.         else {
  50.             leaf->right = new TreeNode;
  51.             leaf->right->data = key;
  52.             leaf->right->left = NULL;
  53.             leaf->right->right = NULL;
  54.         }
  55.     }
  56. }
  57.  
  58. void insertFromRoot(int key) {
  59.     if (root1 != NULL) {
  60.         insertFromChild(root1, key);
  61.     }
  62.     else {
  63.         root1 = new TreeNode;
  64.         root1->data = key;
  65.         root1->left = NULL;
  66.         root1->right = NULL;
  67.     }
  68. }
  69.  
  70. void buildTree() {
  71.     int n;
  72.     cout << "Enter count of elements to insert in bintree:\n";
  73.     cin >> n;
  74.     cout << "Input elements:\n";
  75.     for (int i = 0; i < n; i++) {
  76.         int req;
  77.         cin >> req;
  78.         insertFromRoot(req);
  79.     }
  80. }
  81.  
  82. void destroyTree(TreeNode *leaf) {
  83.     if (leaf != NULL) {
  84.         destroyTree(leaf->left);
  85.         destroyTree(leaf->right);
  86.         delete leaf;
  87.     }
  88. }
  89.  
  90. void preorderPrint(TreeNode *root) {
  91.     if (root == NULL) {
  92.         return;
  93.     }
  94.     cout << root->data << ' ';
  95.     preorderPrint(root->left);
  96.     preorderPrint(root->right);
  97. }
  98.  
  99. void initCopyTree() {
  100.     if (root1 != NULL && root2 == NULL) {
  101.         root2 = new TreeNode;
  102.         root2->data = root1->data;
  103.         root2->left = NULL;
  104.         root2->right = NULL;
  105.         copyTree(root1, root2);
  106.     }
  107. }
  108.  
  109. void copyTree(TreeNode *rootFrom, TreeNode *rootTo) {
  110.     if (rootFrom->left != NULL) {
  111.         rootTo->left = new TreeNode;
  112.         rootTo->left->data = rootFrom->left->data;
  113.         rootTo->left->left = NULL;
  114.         rootTo->left->right = NULL;
  115.         copyTree(rootFrom->left, rootTo->left);
  116.     }
  117.     else {
  118.         rootTo->left = NULL;
  119.     }
  120.  
  121.     if (rootFrom->right != NULL) {
  122.         rootTo->right = new TreeNode;
  123.         rootTo->right->data = rootFrom->right->data;
  124.         rootTo->right->left = NULL;
  125.         rootTo->right->right = NULL;
  126.         copyTree(rootFrom->right, rootTo->right);
  127.     }
  128.     else {
  129.         rootTo->right = NULL;
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement