// Using a binary search tree to sort data // Performance similar to QuickSort #include #include using namespace std; class Tree { private: Tree * left; // nodes with values less than or equal than this value int value; Tree * right; // nodes with values greater than this value public: Tree ( int v):left(NULL), value(v), right(NULL){} void insert (int v) { if( v <= value) { if(left) left->insert(v); else left = new Tree(v); } else { if(right) right->insert(v); else right = new Tree(v); } } void print() { if(left) left->print(); cout << " " << value << " "; if(right) right->print(); } }; int main() { int v; cin >> v; Tree root(v); while(cin >> v) { root.insert(v); } root.print(); }