-
Notifications
You must be signed in to change notification settings - Fork 1
commit #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
commit #1
Changes from 1 commit
9a4ceb5
f732c61
df90803
a133817
1726d0e
94167fc
2198964
48f0ba6
f3e3f12
0f95f34
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| cmake_minimum_required(VERSION 3.24) | ||
| project(CourseProject) | ||
|
|
||
| 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) | ||
| target_link_libraries(CourseProject | ||
| Qt::Core | ||
| Qt::Gui | ||
| Qt::Widgets | ||
| ) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #include "Connection.h" | ||
|
|
||
| Connection::Connection(MainWindow *mainWindow) | ||
| : _view(mainWindow) | ||
|
||
| { | ||
| _controller.ptr = &_model; | ||
|
||
| _view.subscribe(_controller.input()); | ||
| _model.subscribe(_view.input()); | ||
| } | ||
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" | ||
|
|
||
|
||
| class Connection{ | ||
|
||
| public: | ||
| Connection(MainWindow* mainWindow); | ||
| //~Connection(); | ||
|
|
||
| private: | ||
| AVLTree _model; | ||
|
||
| View _view; | ||
| Controller _controller; | ||
| }; | ||
|
|
||
| #endif //COURSEPROJECT_CONNECTION_H | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| #include "Controller.h" | ||
|
|
||
| Controller::Controller(AVLTree* ptr) | ||
| :ptr(ptr) | ||
| { | ||
|
|
||
| } | ||
|
|
||
| void Controller::action(const View::SendData &data) { | ||
| const std::vector<int> vec = data.addValue; | ||
|
||
| if(vec.size() == 0) | ||
|
||
| return; | ||
| if(vec.back() == 0) | ||
|
||
| ptr->insert(vec[0]); | ||
|
||
| else | ||
| ptr->deleteNode(vec[0]); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| #ifndef COURSEPROJECT_CONTROLLER_H | ||
| #define COURSEPROJECT_CONTROLLER_H | ||
|
|
||
| #include <memory> | ||
|
|
||
| #include "Model.h" | ||
| #include "View.h" | ||
|
|
||
| class Controller { | ||
| public: | ||
| Controller() = default; | ||
|
||
| Controller(AVLTree* ptr); | ||
|
|
||
| ///what to do with data | ||
| void action(const View::SendData& data); | ||
|
||
|
|
||
| ///subscribe controller to view | ||
| NSLibrary::CObserver<View::SendData>* input() { return &observerAdd; } | ||
|
||
|
|
||
| ///public, but let it be like that | ||
| AVLTree* ptr; | ||
|
||
|
|
||
| private: | ||
| NSLibrary::CHotInput<View::SendData> observerAdd = [this](const View::SendData& data) | ||
| { action(data); }; | ||
| NSLibrary::CHotInput<View::SendData> observerDelete = [this](const View::SendData& data) | ||
| { action(data); }; | ||
|
||
| }; | ||
|
|
||
|
|
||
| #endif //COURSEPROJECT_CONTROLLER_H | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не могу найти Model.h файл. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,218 @@ | ||
| #include "Model.h" | ||
|
|
||
| int AVLTree::findMaxHeight(Node* left, Node* right) | ||
| { | ||
| int l = 0, r = 0; | ||
| if(left != nullptr) | ||
| l = left->height; | ||
|
||
| if(right != nullptr) | ||
| r = right->height; | ||
| return std::max(l, r); | ||
| } | ||
|
|
||
| int AVLTree::findBalanceFactor(Node* node) | ||
| { | ||
| int l = 0, r = 0; | ||
| if(node == nullptr) | ||
| return 0; | ||
| if(node->leftCh != nullptr) | ||
| l = node->leftCh->height; | ||
| if(node->rightCh != nullptr) | ||
| r = node->rightCh->height; | ||
| return l - r; | ||
| } | ||
|
|
||
| Node* AVLTree::rightRotate(Node* node) | ||
| { | ||
| Node* lCh = node->leftCh; | ||
| Node* rGrCh = node->leftCh->rightCh; | ||
| node->leftCh->rightCh = node; | ||
| node->leftCh = rGrCh; | ||
| node->height = findMaxHeight(node->leftCh, node->rightCh) + 1; | ||
| lCh->height = findMaxHeight(lCh->leftCh, lCh->rightCh) + 1; | ||
| return lCh; | ||
| } | ||
| Node* AVLTree::leftRotate(Node *node) | ||
| { | ||
| Node* rCh = node->rightCh; | ||
| Node* lGrCh = node->rightCh->leftCh; | ||
| node->rightCh->leftCh = node; | ||
| node->rightCh = lGrCh; | ||
| node->height = findMaxHeight(node->leftCh, node->rightCh) + 1; | ||
| rCh->height = findMaxHeight(rCh->leftCh, rCh->rightCh) + 1; | ||
| return rCh; | ||
| } | ||
|
|
||
|
|
||
| Node* AVLTree::insert(Node* node, int key) | ||
| { | ||
|
||
| ///find leaf node, where to insert newNode | ||
| if(node == nullptr) | ||
| return new Node(key); | ||
| if(key < node->key) | ||
| node->leftCh = insert(node->leftCh, key); | ||
| else if(key > node->key) | ||
| node->rightCh = insert(node->rightCh, key); | ||
| else | ||
| return node; | ||
|
|
||
| ///update height of ancestor | ||
| node->height = findMaxHeight(node->leftCh, node->rightCh) + 1; | ||
|
|
||
| ///find balance factor | ||
| int balanceFactor = findBalanceFactor(node); | ||
|
|
||
| ///check whether tree is unbalanced and balance it | ||
| if(balanceFactor > 1 && key < node->leftCh->key) | ||
| return rightRotate(node); | ||
| if(balanceFactor > 1 && key > node->leftCh->key) | ||
| { | ||
| node->leftCh = leftRotate(node->leftCh); | ||
| return rightRotate(node); | ||
| } | ||
| if(balanceFactor < -1 && key > node->rightCh->key) | ||
| return leftRotate(node); | ||
| if(balanceFactor < -1 && key < node->rightCh->key) | ||
| { | ||
| node->rightCh = rightRotate(node->rightCh); | ||
| return leftRotate(node); | ||
| } | ||
| return node; | ||
| } | ||
|
|
||
| void AVLTree::insert(int key) | ||
| { | ||
| int t = 1; | ||
| root_ = insert(root_, key); | ||
|
|
||
| if(root_== nullptr) | ||
| return; | ||
| insertPort.notify(); | ||
|
||
| } | ||
|
|
||
| Node* AVLTree::inorderSuccessor(Node* x) | ||
| { | ||
| Node* cur = x->rightCh; | ||
| while(cur->leftCh != nullptr) | ||
| cur = cur->leftCh; | ||
| return cur; | ||
| } | ||
|
|
||
| Node* AVLTree::deleteNode(Node *node, int key) | ||
| { | ||
| ///findNode, which should be deleted, and delete it | ||
| if(node == nullptr) | ||
| return node; | ||
| if(key < node->key) | ||
| node->leftCh = deleteNode(node->leftCh, key); | ||
| else if(key > node->key) | ||
| node->rightCh = deleteNode(node->rightCh, key); | ||
| else | ||
| { | ||
| ///one or zero children | ||
| if(node->leftCh == nullptr || node->rightCh == nullptr) | ||
| { | ||
| Node* cur = nullptr; | ||
| if(node->leftCh) | ||
| cur = node->leftCh; | ||
| if(node->rightCh) | ||
| cur = node->rightCh; | ||
| if(cur == nullptr) | ||
| { | ||
| cur = node; | ||
| node = nullptr; | ||
| } | ||
| else | ||
| *node = *cur; | ||
| delete cur; | ||
| } | ||
| ///two children | ||
| else | ||
| { | ||
| Node* cur = AVLTree::inorderSuccessor(node); | ||
| node->key = cur->key; | ||
| node->rightCh = deleteNode(node->rightCh, cur->key); | ||
| } | ||
| } | ||
|
|
||
| if(node == nullptr) | ||
| return node; | ||
| ///update height of the node | ||
| node->height = findMaxHeight(node->rightCh, node->leftCh) + 1; | ||
|
|
||
| ///find balance factor | ||
| int balanceFactor = findBalanceFactor(node); | ||
| ///check whether tree is unbalanced and balance it | ||
| if(balanceFactor > 1 && findBalanceFactor(node->leftCh) >= 0) | ||
| return rightRotate(node); | ||
| if(balanceFactor > 1 && findBalanceFactor(node->leftCh) < 0) | ||
| { | ||
| node->leftCh = leftRotate(node->leftCh); | ||
| return rightRotate(node); | ||
| } | ||
| if(balanceFactor < -1 && findBalanceFactor(node->rightCh) < 0) | ||
| return leftRotate(node); | ||
| if(balanceFactor < -1 && findBalanceFactor(node->rightCh) >= 0) | ||
| { | ||
| node->rightCh = rightRotate(node->rightCh); | ||
| return leftRotate(node); | ||
| } | ||
| return node; | ||
| } | ||
|
|
||
| void AVLTree::deleteNode(int key) | ||
| { | ||
| root_ = deleteNode(root_, key); | ||
| insertPort.notify(); | ||
| } | ||
|
|
||
| void AVLTree::clearHelp(Node*& node) | ||
|
||
| { | ||
| if (node != nullptr) | ||
| { | ||
| clearHelp(node->leftCh); | ||
| clearHelp(node->rightCh); | ||
| delete node; | ||
| node = nullptr; | ||
| } | ||
| } | ||
| AVLTree::~AVLTree() | ||
| { | ||
| clearHelp(root_); | ||
| } | ||
|
|
||
| //Node* AVLTree::search(Node *node, int key) | ||
| //{ | ||
| // if(node == nullptr) | ||
| // return node; | ||
| // if(key < node->key) | ||
| // node->leftCh = search(node->leftCh, key); | ||
| // else if(key > node->key) | ||
| // node->rightCh = search(node->rightCh, key); | ||
| // else | ||
| // return node; | ||
| //} | ||
|
|
||
| //Node* AVLTree::search(int key) | ||
| //{ | ||
| // return search(_root, key); | ||
| //} | ||
|
|
||
| //void AVLTree::levelPrint(Node* root) | ||
| //{ | ||
| // if (!root) | ||
| // return; | ||
| // std::queue<Node*> q; | ||
| // q.push(root); | ||
|
|
||
| // while (!q.empty()) | ||
| // { | ||
| // Node* cur = q.front(); | ||
| // q.pop(); | ||
| // std::cout << cur->key <<"\n"; | ||
| // if (cur->leftCh) | ||
| // q.push(cur->leftCh); | ||
| // if (cur->rightCh) | ||
| // q.push(cur->rightCh); | ||
| // } | ||
| //} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| #ifndef COURSEPROJECT_MODEL_H | ||
| #define COURSEPROJECT_MODEL_H | ||
|
|
||
| #include <iostream> | ||
| #include <vector> | ||
| #include "/Users/mayyaspirina/Documents/CourseProject/Observer/Observer.h" | ||
|
|
||
| class Node{ | ||
| public: | ||
|
|
||
| Node(){} | ||
| Node(int key) | ||
|
||
| : key(key), height(1), rightCh(nullptr), leftCh(nullptr) | ||
| {} | ||
| int key; | ||
| int height; | ||
| Node* leftCh; | ||
| Node* rightCh; | ||
| }; | ||
|
|
||
| class AVLTree{ | ||
| public: | ||
| AVLTree() | ||
|
||
| : root_(nullptr) | ||
| {} | ||
|
|
||
| void clearHelp(Node*& treeptr); | ||
| ~AVLTree(); | ||
|
||
|
|
||
| void insert(int key); | ||
|
||
| int findMaxHeight(Node* left, Node* right); | ||
| int findBalanceFactor(Node* node); | ||
| Node* rightRotate(Node* node); | ||
| Node* leftRotate(Node* node); | ||
| Node* insert(Node* node, int key); | ||
|
|
||
| Node* inorderSuccessor(Node* x); | ||
| void deleteNode(int key); | ||
| Node* deleteNode(Node* node, int key); | ||
| void levelPrint(Node* root); | ||
|
|
||
| Node* search(int key); | ||
| Node* search(Node* node, int key); | ||
|
|
||
|
|
||
| struct ModAddData{ | ||
| std::reference_wrapper<Node* const> Value; | ||
|
||
| }; | ||
| void subscribe(NSLibrary::CObserver<ModAddData>* observer) | ||
| { | ||
| assert(observer); | ||
| insertPort.subscribe(observer); | ||
| } | ||
| private: | ||
|
|
||
| // std::vector<Node*> root_; | ||
| Node* root_ = nullptr; | ||
| int Value; | ||
|
||
| NSLibrary::CObservableData<ModAddData> insertPort = ModAddData{std::cref(root_)}; | ||
|
||
| }; | ||
|
|
||
|
|
||
| #endif //COURSEPROJECT_MODEL_H | ||
Uh oh!
There was an error while loading. Please reload this page.