Skip to content
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ compile_commands.json
# QtCreator local machine specific files for imported projects
*creator.user*

*_qmlcache.qrc
*_qmlcache.qrc.DS_Store
.idea
.DS_Store
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.24)
project(CourseProject)

set(CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

set(CMAKE_PREFIX_PATH "/opt/homebrew/opt/qt/lib/cmake")

find_package(Qt6 COMPONENTS
Core
Gui
Widgets
REQUIRED)

add_executable(CourseProject main.cpp mainwindow.cpp mainwindow.ui Model.cpp View.cpp Controller.cpp Connection.cpp InfoTree.cpp aboutwindow.cpp)
target_link_libraries(CourseProject
Qt::Core
Qt::Gui
Qt::Widgets
)

8 changes: 8 additions & 0 deletions Connection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "Connection.h"

namespace mvc {
Application::Application() {
view_.subscribe(controller_.input());
model_.subscribe(view_.input());
}
}
17 changes: 17 additions & 0 deletions Connection.h
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Переименуй файл в Application

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef COURSEPROJECT_CONNECTION_H
#define COURSEPROJECT_CONNECTION_H

#include "Controller.h"

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Помести весь свой проект в namespace. Не пиши ничего в global namespace.

namespace mvc {
class Application {
public:
Application();

private:
AVLTree model_;
View view_;
Controller controller_{&model_};
};
}
#endif //COURSEPROJECT_CONNECTION_H
22 changes: 22 additions & 0 deletions Controller.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "Controller.h"

namespace mvc {
Controller::Controller(AVLTree *model)
: model_(model) {
}

void Controller::action(const ViewData &data) {
if(data.operation == View::Operation::Add)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы сделал switch по enum-у

model_->insert(data.value);
else if(data.operation == View::Operation::Delete)
model_->deleteNode(data.value);
else if(data.operation == View::Operation::Search)
model_->search(data.value);
else if(data.operation == View::Operation::Traversal && data.type == View::Type::InOrder)
model_->inOrder();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

А что это за команда у дерева? Это чисто для визуацилации или это какой-то запрос у дерева, который оно выполняет?

else if(data.operation == View::Operation::Traversal && data.type == View::Type::PreOrder)
model_->preOrder();
else
model_->postOrder();
}
}
28 changes: 28 additions & 0 deletions Controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef COURSEPROJECT_CONTROLLER_H
#define COURSEPROJECT_CONTROLLER_H

#include <memory>

#include "Model.h"
#include "View.h"

namespace mvc {
class Controller {
private:
using ViewData = View::Command;
using Observer = NSLibrary::CObserver<ViewData >;
using Input = NSLibrary::CColdInput<ViewData >;

public:
Controller(AVLTree *ptr);
Observer *input() { return &observer_; }

private:
void action(const ViewData& data);

AVLTree *model_;
Input observer_ = [this](const ViewData &data) { action(data); };
};
}

#endif //COURSEPROJECT_CONTROLLER_H
124 changes: 124 additions & 0 deletions InfoTree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#include "InfoTree.h"

namespace mvc {
InfoTree::~InfoTree() {
clear(root_);
}

std::pair<int, bool> InfoTree::findValue(int x, int y) {
if(this == nullptr)
return {0, false};
std::queue<Info *> que;
que.push(root_);
Info *cur;
while (!que.empty()) {
cur = que.front();
que.pop();
if((x > cur->x - RADIUS && x < cur->x + RADIUS) && (y > cur->y - RADIUS && y < cur->y + RADIUS))
return {cur->key, true};
if (cur->left != nullptr)
que.push(cur->left);
if (cur->right != nullptr)
que.push(cur->right);
}
return {0, false};
}

void InfoTree::clear(Info *treeInfo) {
if (treeInfo != nullptr) {
clear(treeInfo->left);
clear(treeInfo->right);
delete treeInfo;
}
}

void InfoTree::setWidth(Info *cur) {
if (cur == nullptr)
return;
setWidth(cur->left);
setWidth(cur->right);

if (cur->right == nullptr && cur->left == nullptr)
cur->width = 2 * RADIUS;
else if (cur->left == nullptr)
cur->width = 2 * cur->right->width + WIDTH;
else if (cur->right == nullptr)
cur->width = 2 * cur->left->width + WIDTH;
else
cur->width = 2 * std::max(cur->left->width, cur->right->width) + WIDTH;
}

void InfoTree::calcXCoord() {
Info *coord = root_;
setWidth(coord);
Info *root = root_;
if (root == nullptr)
return;
std::queue<Info *> que;
que.push(root);
Info *cur;
int count = 0, timesNow = 1, timesNext = 0;
while (!que.empty()) {
cur = que.front();
que.pop();
if (cur->left != nullptr) {
que.push(cur->left);
++timesNext;
cur->left->x = cur->x - WIDTH / 2 - cur->left->width / 2;
}
if (cur->right != nullptr) {
que.push(cur->right);
++timesNext;
cur->right->x = cur->x + WIDTH / 2 + cur->right->width / 2;
}
--timesNow;
if (timesNow == 0) {
++count;
timesNow = timesNext;
timesNext = 0;
}
}
}

Info *InfoTree::copy(const Node *node) {
if (node == nullptr)
return nullptr;
else {
Info *temp = new Info;
temp->key = node->key;
temp->left = copy(node->leftCh);
temp->right = copy(node->rightCh);
return temp;
}
}

void InfoTree::calcYCoord(const Node *rootGet) {
root_ = copy(rootGet);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Вынеси эту строчку в список инициализации в конструкторе

InfoTree::InfoTree() : root_(copy(rootGet)) {
  calcYCoord();
  calcXCoord();
}

Дело в том, что эта функция копирования не имеет отношения к вычислению координат, она строит дерево. Ее еще стоит переименовать в buildInfoTree или что-то такое.

Info *root = root_;
if (root == nullptr)
return;
std::queue<Info *> que;
que.push(root);
Comment on lines +121 to +122
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Это ты от рекурсии избавлялась?

Info *cur;
int count = 0, timesNow = 1, timesNext = 0;
while (!que.empty()) {
cur = que.front();
que.pop();
cur->y = count * (2 * RADIUS + HEIGHT);
if (cur->left != nullptr) {
que.push(cur->left);
++timesNext;
}
if (cur->right != nullptr) {
que.push(cur->right);
++timesNext;
}
--timesNow;
if (timesNow == 0) {
++count;
timesNow = timesNext;
timesNext = 0;
}
}
}
}
43 changes: 43 additions & 0 deletions InfoTree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#ifndef COURSEPROJECT_INFOTREE_H
#define COURSEPROJECT_INFOTREE_H

#include "Model.h"
#include <queue>

namespace mvc {

struct Info{
int x = 0;
int y = 0;
int key;
Info* left;
Info* right;
int width;
};

class InfoTree {
public:
InfoTree(const Node* rootGet){
calcYCoord(rootGet);
calcXCoord();
}
~InfoTree();
Info* getRoot() { return root_; }
std::pair<int, bool> findValue(int x, int y);

private:
void clear(Info* treeInfo);
void setWidth(Info* cur);
void calcXCoord();
Info* copy(const Node* node);
void calcYCoord(const Node* node);

static constexpr int RADIUS = 40;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не пиши только большими буквами, так обычно пишут макросы, если нужны константы, то добавь k в начале, например kRadius или k_radius в зависимости от стиля. И можно _ в конце имени для единообразия.

static constexpr int HEIGHT = 30;
static constexpr int WIDTH = 15;
Info* root_ = nullptr;
};

}

#endif//COURSEPROJECT_INFOTREE_H
Loading