Created
July 10, 2020 15:57
-
-
Save niklasbuschmann/ced0fe2428cdc00533892640f8822792 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <iostream> | |
#include <cstdlib> | |
#include <time.h> | |
using namespace std; | |
class Node { | |
private: | |
double value; | |
Node* left; | |
Node* right; | |
public: | |
Node(double x) { | |
value = x; | |
left = 0; | |
right = 0; | |
} | |
friend ostream& operator<<(ostream& os, Node& node) { | |
if (node.left) | |
os << *node.left; | |
os << node.value << " "; | |
if (node.right) | |
os << *node.right; | |
return os; | |
} | |
double min() { | |
return left ? left->min() : value; | |
} | |
void insert(double x) { | |
if (x < value && left) | |
left->insert(x); | |
if (x > value && right) | |
right->insert(x); | |
if (x < value && !left) | |
left = new Node(x); | |
if (x > value && !right) | |
right = new Node(x); | |
} | |
}; | |
int main() { | |
srand ((unsigned int)time(0)); | |
Node tree(1.0); | |
for (int i = 0; i < 10; i++) { | |
double x = 2.0 * double(rand())/RAND_MAX; | |
cout << "Einzufügen: " << x << endl; | |
tree.insert(x); | |
} | |
cout << "Die minimale Zahl im Baum ist: " << tree.min() << endl; | |
cout << "Alle Einträge sortiert: " << endl << " " << tree << endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment