kxcoze

AlyonaTrees

Nov 8th, 2020 (edited)
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 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 *root;
  12.  
  13. void buildTree(); // Функция постройки дерева
  14. void insertFromChild(TreeNode*, int);
  15. void insertFromRoot(int);
  16. void destroyTree(TreeNode*);
  17. void preorderPrint(TreeNode*);
  18. void findMaxValue(TreeNode*, int&);
  19.  
  20. int main() {
  21.     setlocale(LC_ALL, "rus");
  22.     buildTree();
  23.     preorderPrint(root);
  24.     int k = -1e9 - 1;
  25.     if (root != NULL) {
  26.         k = root->data;
  27.         findMaxValue(root, k);
  28.         cout << "\nНаибольшое значение ключа из всех узлов дерева: " << k << '\n';
  29.     } else
  30.         cout << "\nВаше дерево пустое!\n";
  31.     destroyTree(root);
  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 (root != NULL) {
  60.         insertFromChild(root, key);
  61.     }
  62.     else {
  63.         root = new TreeNode;
  64.         root->data = key;
  65.         root->left = NULL;
  66.         root->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 findMaxValue(TreeNode *root, int &maxValue) {
  100.     if (root == NULL) {
  101.         return;
  102.     }
  103.     maxValue = max(maxValue, root->data);
  104.     findMaxValue(root->left, maxValue);
  105.     findMaxValue(root->right, maxValue);
  106. }
Add Comment
Please, Sign In to add comment