commit 581be8e5f037443d2ffe5f21fed769a4399e55ae
parent 3e88c90e41c1b2edbfb08bf5051d0a5f552c255b
Author: = <=>
Date: Thu, 23 Oct 2025 21:16:47 +0300
Dialog with user in solution added
Diffstat:
| M | latest_solution.cpp | | | 84 | +++++++++++++++++++++++++++++++++++++++++++++---------------------------------- |
1 file changed, 48 insertions(+), 36 deletions(-)
diff --git a/latest_solution.cpp b/latest_solution.cpp
@@ -30,20 +30,44 @@ Node* right_turn(Node*);
Node* search(Node*, int);
+int steps = 0;
+
+
int main() {
+ setlocale(LC_ALL, "Russian");
+
Node* root = nullptr;
+ int data_limit;
+
+ std::cout << "Количество элементов: ";
+ std::cin >> data_limit;
+ std::cout << std::endl;
+
int k = 1;
- while (k > 0)
+ while (k <= data_limit)
+ {
+ root = add(k, root);
+ k++;
+ }
+
+
+ int search_value = 0;
+ int attempt = 0;
+
+ while (search_value < 1 || search_value > data_limit)
{
- std::cin >> k;
- if (k > 0)
- root = add(k, root);
+ if(attempt) std::cout << "Невозможное значение." << std::endl << std::endl;
+ std::cout << "Искомое значение: ";
+ std::cin >> search_value;
+ std::cout << std::endl;
+ attempt++;
}
+
+ Node* s = search(root, search_value);
- printf("root: %d\nleft: %d\nright: %d\nleft left: %d\nleft right: %d\nright left: %d\nright right: %d\n", root->val, root->left->val, root->right->val, root->left->left->val, root->left->right->val, root->right->left->val, root->right->right->val);
- search(root, 5000);
+ std::cout << "Найденное значение: " << s->val << std::endl << "Затрачено шагов: " << steps << std::endl;
return 0;
}
@@ -52,10 +76,10 @@ int main() {
Node*
init(int val)
{
- Node *n = (Node *) malloc(sizeof(Node));
+ Node* n = (Node*)malloc(sizeof(Node));
if (n == NULL)
return NULL;
-
+
n->val = val;
n->left = NULL;
n->right = NULL;
@@ -69,7 +93,7 @@ add(int val, Node* root)
{
if (!root)
return init(val);
-
+
if (val < root->val)
root->left = add(val, root->left);
else if (val > root->val)
@@ -77,33 +101,33 @@ add(int val, Node* root)
else
return root;
- return balance2(root);
+ return balance(root);
}
-Node*
-parse_pre(Node* root)
+Node*
+parse_pre(Node* root)
{
int val = root->val;
- // do smth with val
+ std::cout << val << " ";
if (root->left) parse_pre(root->left);
if (root->right) parse_pre(root->right);
-
+
return NULL;
}
Node*
-parse_post(Node* root)
+parse_post(Node* root)
{
if (root->left) parse_post(root->left);
if (root->right) parse_post(root->right);
-
+
int val = root->val;
- // do smth with val
+ std::cout << val << " ";
return NULL;
}
@@ -116,7 +140,7 @@ parse_inf(Node* root)
int val = root->val;
- // do smth with val
+ std::cout << val << " ";
if (root->right) parse_inf(root->right);
@@ -142,18 +166,19 @@ get_height(Node* root)
return r;
}
+
inline int
get_balance_factor(Node* root)
{
if (!root)
return 0;
-
+
return get_height(root->left) - get_height(root->right);
}
Node*
-right_turn(Node* root)
+right_turn(Node* root)
{
if (!root || !root->left)
return root;
@@ -181,23 +206,9 @@ left_turn(Node* root)
Node*
balance(Node* root)
{
- int h_l = get_height(root->left), h_r = get_height(root->right);
-
- if (std::abs(h_l - h_r) > 1) {
- if (h_l > h_r) return right_turn(root);
-
- return left_turn(root);
- }
-
- return root;
-}
-
-Node*
-balance2(Node* root)
-{
if (!root)
return nullptr;
-
+
int bf = get_balance_factor(root);
@@ -220,9 +231,10 @@ balance2(Node* root)
Node*
search(Node* root, int val)
{
+ steps++;
if (root->val == val) return root;
if (root->val > val) return search(root->left, val);
-
+
return search(root->right, val);
}