hpnq

AVL template

Oct 28th, 2024
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. template <typename T>
  6. class AVLTree {
  7. private:
  8.     struct Node {
  9.         T data;
  10.         Node* left;
  11.         Node* right;
  12.         int height;
  13.  
  14.         Node(const T& value) : data(value), left(nullptr), right(nullptr), height(1) {}
  15.     };
  16.  
  17.     Node* root;
  18.  
  19.     int height(Node* node) {
  20.         return node ? node->height : 0;
  21.     }
  22.  
  23.     int balanceFactor(Node* node) {
  24.         return node ? height(node->left) - height(node->right) : 0;
  25.     }
  26.  
  27.     void updateHeight(Node* node) {
  28.         if (node) {
  29.             node->height = 1 + std::max(height(node->left), height(node->right));
  30.         }
  31.     }
  32.  
  33.     Node* rotateRight(Node* y) {
  34.         Node* x = y->left;
  35.         Node* T2 = x->right;
  36.  
  37.         x->right = y;
  38.         y->left = T2;
  39.  
  40.         updateHeight(y);
  41.         updateHeight(x);
  42.  
  43.         return x;
  44.     }
  45.  
  46.     Node* rotateLeft(Node* x) {
  47.         Node* y = x->right;
  48.         Node* T2 = y->left;
  49.  
  50.         y->left = x;
  51.         x->right = T2;
  52.  
  53.         updateHeight(x);
  54.         updateHeight(y);
  55.  
  56.         return y;
  57.     }
  58.  
  59.     Node* balance(Node* node) {
  60.         updateHeight(node);
  61.  
  62.         int bf = balanceFactor(node);
  63.  
  64.         if (bf > 1) {
  65.             if (balanceFactor(node->left) < 0) {
  66.                 node->left = rotateLeft(node->left);
  67.             }
  68.             return rotateRight(node);
  69.         }
  70.  
  71.         if (bf < -1) {
  72.             if (balanceFactor(node->right) > 0) {
  73.                 node->right = rotateRight(node->right);
  74.             }
  75.             return rotateLeft(node);
  76.         }
  77.  
  78.         return node;
  79.     }
  80.  
  81.     Node* insert(Node* node, const T& value) {
  82.         if (!node) return new Node(value);
  83.  
  84.         if (value < node->data) {
  85.             node->left = insert(node->left, value);
  86.         } else if (value > node->data) {
  87.             node->right = insert(node->right, value);
  88.         } else {
  89.             return node;
  90.         }
  91.  
  92.         return balance(node);
  93.     }
  94.  
  95.     void inorder(Node* node) const {
  96.         if (!node) return;
  97.         inorder(node->left);
  98.         std::cout << node->data << " ";
  99.         inorder(node->right);
  100.     }
  101.  
  102.     void findLongestPath(Node* node, T* path, T* longestPath, int depth, int& maxDepth) {
  103.         if (!node) return;
  104.  
  105.         path[depth] = node->data;
  106.         if (!node->left && !node->right) {
  107.             if (depth > maxDepth) {
  108.                 maxDepth = depth;
  109.                 std::copy(path, path + depth + 1, longestPath);
  110.             }
  111.         } else {
  112.             findLongestPath(node->left, path, longestPath, depth + 1, maxDepth);
  113.             findLongestPath(node->right, path, longestPath, depth + 1, maxDepth);
  114.         }
  115.     }
  116.  
  117.     void findMaxDepthDiffNode(Node* node, int& maxDiff, T& maxDiffValue) {
  118.         if (!node) return;
  119.  
  120.         int leftHeight = height(node->left);
  121.         int rightHeight = height(node->right);
  122.         int diff = std::abs(leftHeight - rightHeight);
  123.  
  124.         if (diff > maxDiff) {
  125.             maxDiff = diff;
  126.             maxDiffValue = node->data;
  127.         }
  128.  
  129.         findMaxDepthDiffNode(node->left, maxDiff, maxDiffValue);
  130.         findMaxDepthDiffNode(node->right, maxDiff, maxDiffValue);
  131.     }
  132.  
  133.     void clear(Node* node) {
  134.         if (!node) return;
  135.         clear(node->left);
  136.         clear(node->right);
  137.         delete node;
  138.     }
  139.  
  140. public:
  141.     AVLTree() : root(nullptr) {}
  142.  
  143.     ~AVLTree() {
  144.         clear(root);
  145.     }
  146.  
  147.     void insert(const T& value) {
  148.         root = insert(root, value);
  149.     }
  150.  
  151.     void printInOrder() const {
  152.         inorder(root);
  153.         std::cout << std::endl;
  154.     }
  155.  
  156.     void printLongestPath() {
  157.         if (!root) return;
  158.  
  159.         const int maxNodes = 100;
  160.         T path[maxNodes];
  161.         T longestPath[maxNodes];
  162.         int maxDepth = -1;
  163.  
  164.         findLongestPath(root, path, longestPath, 0, maxDepth);
  165.         for (int i = 0; i <= maxDepth; ++i) {
  166.             std::cout << longestPath[i] << " ";
  167.         }
  168.         std::cout << std::endl;
  169.     }
  170.  
  171.     void printMaxDepthDiffNode() {
  172.         if (!root) return;
  173.  
  174.         int maxDiff = -1;
  175.         T maxDiffValue = root->data;
  176.  
  177.         findMaxDepthDiffNode(root, maxDiff, maxDiffValue);
  178.         std::cout << "Node with max depth difference: " << maxDiffValue << std::endl;
  179.     }
  180.  
  181.     void clear() {
  182.         clear(root);
  183.         root = nullptr;
  184.     }
  185. };
  186.  
  187. // Функция для работы с AVL-деревом целых чисел
  188. void processIntegerAVLTree() {
  189.     AVLTree<int> intTree;
  190.     intTree.insert(10);
  191.     intTree.insert(20);
  192.     intTree.insert(5);
  193.     intTree.insert(15);
  194.     intTree.insert(25);
  195.  
  196.     std::cout << "In-order traversal of integer AVL tree: ";
  197.     intTree.printInOrder();
  198.  
  199.     std::cout << "Longest path in integer AVL tree: ";
  200.     intTree.printLongestPath();
  201.  
  202.     intTree.printMaxDepthDiffNode();
  203.  
  204.     intTree.clear();
  205. }
  206.  
  207. // Функция для работы с AVL-деревом строк
  208. void processStringAVLTree() {
  209.     AVLTree<std::string> stringTree;
  210.     stringTree.insert("apple");
  211.     stringTree.insert("banana");
  212.     stringTree.insert("cherry");
  213.     stringTree.insert("date");
  214.     stringTree.insert("elderberry");
  215.  
  216.     std::cout << "In-order traversal of string AVL tree: ";
  217.     stringTree.printInOrder();
  218.  
  219.     std::cout << "Longest path in string AVL tree: ";
  220.     stringTree.printLongestPath();
  221.  
  222.     stringTree.printMaxDepthDiffNode();
  223.  
  224.     stringTree.clear();
  225. }
  226.  
  227. int main() {
  228.     std::cout << "Processing integer AVL tree:" << std::endl;
  229.     processIntegerAVLTree();
  230.  
  231.     std::cout << "\nProcessing string AVL tree:" << std::endl;
  232.     processStringAVLTree();
  233.  
  234.     return 0;
  235. }
  236.  
Advertisement
Add Comment
Please, Sign In to add comment