Skip to content

Instantly share code, notes, and snippets.

@niklasbuschmann
Created July 10, 2020 15:57
Show Gist options
  • Save niklasbuschmann/ced0fe2428cdc00533892640f8822792 to your computer and use it in GitHub Desktop.
Save niklasbuschmann/ced0fe2428cdc00533892640f8822792 to your computer and use it in GitHub Desktop.
#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