#include #include #include struct treenode { int data; treenode * left, * right; treenode(int n, treenode * l = NULL, treenode * r = NULL) { data=n; left=l; right=r; } }; void insert(treenode * & T, int N) { if (T==NULL) T = new treenode(N); else if (T->data==N) { } else if (N < T->data) insert(T->left, N); else insert(T->right, N); } void print(treenode * T, ostream & fo) { if (T==NULL) fo << "@ "; else { fo << "( "; print(T->left, fo); fo << T->data << " "; print(T->right, fo); fo << ") "; } } void print_differently(treenode * T, ostream & fo) { if (T==NULL) fo << "@ "; else { fo << "( "; fo << T->data << " "; print_differently(T->left, fo); print_differently(T->right, fo); fo << ") "; } } treenode * read(istream & fi) { char c; fi >> c; if (c=='@') return NULL; else if (c=='(') { treenode * L = read(fi); int N; fi >> N; treenode * R = read(fi); char c; fi >> c; if (c!=')') { cerr << "saw " << c << " when expecting )\n"; return NULL; } return new treenode(N, L, R); } else { cerr << "first char " << c << " not ( or @\n"; return NULL; } } treenode * read_old_way(istream & fin) { treenode * t = NULL; while (true) { int x; fin >> x; if (fin.fail()) break; insert(t, x); } return t; } void main() { treenode * collection = NULL; ifstream fin("output"); collection = read(fin); fin.close(); ofstream fout("output"); print_differently(collection, cout); cout << "\n"; fout.close(); }