#include <iostream> #include <vector> struct ArrNode { int data; int left; int right;…
- User
#include #include
struct ArrNode { int data; int left; int right;
ArrNode() : data(0), left(-1), right(-1) {} ArrNode(int data, int left, int right) : data(data), left(left), right(right) {} };
class AVL { public:
struct Node { int value; Node* left; Node* right; int height; Node() : left(nullptr), right(nullptr) {} Node(int value) : value(value), left(nullptr), right(nullptr), height(1) {} }; int GetHeight(Node* node) { if (node == nullptr) return 0; return node->height; } void UpdateHeight(Node* node) { if (node->left == 0 && node->right == 0) { node->height = 0; } else { node->height = 1 + std::max(GetHeight(node->left), GetHeight(node->right)); } } Node* Insert(Node* node, ArrNode value) { if (node->value == 0 || node == nullptr) { return new Node(value.data); } if (value.data < node->value) { node->left = Insert(node->left, value); } else if (value.data > node->value) { node->right = Insert(node->right, value); } else { return node; } node->height = 1 + std::max(GetHeight(node->left), GetHeight(node->right)); return node; } Node* LeftRotation(Node* x) { Node *y = x->right; Node *T2 = y->left; y->left = x; x->right = T2; x->height = std::max(GetHeight(x->left), GetHeight(x->right)) + 1; y->height = std::max(GetHeight(y->left), GetHeight(y->right)) + 1; return y;}
Node* RightRotation(Node* y) { Node *x = y->left;
Node *T2 = x->right;x->right = y; y->left = T2; y->height = std::max(GetHeight(y->left), GetHeight(y->right)) + 1; x->height = std::max(GetHeight(x->left), GetHeight(x->right)) + 1; return x;}
bool Search(Node* node, int value) { if (node->value == 0) { return false; } if (value == node->value) { return true; } if (value < node->value) { return Search(node->left, value); } else { return Search(node->right, value); } } void Insert(ArrNode value) { root = Insert(root, value); } bool Search(int value) { return Search(root, value); } void Balance() { if (GetHeight(root->right->left) > GetHeight(root->right->right)) { root->right = RightRotation(root->right); } root = LeftRotation(root); }void PrintCurrentLevel(Node* root, int level, int& counter_childs, int& index, ArrNode arr[]) { if (root == nullptr) return; if (level == 1) { arr[index].data = root->value; if (root->left != nullptr) { arr[index].left = ++counter_childs; } else { arr[index].left = 0; } if (root->right != nullptr) { arr[index].right = ++counter_childs; } else { arr[index].right = 0; } ++index; } else if (level > 1) { PrintCurrentLevel(root->left, level - 1, counter_childs, index, arr); PrintCurrentLevel(root->right, level - 1, counter_childs, index, arr); } }
void PrintLevelOrder(ArrNode arr[]) { int h = GetHeight(root); int counter_childs = 1; int index = 0; for (int i = 1; i <= h; i++) { PrintCurrentLevel(root, i, counter_childs, index, arr); } }
- Assistant
- User
смотри мне нужно создать свой архивато(без сжатия) который будет помещать файлы в архив расширением .haf как мне такое создать? cpp
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.