feat: files from mails

This commit is contained in:
PolishPigeon 2022-06-22 22:29:10 +02:00
parent c3f82ca20e
commit e0b2c05d3f
459 changed files with 1546 additions and 0 deletions

57
EADS/MidTerm/task1.cpp Normal file
View File

@ -0,0 +1,57 @@
// KRZYSZTOF RUDNICKI, 307585, task 1
class List
{
private:
struct Node{
double key;
Node* next;
};
Node* head;
public:
int insert(double newVal, double firstVal, double secondVal);
};
int List::insert(double newVal, double firstVal, double secondVal)
{
Node* curr = head;
if(head == nullptr) return 0; // we can't insert anything inside empty list
if(curr->next == nullptr) return 0; // we can't insert a node between two nodes if there is only one node
if(curr->next->next == nullptr) // If there is exactly two nodes
{
if(head -> key == firstVal && head -> next -> key == secondVal)
{
Node* newNode = new Node();
newNode -> key = newVal;
newNode -> next = head -> next;
head -> next = newNode;
return 1;
}else return 0;
}
int insertions = 0;
while(curr -> next != nullptr) // If there is three nodes or more
{
bool foundKeys = 0;
if(curr -> key == firstVal && curr -> next -> key == secondVal)
{
Node* newNode = new Node();
newNode -> key = newVal;
newNode -> next = curr->next;
curr -> next = newNode;
insertions++;
foundKeys = 1;
curr = newNode -> next; // If newVal == firstVal, then without this line we will have
// infintie loop, to counteract this we skip newly added node.
}
if(!foundKeys) curr = curr -> next;
}
return insertions;
}
int main()
{
return 0;
}

55
EADS/MidTerm/task2.cpp Normal file
View File

@ -0,0 +1,55 @@
// Krzysztof Rudnicki, 307585
struct treeNode
{
int key;
treeNode *left;
treeNode *right;
};
class Tree{
private:
treeNode *root;
public:
bool checkKeys(int givenKey);
bool findNode(treeNode*& nodeToFound, int givenKey);
int sumNodeSub(treeNode* start);
};
int Tree::sumNodeSub(const treeNode* start)
{
return (sumNodeSub(start->right)
+ sumNodeSub(start->left)
+ start->key);
}
bool Tree::findNode(treeNode*& nodeToFound, int givenKey);
{
treeNode* curr = root;
treeNode* Cleft = curr->left;
treeNode* Cright = curr->right;
while(curr != nullptr)
{
if(curr -> key == givenKey)
{
nodeToFound = curr;
return 1;
}else
{
if(Cleft.findNode(nodeToFound, givenKey)) return 1;
if(Cright.findNode(nodeToFound, givenKey)) return 1;
}
}
return 0;
}
bool Tree::checkKeys(int key)
{
treeNode* start;
if (!findNode(start, key)) return 0;
// if we did not find the node we cannot compare the keys of it's left and right subtrees
return (sumNodeSub(start->left) > sumNodeSub(start->right));
}

62
EADS/MidTerm/task3.cpp Normal file
View File

@ -0,0 +1,62 @@
// Krzysztof Rudnicki, 307585, task 3
template<typename Key>
class Ring{
private:
struct Node{
Key key;
Node* prev;
Node* next;
};
Node* any;
public:
bool removeNeighbours(const Key& givenKey);
};
template<typename Key>
bool Ring<Key>::removeNeighbours(const Key& givenKey)
{
Node* curr = any;
if(any == nullptr || any -> next == nullptr) return 0;
// we can't remove 2 nodes if there is less than 2 nodes
bool foundKey = 0;
// we are interested in 5 nodes, lets call them, from left to right:
// A; B; C; D; E
// I will use this for comments
// if C has a key then at the end we want to get:
// A; C; E
do{
if(curr -> key == givenKey)
{
Node* temp = curr;
curr -> prev -> prev -> next = curr;
// curr -> prev -> prev points to Node "A"
// it used to point "next" to Node "B" which we are going to remove so it must
// start to point to a new node in our case node "C"
curr -> next -> next -> prev = curr;
// curr -> next -> next points to Node "E"
// it used to point "prev" to Node "D" which we are going to remove so it must
// start to point to a new node in our case node "C"
curr -> next = curr -> next -> next;
// curr points to Node "C"
// it used to point "next" to Node "D" which we are going to remove so it must
// start to point to a new node in our case node "E"
curr -> prev = curr -> prev -> prev;
// curr points to Node "C"
// it used to point "prev" to Node "B" which we are going to remove so it must
// start to point to a new node in our case node "A"
if(curr->prev == any || curr -> next == any) any = curr;
// if any of the neighours that we are going to delete is "any", we must provide
// the ring with a new "any" node
delete temp->prev;
// this way we will delete what USED to be curr->prev
// temp->prev points to "B" node
delete temp->next;
// this way we will delete what USED to be curr->next
// temp->next points to "D" node
foundKey = 1;
}
curr = curr->next;
}while(curr != any);
return foundKey;
}

355
EADS/lab1.cpp Normal file
View File

@ -0,0 +1,355 @@
#include <iostream>
#include <vector>
template<typename Key, typename Info>
class Sequence{
private:
struct Node{
Key key;
Info info;
Node* next;
};
Node* head;
public:
Sequence();
void destroySequence();
~Sequence();
void copySequence(const Sequence<Key, Info>& otherSequence);
Sequence(const Sequence<Key, Info>& otherSequence);
const Sequence<Key, Info>& operator=(const Sequence<Key, Info>& otherSequence);
int length() const;
void print() const;
bool isEmpty() const;
bool getKeyandInfoAt(const int position, Key& keyToReturn, Info& infoToReturn) const;
void insertAtEnd(const Key& newKey, const Info& newInfo);
};
template<typename Key, typename Info>
Sequence<Key, Info>::Sequence()
{
head = nullptr;
}
template<typename Key, typename Info>
void Sequence<Key, Info>::destroySequence()
{
Node* curr;
while(head != nullptr)
{
curr = head;
head = head->next;
delete curr;
}
}
template<typename Key, typename Info>
Sequence<Key, Info>::~Sequence()
{
destroySequence();
}
template<typename Key, typename Info>
void Sequence<Key, Info>::copySequence(const Sequence<Key, Info>& otherSequence)
{
if(head != nullptr) destroySequence();
if(otherSequence.head == nullptr) head = nullptr;
else
{
Node* curr = otherSequence.head;
head = new Node();
head -> key = curr -> key;
head -> info = curr -> info;
head -> next = nullptr;
curr = curr -> next;
Node* nextNode;
while(curr != nullptr)
{
nextNode = new Node();
nextNode->key = curr->key;
nextNode->info = curr->info;
nextNode->next = nullptr;
curr = curr -> next;
}
}
}
template<typename Key, typename Info>
Sequence<Key, Info>::Sequence(const Sequence<Key, Info>& otherSequence)
{
head = nullptr;
copySequence(otherSequence);
}
template<typename Key, typename Info>
const Sequence<Key, Info>& Sequence<Key, Info>::operator=(const Sequence<Key, Info>& otherSequence)
{
if(this != &otherSequence)
{
copySequence(otherSequence);
}
return *this;
}
template<typename Key, typename Info>
int Sequence<Key, Info>::length() const
{
Node* curr = head;
int length = 0;
while(curr != nullptr)
{
curr = curr->next;
length++;
}
return length;
}
template<typename Key, typename Info>
void Sequence<Key, Info>::print() const
{
Node* curr = head;
int position = 0;
if(head == nullptr) std::cout << "Sequence is empty" << std::endl;
while(curr != nullptr)
{
std::cout << "Inspecting element number: [" << position << "]" << std::endl;
std::cout << "Key value is: [" << curr -> key << "]" << std::endl;
std::cout << "Info value is: [" << curr -> info << "]" << std::endl;
curr = curr -> next;
position++;
}
}
template<typename Key, typename Info>
bool Sequence<Key, Info>::isEmpty() const
{
return length() == 0;
}
template<typename Key, typename Info>
bool Sequence<Key, Info>::getKeyandInfoAt(const int position, Key& keyToReturn, Info& infoToReturn) const
{
Node* curr = head;
int currentPosition = 0;
while(curr != nullptr)
{
if(currentPosition == position)
{
infoToReturn = curr -> info;
keyToReturn = curr -> key;
return 1;
}
curr = curr -> next;
currentPosition++;
}
return 0;
}
template<typename Key, typename Info>
void Sequence<Key, Info>::insertAtEnd(const Key& newKey, const Info& newInfo)
{
Node* newNode = new Node();
newNode -> key = newKey;
newNode -> info = newInfo;
newNode -> next = nullptr;
if(head == nullptr) head = newNode;
else
{
Node* curr = head;
while(curr->next != nullptr) curr = curr->next;
curr -> next = newNode;
}
}
template<typename Key, typename Info>
bool addSingleSequence(const Sequence <Key, Info>& seq, int start, int dl, int limit, Sequence <Key, Info>& outputSequence)
{
int elementsFromFirstSequence = start + dl;
int lengthOfFirstSequence = seq.length();
int outputSize = outputSequence.length();
bool maxSize = 0;
for(int i = start; i <= elementsFromFirstSequence && i <= lengthOfFirstSequence; i++)
{
Key newKey;
Info newInfo;
if(seq.getKeyandInfoAt(i, newKey, newInfo))
{
outputSequence.insertAtEnd(newKey, newInfo);
outputSize++;
if(outputSize >= limit)
{
maxSize = 1;
break;
}
}
}
return maxSize;
}
template<typename Key, typename Info>
Sequence<Key, Info> produce(
const Sequence <Key, Info>& seq1, int start1, int dl1,
const Sequence <Key, Info>& seq2, int start2, int dl2,
int limit)
{
Sequence<Key, Info> newSequence;
bool maxSize = addSingleSequence(seq1, start1, dl1, limit, newSequence);
if(maxSize) return newSequence;
else addSingleSequence(seq2, start2, dl2, limit, newSequence);
return newSequence;
}
bool testingTwoEmpty()
{
Sequence<int, int> empty1;
Sequence<int, int> empty2;
Sequence<int, int> outputSequence = produce(empty1, 0, 1, empty2, 0, 1, 5);
if(!outputSequence.isEmpty())
{
outputSequence.print();
std::cerr << "testingTwoEmpty()" << std::endl;
return 0;
}
return 1;
}
bool testingOneEmpty()
{
Sequence<int, int> empty1;
Sequence<int, int> notEmpty;
notEmpty.insertAtEnd(1, 2);
notEmpty.insertAtEnd(3, 4);
notEmpty.insertAtEnd(5, 6);
Sequence<int, int> outputSequence = produce(empty1, 0, 1, notEmpty, 0, 4, 5);
std::vector <int> keys(3);
std::vector <int> infos(3);
outputSequence.getKeyandInfoAt(0, keys.at(0), infos.at(0));
outputSequence.getKeyandInfoAt(1, keys.at(1), infos.at(1));
outputSequence.getKeyandInfoAt(2, keys.at(2), infos.at(2));
if(keys.at(0) != 1 || keys.at(1) != 3 || keys.at(2) != 5 ||
infos.at(0) != 2 || infos.at(1) != 4 || infos.at(2) != 6)
{
outputSequence.print();
std::cerr << "testingOneEmpty()" << std::endl;
return 0;
}
return 1;
}
bool testingFirstOverLimit()
{
Sequence<int, int> overLimit;
Sequence<int, int> shouldntMatter;
overLimit.insertAtEnd(1, 2);
overLimit.insertAtEnd(3, 4);
overLimit.insertAtEnd(5, 6);
overLimit.insertAtEnd(7, 9);
overLimit.insertAtEnd(9, 10);
shouldntMatter.insertAtEnd(100, 200);
Sequence<int, int> outputSequence = produce(overLimit, 0, 5, shouldntMatter, 0, 1, 4);
if(outputSequence.length() != 4)
{
outputSequence.print();
std::cerr << "testingFirstOverLimit()" << std::endl;
return 0;
}
return 1;
}
bool testingSecondOverLimit()
{
Sequence<int, int> empty;
Sequence<int, int> overLimit;
overLimit.insertAtEnd(1, 2);
overLimit.insertAtEnd(3, 4);
overLimit.insertAtEnd(5, 6);
overLimit.insertAtEnd(7, 9);
overLimit.insertAtEnd(9, 10);
Sequence<int, int> outputSequence = produce(empty, 0, 2, overLimit, 0, 5, 4);
if(outputSequence.length() != 4)
{
outputSequence.print();
std::cerr << "testingSecondOverLimit()" << std::endl;
return 0;
}
return 1;
}
bool testingTooBigStart()
{
Sequence<int, int> tooSmall;
Sequence<int, int> shouldntMatter;
tooSmall.insertAtEnd(1, 2);
tooSmall.insertAtEnd(3, 4);
tooSmall.insertAtEnd(5, 6);
Sequence<int, int> outputSequence = produce(tooSmall, 4, 5, shouldntMatter, 1, 0, 5);
if(!outputSequence.isEmpty())
{
outputSequence.print();
std::cerr << "testingTooBigStart()" << std::endl;
return 0;
}
return 1;
}
bool testingTooBigLength()
{
Sequence<int, int> tooShort;
Sequence<int, int> shouldntMatter;
tooShort.insertAtEnd(1, 2);
tooShort.insertAtEnd(3, 4);
tooShort.insertAtEnd(5, 6);
Sequence<int, int> outputSequence = produce(tooShort, 0, 6, shouldntMatter, 1, 0, 7);
if(outputSequence.length() != 3)
{
outputSequence.print();
std::cerr << "testingTooBigLength()" << std::endl;
return 0;
}
return 1;
}
bool testingNormal()
{
Sequence<int, int> normal1;
Sequence<int, int> normal2;
normal1.insertAtEnd(1, 2);
normal1.insertAtEnd(3, 4);
normal2.insertAtEnd(5, 6);
Sequence<int, int> outputSequence = produce(normal1, 0, 2, normal2, 0, 1, 3);
std::vector <int> keys(3);
std::vector <int> infos(3);
outputSequence.getKeyandInfoAt(0, keys.at(0), infos.at(0));
outputSequence.getKeyandInfoAt(1, keys.at(1), infos.at(1));
outputSequence.getKeyandInfoAt(2, keys.at(2), infos.at(2));
if(keys.at(0) != 1 || keys.at(1) != 3 || keys.at(2) != 5 ||
infos.at(0) != 2 || infos.at(1) != 4 || infos.at(2) != 6)
{
outputSequence.print();
std::cerr << "testingNormal()" << std::endl;
return 0;
}
return 1;
}
bool tests()
{
return testingTwoEmpty()&&testingOneEmpty()&&testingFirstOverLimit()&&
testingSecondOverLimit()&&testingTooBigStart()&&testingTooBigLength()&&testingNormal();
}
int main()
{
std::cout << "Result of tests: " << tests() << std::endl;
return 0;
}

359
EADS/lab2.cpp Normal file
View File

@ -0,0 +1,359 @@
// Krzysztof Rudnicki, 307585, EADS 2
#include <iostream>
void print(const std::string s)
{
std::cout << s << std::endl;
}
template<typename Key, typename Info>
class Ring{
private:
struct Node{
Key key;
Info info;
Node* next;
Node* prev;
};
Node* any;
public:
class Iterator
{
private:
Node* pNode;
public:
Iterator() { pNode = nullptr; }
~Iterator() { pNode = nullptr; }
Iterator(Node* ptr) : pNode(ptr) {}
bool operator!=(const Iterator& other) const
{
return pNode != other.pNode;
}
Iterator& operator++()
{
pNode = pNode -> next;
return *this;
}
Iterator operator++(int)
{
Iterator it = *this;
pNode = pNode -> next;
return it;
}
Iterator& operator--()
{
pNode = pNode -> prev;
return *this;
}
Iterator operator--(int)
{
Iterator it = *this;
pNode = pNode -> prev;
return it;
}
Node& operator[](int index)
{
Node* indexedNode = pNode;
for(int i = 0; i < index; i++)
{
indexedNode = indexedNode -> next;
}
return *indexedNode;
}
Node* operator->()
{
return pNode;
}
Node& operator*()
{
return *pNode;
}
bool operator==(const Iterator& other) const
{
return pNode == other.pNode;
}
};
Ring();
~Ring();
void clearRing();
void print() const;
void addAtEnd(Key newKey, Info newInfo);
int size() const;
Iterator begin() const // I will treat "any" Node as a "begining" of a ring
{
return Iterator(any);
}
Iterator end() const // I will treat a Node just before "any" node as an "end" of a ring
{
if(any == nullptr) return nullptr;
return any -> prev;
}
};
template<typename Key, typename Info>
Ring<Key, Info>::Ring()
{
any = nullptr;
}
template<typename Key, typename Info>
void Ring<Key, Info>::clearRing()
{
if(any == nullptr) return;
Node* curr = any;
Node* temp = any;
do{
temp = curr;
curr = curr->next;
delete temp;
}while(curr != any);
delete any;
}
template<typename Key, typename Info>
Ring<Key, Info>::~Ring()
{
this -> clearRing();
}
template<typename Key, typename Info>
void Ring<Key, Info>::print() const
{
Node* curr = any;
do{
std::cout << curr -> key << "; " << curr->info << std::endl;
curr = curr->next;
}while (curr != any);
}
template<typename Key, typename Info>
void Ring<Key, Info>::addAtEnd(Key newKey, Info newInfo)
{
Node* newNode = new Node();
newNode -> key = newKey;
newNode -> info = newInfo;
if(any == nullptr)
{
any = newNode;
any -> next = any;
any -> prev = any;
return;
}
if(any -> next == nullptr)
{
any -> next = newNode;
any -> prev = newNode;
newNode -> next = any;
newNode -> prev = any;
return;
}
Node* curr = any;
while(curr -> next != any) curr = curr -> next;
any->prev = newNode;
curr->next = newNode;
newNode -> prev = curr;
newNode -> next = any;
}
template<typename Key, typename Info>
int Ring<Key, Info>::size() const
{
Node* curr = any;
int size = 0;
do{
size++;
curr = curr -> next;
}while (curr != any);
return size;
}
template <typename Key, typename Info>
void printPair(const Key key, const Info info)
{
std::cout << "Key: " << key << "; Info: " << info << std::endl;
}
bool testForEmpty()
{
Ring<int, int> marcel;
Ring<int, int>::Iterator it = marcel.begin();
if(it != nullptr)
{
print("testForEmpty");
return false;
}
return true;
}
bool testOneElement()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
Ring<int, int>::Iterator it = marcel.begin();
if(it -> key != 1 || it -> info != 1)
{
print("testForwardIncrementingOneElement");
return false;
}
return true;
}
bool testForwardIncrementing()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
marcel.addAtEnd(2, 2);
marcel.addAtEnd(3, 3);
marcel.addAtEnd(4, 4);
marcel.addAtEnd(5, 5);
int i = 0;
for( Ring<int, int>::Iterator it = marcel.begin();
it != marcel.end(); it++, i++)
{
if(it -> key != i + 1 || it -> info != i + 1)
{
print("testForwardIncrementingNormal");
return false;
}
}
if(marcel.end() -> key != 5 || marcel.end() -> info != 5)
{
print("testForwardIncrementingNormal");
return false;
}
return true;
}
bool testBackwardDecrementing()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
marcel.addAtEnd(2, 2);
marcel.addAtEnd(3, 3);
marcel.addAtEnd(4, 4);
marcel.addAtEnd(5, 5);
int i = 0;
for( Ring<int, int>::Iterator it = marcel.end();
it != marcel.begin(); it++, i++)
{
if(it -> key != 5 - i || it -> info != 5 - i)
{
print("testBackwardDecrementing");
return false;
}
}
if(marcel.begin() -> key != 1 || marcel.begin() -> info != 1)
{
print("testBackwardDecrementing");
return false;
}
return true;
}
bool testForwardSquareBracket()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
marcel.addAtEnd(2, 2);
marcel.addAtEnd(3, 3);
marcel.addAtEnd(4, 4);
marcel.addAtEnd(5, 5);
Ring<int, int>::Iterator it = marcel.begin();
for(int i = 0; i < marcel.size(); i++)
{
if(it[i].key != i + 1 || it[i].info != i + 1)
{
print("testForwardSquareBracket");
return false;
}
}
return true;
}
bool testBackwardSquareBracket()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
marcel.addAtEnd(2, 2);
marcel.addAtEnd(3, 3);
marcel.addAtEnd(4, 4);
marcel.addAtEnd(5, 5);
Ring<int, int>::Iterator it = marcel.end();
for(int i = marcel.size(); i > 0; i--)
{
if(it[i].key != i || it[i].info != i)
{
print("testBackwardSquareBracket");
return false;
}
}
return true;
}
bool testSquareBracketRandomOverflow()
{
Ring<int, int> marcel;
marcel.addAtEnd(1, 1);
marcel.addAtEnd(2, 2);
marcel.addAtEnd(3, 3);
marcel.addAtEnd(4, 4);
marcel.addAtEnd(5, 5);
Ring<int, int>::Iterator it = marcel.begin();
//Checking for some random places in ring:
if(it[2].key != 3)
{
print("testSquareBracketRandomOverflow");
return false;
}
if(it[4].key != 5)
{
print("testSquareBracketRandomOverflow");
return false;
}
if(it[0].key != 1)
{
print("testSquareBracketRandomOverflow");
return false;
}
//Checking for overflow:
if(it[10].key != 1)
{
print("testSquareBracketRandomOverflow");
return false;
}
return true;
}
bool tests()
{
return testForEmpty()&&testOneElement()&&testForwardIncrementing()
&&testBackwardDecrementing()&&testForwardSquareBracket()&&testBackwardSquareBracket()
&&testSquareBracketRandomOverflow();
}
int main()
{
std::cout << "Result of tests is: " << tests() << std::endl;
return 1;
}

540
EADS/lab3.cpp Normal file
View File

@ -0,0 +1,540 @@
// Krzysztof Rudnicki, 307585, lab3 EADS
#include <iostream>
#include <string>
template<typename Key, typename Info>
struct Node{
Key key;
Info info;
int balanceFactor;
Node<Key, Info>* left;
Node<Key, Info>* right;
};
template<typename Key, typename Info>
class Dictionary{
private:
Node<Key, Info>* root;
public:
Dictionary();
Node<Key, Info>* getRoot() const;
void destroy(Node<Key, Info>* &someNode);
~Dictionary();
void copyDictionary(Dictionary *&copiedDictRoot, Dictionary *otherTreeRoot);
Dictionary(const Dictionary &other);
void insert(const Key &newKey, const Info &newInfo);
void remove(const Key &keyToRemove);
Info& search(const Key &keyToFind) const;
void print() const;
void insertAndPrint(const Key &newKey, const Info &newInfo);
Dictionary& operator=(const Dictionary& otherDictionary);
};
void print(std::string s)
{
std::cout << s << std::endl;
}
template<typename Key, typename Info>
Dictionary<Key, Info>::Dictionary()
{
root = nullptr;
}
template<typename Key, typename Info>
Node<Key, Info>* Dictionary<Key, Info>::getRoot() const
{
return root;
}
template<typename Key, typename Info>
void Dictionary<Key, Info>::destroy(Node<Key, Info>* &someNode)
{
if( someNode != nullptr)
{
destroy(someNode -> left);
destroy(someNode -> right);
delete someNode;
someNode = nullptr;
}
}
template<typename Key, typename Info>
Dictionary<Key, Info>::~Dictionary() { destroy(root); }
template<typename Key, typename Info>
void Dictionary<Key, Info>::copyDictionary(Dictionary* &copiedDictRoot, Dictionary* otherDictRoot)
{
if(otherDictRoot == nullptr) copiedDictRoot = nullptr;
else
{
copiedDictRoot = new Dictionary;
copiedDictRoot->info = otherDictRoot -> info;
copyDictionary(copiedDictRoot -> left, otherDictRoot -> left);
copyDictionary(copiedDictRoot -> right, otherDictRoot -> right);
}
}
template<typename Key, typename Info>
Dictionary<Key, Info>::Dictionary(const Dictionary &other)
{
if(other.root == nullptr) root = nullptr;
else copyTree(root, other.root);
}
template<typename Key, typename Info>
Dictionary<Key, Info>& Dictionary<Key, Info>::operator=(const Dictionary& otherDictionary)
{
if(this != &otherDictionary)
{
if(root != nullptr) destroy(root);
if(otherDictionary.root == nullptr) root = nullptr;
else copyDictionary(root, otherDictionary.root);
}
return *this;
}
template<typename Key, typename Info>
void rotateToLeft(Node<Key, Info>* &root)
{
Node<Key, Info>* p;
if (root == nullptr) std::cerr << "Error in the tree" << std::endl;
else if (root->right == nullptr)
std::cerr << "Error in the tree:"
<<" No right subtree to rotate." << std::endl;
else
{
p = root->right;
root->right = p->left;
p->left = root;
root = p;
}
}
template<typename Key, typename Info>
void rotateToRight(Node<Key, Info>* &root)
{
Node<Key, Info> *p;
if (root == NULL) std::cerr << "Error in the tree" << std::endl;
else if (root-> left == NULL)
std::cerr << "Error in the tree:"
<< " No left subtree to rotate." << std::endl;
else
{
p = root->left;
root->left = p->right;
p->right = root;
root = p;
}
}
template<typename Key, typename Info>
void balanceFromLeft(Node<Key, Info>* &root)
{
Node<Key, Info> *p;
Node<Key, Info> *w;
p = root->left;
switch (p->balanceFactor)
{
case -1:
root->balanceFactor = 0;
p->balanceFactor = 0;
rotateToRight(root);
break;
case 0:
std::cerr << "Error: Cannot balance from the left." << std::endl;
break;
case 1:
w = p->right;
switch (w->balanceFactor)
{
case -1:
root->balanceFactor = 1;
p->balanceFactor = 0;
break;
case 0:
root->balanceFactor = 0;
p->balanceFactor = 0;
break;
case 1:
root->balanceFactor = 0;
p->balanceFactor = -1;
}
w->balanceFactor = 0;
rotateToLeft(p);
root->left = p;
rotateToRight(root);
};
}
template<typename Key, typename Info>
void balanceFromRight(Node<Key, Info>* &root)
{
Node<Key, Info> *p;
Node<Key, Info> *w;
p = root->right;
switch (p->balanceFactor)
{
case -1:
w = p->left;
switch (w->balanceFactor)
{
case -1:
root->balanceFactor = 0;
p->balanceFactor = 1;
break;
case 0:
root->balanceFactor = 0;
p->balanceFactor = 0;
break;
case 1:
root->balanceFactor = -1;
p->balanceFactor = 0;
}
w->balanceFactor = 0;
rotateToRight(p);
root->right = p;
rotateToLeft(root);
break;
case 0:
std::cerr << "Error: Cannot balance from the left." << std::endl;
break;
case 1:
root->balanceFactor = 0;
p->balanceFactor = 0;
rotateToLeft(root);
}
}
template <typename Key, typename Info>
void insertIntoDict(Node<Key, Info>* &root, Node<Key, Info> *newNode, bool& isTaller)
{
if (root == NULL)
{
root = newNode;
isTaller = true;
}
else if (root->key == newNode->key) // if two nodes have the same key we replace one with another
{
root = newNode;
isTaller = true;
}
else if (root->key > newNode->key)
{
insertIntoDict(root->left, newNode, isTaller);
if (isTaller)
switch (root->balanceFactor)
{
case -1:
balanceFromLeft(root);
isTaller = false;
break;
case 0:
root->balanceFactor = -1;
isTaller = true;
break;
case 1:
root->balanceFactor = 0;
isTaller = false;
}
}
else
{
insertIntoDict(root->right, newNode, isTaller);
if (isTaller)
switch (root->balanceFactor)
{
case -1:
root->balanceFactor = 0;
isTaller = false;
break;
case 0:
root->balanceFactor = 1;
isTaller = true;
break;
case 1:
balanceFromRight(root);
isTaller = false;
}
}
}
template<typename Key, typename Info>
Info& Dictionary<Key, Info>::search(const Key& keyToFind) const
{
Node<Key, Info> *curr;
bool found = 0;
if(root == nullptr)
{
std::cerr << "cannot search if tree is empty" << std::endl;
}else
{
curr = root;
while(curr != nullptr && !found)
{
if( curr -> key == keyToFind) found = true;
else if (curr -> key > keyToFind) curr = curr -> left;
else curr = curr -> right;
}
}
if(found) return curr -> info;
else throw "Key not found";
}
template<typename Key, typename Info>
void printDict(const std::string& prefix, const Node<Key, Info>* node, bool isLeft)
{
if( node != nullptr )
{
std::cout << prefix;
std::cout << (isLeft ? "├──" : "└──" );
// print the value of the node
std::cout << "( " << node->info << ", " << node->key << ")" << std::endl;
// enter the next tree level - left and right branch
printDict( prefix + (isLeft ? "" : " "), node->left, true);
printDict( prefix + (isLeft ? "" : " "), node->right, false);
}
}
template<typename Key, typename Info>
void Dictionary<Key, Info>::print() const
{
printDict("", root, false);
std::cout << std::endl;
}
template <typename Key, typename Info>
void Dictionary<Key, Info>::insert(const Key &newKey, const Info &newInfo)
{
bool isTaller = false;
Node<Key, Info>* newNode;
newNode = new Node<Key, Info>;
newNode -> key = newKey;
newNode->info = newInfo;
newNode->balanceFactor = 0;
newNode->left = NULL;
newNode->right = NULL;
insertIntoDict(root, newNode, isTaller);
}
template <typename Key, typename Info>
void Dictionary<Key, Info>::insertAndPrint(const Key &newKey, const Info &newInfo)
{
insert(newKey, newInfo);
print();
}
template <typename Key, typename Info>
Node<Key, Info>* minKeyNode(Node<Key, Info>* node)
{
Node<Key, Info>* curr = node;
while(curr -> left != nullptr) curr = curr -> left;
return curr;
}
int max(int a, int b) { return ( a > b ? a : b ); }
template <typename Key, typename Info>
int height(Node<Key, Info>* root)
{
if(root == nullptr) return 0;
else return 1 + max(height(root -> left), height(root -> right));
}
template <typename Key, typename Info>
int getBalance(Node<Key, Info>* root)
{
if(root == nullptr) return 0;
return height(root -> left) - height(root -> right);
}
template <typename Key, typename Info>
Node<Key, Info>* deleteNode(Node<Key, Info>* root, const Key& keyToDelete)
{
if (root == nullptr) return root;
if ( (root -> key) > keyToDelete) root->left = deleteNode(root->left, keyToDelete);
else if( (root -> key) < keyToDelete) root->right = deleteNode(root->right, keyToDelete);
else
{
if( (root->left == nullptr) || (root->right == nullptr) )
{
Node<Key, Info> *temp;
if(root -> left == nullptr) temp = root -> right;
else temp = root -> left;
if (temp == nullptr)
{
temp = root;
root = nullptr;
}
else
{
*root = *temp;
delete temp;
}
}
else
{
Node<Key, Info>* temp = minKeyNode(root->right);
root->key = temp->key;
root->info = temp->info;
root->right = deleteNode(root->right, temp->key);
}
}
if (root == nullptr) return root;
int balance = getBalance(root);
if (balance > 1 && getBalance(root->left) >= 0)
{
rotateToRight(root);
return root;
}
if (balance > 1 && getBalance(root->left) < 0)
{
rotateToLeft(root->left);
rotateToRight(root);
return root;
}
if (balance < -1 && getBalance(root->right) <= 0)
{
rotateToLeft(root);
return root;
}
if (balance < -1 && getBalance(root->right) > 0)
{
rotateToRight(root->right);
rotateToLeft(root);
return root;
}
return root;
}
template<typename Key, typename Info>
void Dictionary<Key, Info>::remove(const Key& keyToRemove)
{
deleteNode(root, keyToRemove);
}
bool testInsertEmpty()
{
print("testInsertEmpty print:");
Dictionary<std::string, int> marcel;
marcel.print();
return true;
}
bool testInsertOneElement()
{
print("testInsertOneElement print:");
Dictionary<std::string, int> marcel;
marcel.insert("perception", 1);
marcel.print();
return true;
}
bool testInsertMoreElements()
{
print("testInsertMoreElements print:");
Dictionary<std::string, int> marcel;
marcel.insertAndPrint("thank", 20);
marcel.insertAndPrint("accept", 90);
marcel.insertAndPrint("public", 30);
marcel.insertAndPrint("rack", 8);
marcel.insertAndPrint("pest", 10);
return true;
}
bool testInsert()
{
return testInsertEmpty()&&testInsertOneElement()&&testInsertMoreElements();
}
bool testSearchEmpty()
{
Dictionary<std::string, int> marcel;
try{
marcel.search("marcel");
}
catch (char const* e)
{
return true;
};
return false;
}
bool testSearchOneElement()
{
Dictionary<std::string, int> marcel;
marcel.insert("jungle", 20);
if(marcel.search("jungle") != 20)
{
print("Error in testSearchOneElement");
return 0;
}
return 1;
}
bool testSearchMoreElements()
{
Dictionary<std::string, int> marcel;
marcel.insert("thank", 20);
marcel.insert("accept", 90);
marcel.insert("public", 30);
marcel.insert("rack", 8);
marcel.insert("pest", 10);
if(marcel.search("public") != 30)
{
print("Error in testSearchMoreElements");
return 0;
}
return 1;
}
bool testSearch()
{
return testSearchEmpty()&&testSearchOneElement()&&testSearchMoreElements();
}
bool testRemoveEmpty()
{
Dictionary<std::string, int> marcel;
marcel.remove("marcel");
return true;
}
bool testRemove()
{
return testRemoveEmpty();
}
bool tests()
{
return testInsert()&&testSearch()&&testRemove();
}
int main()
{
bool resultOfTests = tests();
std::cout << "Result of tests is: " << resultOfTests << std::endl;
return 1;
}

Binary file not shown.

BIN
EDISP/EDISP_LAB_4.pdf Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

100
EOOP/Various/set/set.cpp Normal file
View File

@ -0,0 +1,100 @@
#include "set.hpp"
Set::Set()
{
data_ = {};
}
Set::~Set()
{
data_.erase(data_.begin(), data_.end());
}
Set::Set(const Set& s)
{
data_ = s.data_;
}
Set& Set::operator=(const Set& s)
{
data_ = s.data_;
return *this;
}
bool Set::isEmpty() const
{
return data_.size() == 0;
}
SetData::size_type Set::size() const
{
return data_.size();
}
void Set::add(int value)
{
if(!contains(value)) data_.push_back(value);
}
Set Set::operator+(const Set& s) const
{
Set newSet(s);
for (unsigned int i = 0; i < data_.size(); ++i)
{
if(!newSet.contains(data_[i])) newSet.add(data_[i]);
}
return newSet;
}
Set& Set::operator+=(const Set& s)
{
for(unsigned int i = 0; i < s.size(); ++i)
{
if(!contains(s.data_[i])) add(s.data_[i]);
}
return *this;
}
bool Set::contains(int value) const
{
for(unsigned int i = 0; i < size(); ++i)
{
if(data_[i] == value) return 1;
}
return 0;
}
Set Set::operator-(const Set& s) const
{
Set newSet;
for(unsigned int i = 0; i < size(); ++i)
{
if(!s.contains(data_[i])) newSet.add(data_[i]);
}
return newSet;
}
void Set::remove(int value)
{
if(contains(value))
{
for(unsigned int i = 0; i < size(); ++i)
{
if(data_[i] == value) data_.erase(data_.begin() + i);
}
}
}
Set& Set::operator-=(const Set& s)
{
for(int i = size(); i >= 0; i--)
{
if(s.contains(data_[i])) remove(data_[i]);
}
return *this;
}
const int& Set::operator[](int index) const
{
return data_[index];
}

18
EPFU/labs/krudnic3_lab1.c Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i_one, i_two;
float f_one, f_two;
char c_one, c_two;
printf("Chris\n");
printf("Write two integers then two float numbers then two characters:");
scanf("%d %d %f %f %c %c", &i_one, &i_two, &f_one, &f_two, &c_one, &c_two);
printf("%d %d %d %f %c %d", i_one, i_two, f_one, f_two, c_one, c_two);
printf("\n%d", i_one + i_two);
printf("\n%f", i_one / i_two);
printf("\n%f", f_one / f_two);
printf("\n%c%d", c_one, i_one);
return 0;
}

View File

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 110 KiB

View File

Before

Width:  |  Height:  |  Size: 73 KiB

After

Width:  |  Height:  |  Size: 73 KiB

View File

Before

Width:  |  Height:  |  Size: 52 KiB

After

Width:  |  Height:  |  Size: 52 KiB

View File

Before

Width:  |  Height:  |  Size: 83 KiB

After

Width:  |  Height:  |  Size: 83 KiB

View File

Before

Width:  |  Height:  |  Size: 90 KiB

After

Width:  |  Height:  |  Size: 90 KiB

View File

Before

Width:  |  Height:  |  Size: 232 KiB

After

Width:  |  Height:  |  Size: 232 KiB

View File

Before

Width:  |  Height:  |  Size: 43 KiB

After

Width:  |  Height:  |  Size: 43 KiB

Some files were not shown because too many files have changed in this diff Show More