#include <iostream> #include <string> struct Node { int data; Node* left; Node* right;…
- User
#include #include
struct Node { int data; Node* left; Node* right; int height; };
struct BST { Node* top = nullptr;
Node* CreateNode(int value) { Node* newNode = new Node; newNode->data = value; newNode->height = 1; newNode->left = newNode->right = nullptr; return newNode; }
Node* Insert(Node* node, int value) { if (node == nullptr) return CreateNode(value); if (value < node->data) { node->left = Insert(node->left, value); } else if (value > node->data) { node->right = Insert(node->right, value); }
node->height = 1 + std::max(node->left ? node->left->height : 0, node->right ? node->right->height : 0); return node;}
int Find(int value) { Node* current = top; while (current != nullptr) { if (value == current->data) { return current->height; } current = (value > current->data) ? current->right : current->left; } return -1; } };
int main() { BST bst; Node* top; std::string str; std::cin >> str; for (int i = 0; i < str.length(); ++i) { bst.top = bst.Insert(top, str[i] - 'a'); }
int length = 0; for (int i = 0; i < str.length(); ++i) { length += bst.Find(str[i] - 'a'); } std::cout << length; }
❯ ./main daccaba [1] 28909 segmentation fault ./main
исправь
- Assistant
Похоже, что в вашем коде есть несколько ошибок:
- В функции
mainвы используете неинициализированную переменнуюtop, которая передается вInsert, вместо использованияbst.top. - Ваша функция
Insertдолжна в конце возвращать новый корень поддерева, но она возвращает тот же узел, что был передан на вход, что может привести к потере новых узлов.
- В функции
- User
#include #include
struct Node { int data; Node* left; Node* right; int height; };
struct BST { Node* top = nullptr;
Node* CreateNode(int value) { Node* newNode = new Node; newNode->data = value; newNode->height = 1; newNode->left = newNode->right = nullptr; return newNode; }
Node* Insert(Node* node, int value) { if (node == nullptr) return CreateNode(value); if (value < node->data) { node->left = Insert(node->left, value); } else if (value > node->data) { node->right = Insert(node->right, value); }
node->height = 1 + std::max(node->left ? node->left->height : 0, node->right ? node->right->height : 0); return node;}
int Find(int value) { Node* current = top; while (current != nullptr) { if (value == current->data) { return current->height; } current = (value > current->data) ? current->right : current->left; } return -1; } };
- Assistant
- User
#include #include
struct Node { int data; Node* left; Node* right; int height; };
struct BST { Node* top = nullptr;
Node* CreateNode(int value) { Node* newNode = new Node; newNode->data = value; newNode->height = 1; newNode->left = newNode->right = nullptr; return newNode; }
Node* Insert(Node* node, int value) { if (node == nullptr) return CreateNode(value); if (value < node->data) { node->left = Insert(node->left, value); } else if (value > node->data) { node->right = Insert(node->right, value); }
node->height = 1 + std::max(node->left ? node->left->height : 0, node->right ? node->right->height : 0); return node;}
int Find(int value) { Node* current = top; while (current != nullptr) { if (value == current->data) { return current->height; } current = (value > current->data) ? current->right : current->left; } return -1; } };
int main() { BST bst; std::string str; std::cin >> str; for (int i = 0; i < str.length(); ++i) { bst.top = bst.Insert(bst.top, str[i] - 'a'); }
int length = 0; for (int i = 0; i < str.length(); ++i) { length += bst.Find(str[i] - 'a'); } std::cout << length; }
замени BST на мин кучу
- Assistant