Advertisement
DaniDori

BinarySearchTree.cpp

Nov 2nd, 2023
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. #include "BinarySearchTree.h"
  2. #include <fstream>
  3. #include <iomanip>
  4.  
  5. // Конструктор
  6. BinarySearchTree::BinarySearchTree(const string& fileName) : root(nullptr), fileName(fileName) {
  7.     buildTreeFromFile();
  8. }
  9.  
  10. // Построение дерева из файла
  11. void BinarySearchTree::buildTreeFromFile() {
  12.     ifstream binaryFile(fileName, ios::binary);
  13.     if (!binaryFile.is_open()) {
  14.         cerr << "Failed to open binary file for building tree!" << endl;
  15.         return;
  16.     }
  17.  
  18.     long position = 0;
  19.     DictionaryEntry entry;
  20.     while (binaryFile.read(reinterpret_cast<char*>(&entry), sizeof(entry))) {
  21.         if (entry.englishWord[0] != '\0' && entry.englishWord[0] != '*') { // Пропускаем удалённые и пустые записи
  22.             insert(string(entry.englishWord.data()), position);
  23.         }
  24.         position += 1;
  25.     }
  26.     binaryFile.close();
  27. }
  28.  
  29. // Вставка элемента
  30. shared_ptr<TreeNode> BinarySearchTree::insertRecursively(shared_ptr<TreeNode> node, const string& key, long filePosition) {
  31.     if (!node) {
  32.         return make_shared<TreeNode>(key, filePosition);
  33.     }
  34.     if (key < node->key) {
  35.         node->left = insertRecursively(node->left, key, filePosition);
  36.     }
  37.     else if (key > node->key) {
  38.         node->right = insertRecursively(node->right, key, filePosition);
  39.     }
  40.     return node;
  41. }
  42.  
  43. void BinarySearchTree::insert(const string& key, long filePosition) {
  44.     root = insertRecursively(root, key, filePosition);
  45. }
  46.  
  47. // Поиск элемента
  48. void BinarySearchTree::search(const string& key) const {
  49.     shared_ptr<TreeNode> current = root;
  50.     while (current) {
  51.         if (key == current->key) {
  52.             cout << "Ключ найден: " << key << ", позиция в файле: " << current->filePosition << endl;
  53.             return;
  54.         }
  55.         else if (key < current->key) {
  56.             current = current->left;
  57.         }
  58.         else {
  59.             current = current->right;
  60.         }
  61.     }
  62.     cout << "Ключ не найден: " << key << endl;
  63. }
  64.  
  65. // Удаление элемента
  66. shared_ptr<TreeNode> BinarySearchTree::removeRecursively(shared_ptr<TreeNode> node, const string& key) {
  67.     if (!node) return node;
  68.  
  69.     if (key < node->key) {
  70.         node->left = removeRecursively(node->left, key);
  71.     }
  72.     else if (key > node->key) {
  73.         node->right = removeRecursively(node->right, key);
  74.     }
  75.     else {
  76.         if (!node->left) {
  77.             return node->right;
  78.         }
  79.         else if (!node->right) {
  80.             return node->left;
  81.         }
  82.  
  83.         node->key = findMinimum(node->right)->key;
  84.         node->right = removeRecursively(node->right, node->key);
  85.     }
  86.     return node;
  87. }
  88.  
  89. void BinarySearchTree::remove(const string& key) {
  90.     root = removeRecursively(root, key);
  91. }
  92.  
  93. shared_ptr<TreeNode> BinarySearchTree::findMinimum(shared_ptr<TreeNode> node) const {
  94.     shared_ptr<TreeNode> current = node;
  95.     while (current && current->left) {
  96.         current = current->left;
  97.     }
  98.     return current;
  99. }
  100.  
  101. // Вывод дерева
  102. void BinarySearchTree::printTree() const {
  103.     printTreeRecursively(root, 0);
  104. }
  105.  
  106. void BinarySearchTree::printTreeRecursively(shared_ptr<TreeNode> node, int space) const {
  107.     if (!node) return;
  108.  
  109.     space += 5;
  110.     printTreeRecursively(node->right, space);
  111.     cout << endl;
  112.     for (int i = 5; i < space; i++) {
  113.         cout << " ";
  114.     }
  115.     cout << node->key << "\n";
  116.     printTreeRecursively(node->left, space);
  117. }
  118.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement