School Commit Init

This commit is contained in:
2024-08-31 12:07:21 +03:00
commit 0b130ee18c
2801 changed files with 4720552 additions and 0 deletions
@@ -0,0 +1,15 @@
#include "SortedBag.h"
#include "SortedBagIterator.h"
#include <iostream>
#include "ShortTest.h"
#include "ExtendedTest.h"
using namespace std;
int main() {
testAll();
testAllExtended();
cout << "Test over" << endl;
system("pause");
}
@@ -0,0 +1,328 @@
#include "ShortTest.h"
#include "SortedBag.h"
#include "SortedBagIterator.h"
#include <assert.h>
#include <iostream>
#include <exception>
using namespace std;
bool relation2(TComp r1, TComp r2) {
return r1 <= r2;
}
bool relation3(TComp r1, TComp r2) {
return r1 >= r2;
}
void testCreate() {
cout << "Test create" << endl;
SortedBag sb(relation2);
assert(sb.isEmpty() == true);
assert(sb.size() == 0);
for (int i = -10; i < 30; i++) {
assert(sb.remove(i) == false);
}
for (int i = -10; i < 30; i++) {
assert(sb.search(i) == false);
}
for (int i = -10; i < 30; i++) {
assert(sb.nrOccurrences(i) == 0);
}
SortedBagIterator it = sb.iterator();
assert(it.valid() == false);
try {
it.getCurrent();
assert(false);
}
catch (exception&) {
assert(true);
}
try {
it.next();
assert(false);
}
catch (exception&) {
assert(true);
}
}
void testIterator(SortedBag& sb, Relation rel) {
SortedBagIterator it = sb.iterator();
TComp e1;
TComp e2;
if (it.valid()) {
e1 = it.getCurrent();
it.next();
}
while (it.valid()) {
e2 = e1;
e1 = it.getCurrent();
it.next();
assert(sb.search(e1) == true);
assert(sb.search(e2) == true);
assert(sb.nrOccurrences(e1) > 0);
assert(sb.nrOccurrences(e2) > 0);
assert(rel(e2, e1));
}
try {
it.getCurrent();
assert(false);
}
catch (exception&) {
assert(true);
}
try {
it.next();
assert(false);
}
catch (exception&) {
assert(true);
}
it.first();
assert(it.valid() == true);
it.first();
int count = 0;
while (it.valid()) {
count++;
it.next();
}
assert(count == sb.size());
}
void testAdd(Relation r) {
cout << "Test add" << endl;
SortedBag sb(r);
for (int i = 0; i < 100; i++) {
sb.add(i);
}
assert(sb.size() == 100);
assert(sb.isEmpty() == false);
testIterator(sb, r);
for (int i = 200; i >= -200; i--) {
sb.add(i);
}
assert(sb.size() == 501);
testIterator(sb, r);
for (int i = -300; i < 300; i++) {
bool exista = sb.search(i);
int nrA = sb.nrOccurrences(i);
if (i < -200 || i > 200) {
assert(exista == false);
assert(nrA == 0);
}
else if (i >= -200 && i < 0) {
assert(exista == true);
assert(nrA == 1);
}
else if (i >= 0 && i < 100) {
assert(exista == true);
assert(nrA == 2);
}
else if (i >= 100 && i <= 200) {
assert(exista == true);
assert(nrA == 1);
}
}
for (int i = 0; i < 100; i++) {
sb.add(0);
}
assert(sb.nrOccurrences(0) == 102);
testIterator(sb, r);
SortedBag sb2(r);
for (int i = 0; i < 300; i = i + 2) {
sb2.add(i);
sb2.add(2 * i);
sb2.add(-2 * i);
}
assert(sb2.size() == 450);
testIterator(sb2, r);
}
void testRemove(Relation r) {
cout << "Test remove" << endl;
SortedBag sb(r);
for (int i = -100; i < 100; i++) {
assert(sb.remove(i) == false);
assert(sb.search(i) == false);
assert(sb.nrOccurrences(i) == 0);
}
assert(sb.isEmpty() == true);
for (int i = -100; i < 100; i++) {
sb.add(i);
}
assert(sb.size() == 200);
for (int i = -100; i < 100; i = i + 2) {
assert(sb.remove(i) == true);
}
assert(sb.size() == 100);
for (int i = -100; i < 100; i++) {
if (i % 2 == 0) {
assert(sb.nrOccurrences(i) == 0);
assert(sb.search(i) == false);
assert(sb.remove(i) == false);
}
else {
assert(sb.nrOccurrences(i) == 1);
assert(sb.search(i) == true);
}
sb.add(i);
sb.add(i);
sb.add(i);
}
assert(sb.size() == 700);
for (int i = -200; i < 200; i++) {
if (i < -100 || i >= 100) {
assert(sb.search(i) == false);
assert(sb.nrOccurrences(i) == 0);
assert(sb.remove(i) == false);
}
else if (i % 2 == 0) {
assert(sb.search(i) == true);
assert(sb.nrOccurrences(i) == 3);
assert(sb.remove(i) == true);
assert(sb.remove(i) == true);
assert(sb.remove(i) == true);
assert(sb.remove(i) == false);
}
else {
assert(sb.search(i) == true);
assert(sb.nrOccurrences(i) == 4);
assert(sb.remove(i) == true);
assert(sb.remove(i) == true);
assert(sb.remove(i) == true);
assert(sb.remove(i) == true);
assert(sb.remove(i) == false);
}
}
SortedBag sb2(r);
for (int i = 300; i >= -500; i--) {
sb2.add(i);
sb2.add(i * 2);
sb2.add(-2 * i);
}
for (int i = -100; i < 100; i++) {
for (int j = 0; j < 100; j++) {
sb2.remove(i);
}
}
for (int i = -100; i < 0; i++) {
assert(sb2.nrOccurrences(i) == 0);
assert(sb2.search(i) == false);
}
}
void testQuantity(Relation r) {
cout << "Test quantity" << endl;
SortedBag sb(r);
for (int j = 0; j < 10; j++) {
sb.add(0);
for (int i = 1; i < 300; i++) {
sb.add(i);
sb.add(-i);
}
sb.add(-3000);
}
int count = 6000;
assert(sb.size() == 6000);
for (int i = 0; i < 5000; i++) {
TComp nr = rand() % 8000 - 4000;
if (sb.remove(nr) == true) {
count--;
}
assert(sb.size() == count);
}
assert(sb.size() == count);
}
void testIterator(Relation rel) {
cout << "Test iterator" << endl;
SortedBag sb(rel);
for (int i = 0; i < 500; i++) {
sb.add(i);
sb.add(-2 * i);
sb.add(2 * i);
sb.add(i);
sb.add(-2 * i);
sb.add(2 * i);
}
assert(sb.size() == 3000);
SortedBagIterator sbi = sb.iterator();
int count = 0;
while (sbi.valid()) {
count++;
sbi.next();
}
assert(count == sb.size());
sbi.first();
TElem e = sbi.getCurrent();
sbi.next();
count = 1;
while (sbi.valid()) {
TElem ee = sbi.getCurrent();
assert(rel(e, ee));
TElem ee2 = sbi.getCurrent();
assert(ee == ee2);
TElem ee3 = sbi.getCurrent();
assert(ee == ee3);
e = ee;
sbi.next();
count++;
}
assert(count == sb.size());
}
void testExtra(){
cout << "Test extra" << endl;
SortedBag sb(relation2);
sb.add(2);
sb.add(2);
sb.add(2);
sb.add(1);
sb.add(1);
sb.add(1);
sb.add(1);
sb.add(2);
sb.add(5);
sb.add(3);
sb.add(4);
assert(sb.size() == 11);
assert(sb.nrOccurrences(1) == 4);
assert(sb.nrOccurrences(2) == 4);
assert(sb.nrOccurrences(3) == 1);
assert(sb.nrOccurrences(4) == 1);
assert(sb.nrOccurrences(5) == 1);
assert(sb.elementsWithThisFrequency(1) == 3);
assert(sb.elementsWithThisFrequency(4) == 2);
try {
sb.elementsWithThisFrequency(0);
assert(false);
}
catch (exception&) {
assert(true);
}
}
void testAllExtended() {
testCreate();
testAdd(relation2);
testAdd(relation3);
testRemove(relation2);
testRemove(relation3);
testIterator(relation2);
testIterator(relation3);
testQuantity(relation2);
testQuantity(relation3);
testExtra();
}
@@ -0,0 +1,3 @@
#pragma once
void testAllExtended();
@@ -0,0 +1,39 @@
#include "ShortTest.h"
#include "SortedBag.h"
#include "SortedBagIterator.h"
#include <assert.h>
bool relation1(TComp e1, TComp e2) {
return e1 <= e2;
}
void testAll() {
SortedBag sb(relation1);
sb.add(5);
sb.add(6);
sb.add(0);
sb.add(5);
sb.add(10);
sb.add(8);
assert(sb.size() == 6);
assert(sb.nrOccurrences(5) == 2);
assert(sb.remove(5) == true);
assert(sb.size() == 5);
assert(sb.search(6) == true);
assert(sb.isEmpty() == false);
SortedBagIterator it = sb.iterator();
assert(it.valid() == true);
while (it.valid()) {
it.getCurrent();
it.next();
}
assert(it.valid() == false);
it.first();
assert(it.valid() == true);
}
@@ -0,0 +1,3 @@
#pragma once
void testAll();
@@ -0,0 +1,239 @@
#include "SortedBag.h"
#include "SortedBagIterator.h"
#include <exception>
SortedBag::SortedBag(Relation r) {
//TODO - Implementation
this->relation = r;
this->root = nullptr;
this->length = 0;
}
void SortedBag::add(TComp e) {
//Best case: O(1) Worst case: O(n) Average case: O(log n)
BSTNode* newNode = new BSTNode;
newNode->elem = e;
newNode->left = nullptr;
newNode->right = nullptr;
newNode->parent = nullptr;
newNode->count = 1;
if (this->root == nullptr) {
this->root = newNode;
this->length++;
return;
}
BSTNode* current = this->root;
while (current != nullptr) {
if (current->elem == e) {
current->count++;
this->length++;
return;
}
if (this->relation(e, current->elem)) {
if (current->left == nullptr) {
current->left = newNode;
newNode->parent = current;
this->length++;
return;
}
else {
current = current->left;
}
}
else {
if (current->right == nullptr) {
current->right = newNode;
newNode->parent = current;
this->length++;
return;
}
else {
current = current->right;
}
}
}
}
bool SortedBag::remove(TComp e) {
//Best case: O(1) Worst case: O(n) Average case: O(log n)
BSTNode* current = this->root;
while (current != nullptr) {
if(current->elem==e){
if(current->count>1){
current->count--;
this->length--;
return true;
}
else{
if(current->left==nullptr && current->right==nullptr){
if(current->parent==nullptr){
this->root=nullptr;
this->length--;
return true;
}
else{
if(current->parent->left==current){
current->parent->left=nullptr;
this->length--;
return true;
}
else{
current->parent->right=nullptr;
this->length--;
return true;
}
}
}
else{
if(current->left==nullptr){
if(current->parent==nullptr){
this->root=current->right;
this->root->parent=nullptr;
this->length--;
return true;
}
else{
if(current->parent->left==current){
current->parent->left=current->right;
this->length--;
return true;
}
else{
current->parent->right=current->right;
this->length--;
return true;
}
}
}
else{
if(current->right==nullptr){
if(current->parent==nullptr){
this->root=current->left;
this->root->parent=nullptr;
this->length--;
return true;
}
else{
if(current->parent->left==current){
current->parent->left=current->left;
this->length--;
return true;
}
else{
current->parent->right=current->left;
this->length--;
return true;
}
}
}
else{
BSTNode* current2=current->right;
while(current2->left!=nullptr){
current2=current2->left;
}
current->elem=current2->elem;
current->count=current2->count;
if(current2->parent->left==current2){
current2->parent->left=current2->right;
this->length--;
return true;
}
else{
current2->parent->right=current2->right;
this->length--;
return true;
}
}
}
}
}
}
else{
if(this->relation(e,current->elem)){
current=current->left;
}
else{
current=current->right;
}
}
}
return false;
}
bool SortedBag::search(TComp elem) const {
// Best case: O(1) Worst case: O(n) Average case: O(log n)
BSTNode* current = this->root;
while (current != nullptr) {
if (current->elem == elem) {
return true;
}
if (this->relation(elem, current->elem)) {
current = current->left;
}
else {
current = current->right;
}
}
return false;
}
int SortedBag::nrOccurrences(TComp elem) const {
// Best case: O(1) Worst case: O(n) Average case: O(log n)
BSTNode* current = this->root;
while (current != nullptr) {
if (current->elem == elem) {
return current->count;
}
if (this->relation(elem, current->elem)) {
current = current->left;
}
else {
current = current->right;
}
}
return 0;
}
int SortedBag::size() const {
//O(1)
return this->length;
}
bool SortedBag::isEmpty() const {
//O(1)
return (this->length == 0);
}
SortedBagIterator SortedBag::iterator() const {
return SortedBagIterator(*this);
}
SortedBag::~SortedBag() {
//O(n)
delete this->root;
}
int SortedBag::elementsWithThisFrequency(int frequency) const {
// O(n)
if(frequency<=0){
throw std::exception();
}
return reccuriveCount(this->root,frequency);
}
int SortedBag::reccuriveCount(BSTNode* node, int frequency) const {
if(node==nullptr){
return 0;
}
return reccuriveCount(node->left,frequency)+reccuriveCount(node->right, frequency) + (this->nrOccurrences(node->elem)==frequency);
}
@@ -0,0 +1,65 @@
#pragma once
//DO NOT INCLUDE SORTEDBAGITERATOR
//DO NOT CHANGE THIS PART
typedef int TComp;
typedef TComp TElem;
typedef bool(*Relation)(TComp, TComp);
#define NULL_TCOMP -11111;
class BSTNode {
public:
TComp elem;
BSTNode* left;
BSTNode* right;
BSTNode* parent;
~BSTNode() {
delete left;
delete right;
}
int count;
};
class SortedBagIterator;
class SortedBag {
friend class SortedBagIterator;
private:
//TODO - Representation
BSTNode* root;
Relation relation;
int length;
int reccuriveCount(BSTNode* node, int frequency) const;
public:
//constructor
SortedBag(Relation r);
//adds an element to the sorted bag
void add(TComp e);
//removes one occurence of an element from a sorted bag
//returns true if an element was removed, false otherwise (if e was not part of the sorted bag)
bool remove(TComp e);
//checks if an element appearch is the sorted bag
bool search(TComp e) const;
//returns the number of occurrences for an element in the sorted bag
int nrOccurrences(TComp e) const;
//returns the number of elements from the sorted bag
int size() const;
//returns an iterator for this sorted bag
SortedBagIterator iterator() const;
//checks if the sorted bag is empty
bool isEmpty() const;
//destructor
~SortedBag();
int elementsWithThisFrequency(int frequency) const;
};
@@ -0,0 +1,73 @@
#include "SortedBagIterator.h"
#include "SortedBag.h"
#include <exception>
using namespace std;
SortedBagIterator::SortedBagIterator(const SortedBag& b) : bag(b) {
// Best case: O(1) Worst case: O(n) Average case: O(log n)
this->first();
}
TComp SortedBagIterator::getCurrent() {
//O(1)
if(this->valid())
return this->current->elem;
else
throw exception();
}
bool SortedBagIterator::valid() {
//O(1)
if(this->current == nullptr)
return false;
return true;
}
void SortedBagIterator::next() {
// Best case: O(1) Worst case: O(n) Average case: O(log n)
if(this->valid())
{
if(this->currentCount > 1)
this->currentCount--;
else
{
if(this->current->right != nullptr)
{
this->current = this->current->right;
while(this->current->left != nullptr)
this->current = this->current->left;
this->currentCount = this->current->count;
}
else
{
BSTNode* currentParent = this->current->parent;
while(currentParent != nullptr && currentParent->right == this->current)
{
this->current = currentParent;
currentParent = currentParent->parent;
}
this->current = currentParent;
if(this->current != nullptr)
this->currentCount = this->current->count;
else
this->currentCount = 0;
}
}
}
else
throw exception();
}
void SortedBagIterator::first() {
// Best case: O(1) Worst case: O(n) Average case: O(log n)
this->current = this->bag.root;
if(this->current != nullptr){
while(this->current->left != nullptr)
this->current = this->current->left;
this->currentCount = this->current->count;
}
else{
this->currentCount = 0;
}
}
@@ -0,0 +1,24 @@
#pragma once
#include "SortedBag.h"
class SortedBag;
class SortedBagIterator
{
friend class SortedBag;
private:
const SortedBag& bag;
SortedBagIterator(const SortedBag& b);
//TODO - Representation
BSTNode* current;
int currentCount;
public:
TComp getCurrent();
bool valid();
void next();
void first();
};