#include <iostream>
#include <iomanip>
#include <math.h>


using namespace std;

// Deklarationen


class Node {
	private:
		double value ; // Wert des Knoten
		Node* left ;    // linker Ast
		Node* right ;   // rechter Ast
	public:
		Node(double wert);
		void insert(double x);
		float min();
		friend ostream& operator<< (ostream& os, Node knoten);
};

// Implementation

ostream& operator<< (ostream& os, Node knoten)
{
	if(knoten.left) os << *knoten.left;
	os << knoten.value << " ";
	if(knoten.right) os << *knoten.right;
	return(os);
}

Node::Node(double wert)
{
	value = wert;
	left = NULL;
	right = NULL;
}

void Node::insert(double x)
{
	if(x <= value)
	{
		if(left)
		{
			left->insert(x);
		}
		else
		{
			left = new Node(x);
		}
	}else
	{
		if(right)
		{
			right->insert(x);
		}
		else
		{
			right = new Node(x);
		}
	}
}

float Node::min()
{
	double tmpmin = value;
	// Nach links schauen
	if(left && left->min() < tmpmin) tmpmin = left->min();
	// Nach rechts schauen
	if(right && right->min() < tmpmin) tmpmin = right->min();
	return(tmpmin);
}

int main()
{
	srand((unsigned int)time(0));
	Node tree(1.0) ; // Basiselement des Baums
	for(int i=0; i<10; i++) // 10 Zufallszahlen (0..2) in den Baum einhaengen
	tree.insert(2*double(rand())/RAND_MAX) ;
	cout << "Die minimale Zahl im Baum ist: " << tree.min() << endl ;
	cout << "Alle Eintraege sortiert: " << endl << " " << tree << endl ;
	return(0);
}
