Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100
PointerAlignment: Left
DerivePointerAlignment: false
ReflowComments: true
42 changes: 42 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: CI

on:
push:
branches: [ master ]
pull_request:

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Qt
run: sudo apt-get update && sudo apt-get install -y qtbase5-dev qttools5-dev-tools qtdeclarative5-dev
- name: Build application
run: |
qmake Trees.pro
make -j2

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install clang-format
run: sudo apt-get update && sudo apt-get install -y clang-format
- name: Verify formatting
run: clang-format --dry-run --Werror $(find . -name "*.h" -o -name "*.cpp")

tests:
runs-on: ubuntu-latest
env:
QT_QPA_PLATFORM: offscreen
steps:
- uses: actions/checkout@v4
- name: Install Qt and tools
run: sudo apt-get update && sudo apt-get install -y qtbase5-dev qttools5-dev-tools qtdeclarative5-dev clang-format
- name: Build tests
run: |
qmake tests/tests.pro -o tests/Makefile
make -C tests -j2
- name: Run tests
run: ./tests/trees_tests
68 changes: 34 additions & 34 deletions AVLTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
#define AVLTREE_H

#include <tree.h>

#include <algorithm>
#include <stack>

class AVLNode : public Node {
public:
public:
int key;
int h;
AVLNode *l;
AVLNode *r;
AVLNode* l;
AVLNode* r;

AVLNode() {
key = 0;
Expand All @@ -20,28 +21,28 @@ class AVLNode : public Node {
}
~AVLNode() {}

AVLNode *getL() override { return this->l; }
AVLNode *getR() override { return this->r; }
AVLNode* getL() override { return this->l; }
AVLNode* getR() override { return this->r; }
int getKey() override { return this->key; }
int getType() override { return TTAVL; }
};

class AVLTree : public Tree {
public:
public:
AVLTree() { root = nullptr; }
~AVLTree() {}
AVLNode *getRoot() override { return root; }
AVLNode* getRoot() override { return root; }
void add(int key) override { add(key, this->root); }
void drop(int key) override { drop(key, this->root); }
void clear() override {
clear(root);
root = nullptr;
}

private:
AVLNode *root;
private:
AVLNode* root;

void add(int key, AVLNode *&p) {
void add(int key, AVLNode*& p) {
if (p == nullptr) {
p = new AVLNode();
p->key = key;
Expand All @@ -61,7 +62,7 @@ class AVLTree : public Tree {
updateHeight(p);
}

void drop(int key, AVLNode *&p) {
void drop(int key, AVLNode*& p) {
if (p == nullptr) return;
if (p->key == key) {
if (p->l == nullptr && p->r == nullptr) {
Expand All @@ -77,7 +78,7 @@ class AVLTree : public Tree {
p->r = nullptr;
} else {
if (p->r->l == nullptr) {
AVLNode **q = &p->r;
AVLNode** q = &p->r;
std::swap((*q)->key, p->key);
if ((*q)->r != nullptr) {
(*q)->key = (*q)->r->key;
Expand All @@ -90,9 +91,9 @@ class AVLTree : public Tree {
int nb = needBalance(p);
if (nb) balance(p, nb);
} else {
std::stack<AVLNode *> s;
AVLNode *q = p->r;
AVLNode **z = nullptr;
std::stack<AVLNode*> s;
AVLNode* q = p->r;
AVLNode** z = nullptr;
while (q->l != nullptr) {
if (q->l->l == nullptr) {
z = &q->l;
Expand All @@ -111,7 +112,7 @@ class AVLTree : public Tree {
}
int nb;
while (!s.empty()) {
AVLNode *u = s.top();
AVLNode* u = s.top();
s.pop();
nb = needBalance(u);
if (nb) balance(u, nb);
Expand All @@ -130,14 +131,14 @@ class AVLTree : public Tree {
if (nb) balance(p, nb);
}

void clear(AVLNode *p) {
void clear(AVLNode* p) {
if (p == nullptr) return;
clear(p->l);
clear(p->r);
delete p;
}

int needBalance(AVLNode *elem) {
int needBalance(AVLNode* elem) {
if (elem == nullptr) throw -1;
if (elem->l == nullptr && elem->r == nullptr) {
return 0;
Expand Down Expand Up @@ -165,7 +166,7 @@ class AVLTree : public Tree {
}
}

void balance(AVLNode *&p, int troubleSide) {
void balance(AVLNode*& p, int troubleSide) {
if (troubleSide == -1) {
int k = compareHeigth(p->l);
if (k == -1) {
Expand All @@ -183,17 +184,16 @@ class AVLTree : public Tree {
}
}

void updateHeight(AVLNode *&p) {
void updateHeight(AVLNode*& p) {
if (p == nullptr) throw -1;
if (p->l == nullptr && p->r == nullptr) {
p->h = 1;
return;
}
p->h = std::max(p->l == nullptr ? 0 : p->l->h,
p->r == nullptr ? 0 : p->r->h) + 1;
p->h = std::max(p->l == nullptr ? 0 : p->l->h, p->r == nullptr ? 0 : p->r->h) + 1;
}

int compareHeigth(AVLNode *&p) {
int compareHeigth(AVLNode*& p) {
if (p == nullptr) throw -1;
if (p->l == nullptr && p->r == nullptr) {
return -1;
Expand All @@ -207,18 +207,18 @@ class AVLTree : public Tree {
return ((p->l->h) >= (p->r->h) ? -1 : 1);
}

void smallRightTurn(AVLNode *&p) {
AVLNode *q = p->l;
void smallRightTurn(AVLNode*& p) {
AVLNode* q = p->l;
p->l = q->r;
q->r = p;
updateHeight(p);
updateHeight(q);
p = q;
}

void bigRightTurn(AVLNode *&p) {
AVLNode *q = p->l;
AVLNode *r = q->r;
void bigRightTurn(AVLNode*& p) {
AVLNode* q = p->l;
AVLNode* r = q->r;
p->l = r->r;
q->r = r->l;
updateHeight(p);
Expand All @@ -229,18 +229,18 @@ class AVLTree : public Tree {
p = r;
}

void smallLeftTurn(AVLNode *&p) {
AVLNode *q = p->r;
void smallLeftTurn(AVLNode*& p) {
AVLNode* q = p->r;
p->r = q->l;
q->l = p;
updateHeight(p);
updateHeight(q);
p = q;
}

void bigLeftTurn(AVLNode *&p) {
AVLNode *q = p->r;
AVLNode *r = q->l;
void bigLeftTurn(AVLNode*& p) {
AVLNode* q = p->r;
AVLNode* r = q->l;
p->r = r->l;
q->l = r->r;
updateHeight(p);
Expand All @@ -252,4 +252,4 @@ class AVLTree : public Tree {
}
};

#endif // AVLTREE_H
#endif // AVLTREE_H
15 changes: 6 additions & 9 deletions Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@
#define CANVAS_H
#include <QGraphicsView>


class Canvas : public QGraphicsView{
class Canvas : public QGraphicsView {
Q_OBJECT
public:
explicit Canvas(int h, int w, QWidget *parent = 0);
protected:
public:
explicit Canvas(int h, int w, QWidget* parent = 0);

protected:
QGraphicsScene m_scene;
};




#endif // CANVAS_H
#endif // CANVAS_H
Loading
Loading