-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <stdexcept> | ||
#include <string> | ||
#include <iostream> | ||
|
||
class Node | ||
{ | ||
public: | ||
Node(int value, Node* left, Node* right) | ||
{ | ||
this->value = value; | ||
this->left = left; | ||
this->right = right; | ||
} | ||
|
||
int getValue() const | ||
{ | ||
return value; | ||
} | ||
|
||
Node* getLeft() const | ||
{ | ||
return left; | ||
} | ||
|
||
Node* getRight() const | ||
{ | ||
return right; | ||
} | ||
|
||
private: | ||
int value; | ||
Node* left; | ||
Node* right; | ||
}; | ||
|
||
class BinarySearchTree | ||
{ | ||
public: | ||
static bool contains(const Node& root, int value) | ||
{ | ||
int node_value = root.getValue(); | ||
if (value == node_value){ | ||
return true; | ||
} | ||
else if (value > node_value && root.getRight() != NULL){ | ||
|
||
return contains(*root.getRight(), value); | ||
} | ||
else if (value < node_value && root.getLeft() != NULL){ | ||
return contains(*root.getLeft(), value); | ||
} | ||
else{ | ||
return false; | ||
} | ||
} | ||
}; | ||
|
||
#ifndef RunTests | ||
int main() | ||
{ | ||
Node n1(1, NULL, NULL); | ||
Node n3(3, NULL, NULL); | ||
Node n2(2, &n1, &n3); | ||
|
||
std::cout << BinarySearchTree::contains(n2, 3); | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <tuple> | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <string> | ||
#include <cmath> | ||
|
||
std::pair<double, double> findRoots(double a, double b, double c) | ||
{ | ||
double discriminant = std::pow(b,2) - 4*a*c; | ||
|
||
std::pair<double, double> roots; | ||
|
||
roots.first = (-b + std::sqrt(discriminant))/(2*a); | ||
roots.second = (-b - std::sqrt(discriminant))/(2*a); | ||
|
||
return roots; | ||
|
||
|
||
} | ||
|
||
#ifndef RunTests | ||
int main() | ||
{ | ||
std::pair<double,double> roots = findRoots(2, 10, 8); | ||
std::cout << "Roots: " + std::to_string(roots.first) + ", " + std::to_string(roots.second); | ||
} | ||
#endif |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <deque> | ||
#include <chrono> | ||
|
||
class TrainComposition | ||
{ | ||
private: | ||
std::deque <int> train; | ||
|
||
public: | ||
void attachWagonFromLeft(int wagonId) | ||
{ | ||
this->train.push_back(wagonId); | ||
} | ||
|
||
void attachWagonFromRight(int wagonId) | ||
{ | ||
this->train.push_front(wagonId); | ||
} | ||
|
||
int detachWagonFromLeft() | ||
{ | ||
int wagonID = this->train.back(); | ||
this->train.pop_back(); | ||
|
||
return wagonID; | ||
} | ||
|
||
int detachWagonFromRight() | ||
{ | ||
int wagonID = this->train.front(); | ||
this->train.pop_front(); | ||
|
||
return wagonID; | ||
} | ||
}; | ||
|
||
#ifndef RunTests | ||
int main() | ||
{ | ||
TrainComposition train; | ||
train.attachWagonFromLeft(7); | ||
train.attachWagonFromLeft(13); | ||
std::cout << train.detachWagonFromRight() << "\n"; // 7 | ||
std::cout << train.detachWagonFromLeft(); // 13 | ||
} | ||
#endif |