Advertisement
DaniDori

BinarySearchTree.h

Nov 2nd, 2023
584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #pragma once
  2. #define _CRT_SECURE_NO_WARNINGS
  3. #include "BinaryFileManagement.h"
  4. #include <memory>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. struct TreeNode {
  9.     string key;
  10.     long filePosition; // Позиция записи в файле
  11.     shared_ptr<TreeNode> left; // Используем умные указатели
  12.     shared_ptr<TreeNode> right;
  13.  
  14.     TreeNode(const string& key, long filePosition)
  15.         : key(key), filePosition(filePosition), left(nullptr), right(nullptr) {}
  16. };
  17.  
  18. class BinarySearchTree {
  19. public:
  20.     BinarySearchTree(const string& fileName);
  21.  
  22.     void insert(const string& key, long filePosition);
  23.     void remove(const string& key);
  24.     void search(const string& key) const;
  25.     void printTree() const;
  26.  
  27.     // Вспомогательные функции
  28.     shared_ptr<TreeNode> insertRecursively(shared_ptr<TreeNode> node, const string& key, long filePosition);
  29.     shared_ptr<TreeNode> removeRecursively(shared_ptr<TreeNode> node, const string& key);
  30.     shared_ptr<TreeNode> findMinimum(shared_ptr<TreeNode> node) const;
  31.     void printTreeRecursively(shared_ptr<TreeNode> node, int space) const;
  32.     void buildTreeFromFile();
  33.  
  34. private:
  35.     shared_ptr<TreeNode> root;
  36.     string fileName; // Имя файла, из которого строится дерево
  37. };
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement