#include <string> #include <iostream> struct node { node * left; string data; node * right; node(node * L, string S, node * R): left(L), data(S), right(R) { } }; void insert(node * & tree, string s) { if (tree==NULL) tree=new node(NULL, s, NULL); else if (s<tree->data) insert(tree->left, s); else insert(tree->right, s); } void recursive_print(node * tree) { // this is point 0 if (tree==NULL) return; // this is point 1 recursive_print(tree->left); // this is point 2 cout << tree->data << "\n"; // this is point 3 recursive_print(tree->right); // this is point 4 } struct SFprint { node * tree; int where; SFprint * prev; SFprint(node * T, int W, SFprint * P): tree(T), where(W), prev(P) { } }; SFprint * create_enumeration(node * tree) { return new SFprint(tree, 0, NULL); } bool more_to_come(SFprint * & stack) { while (stack!=NULL) { node * tree = stack->tree; int where = stack->where; if (where==0) { if (tree==NULL) { stack = stack->prev; } else { stack->where = 1; } } else if (where==1) { stack->where = 2; stack = new SFprint(tree->left, 0, stack); } else if (where==2) { stack->where = 3; return true; } else if (where==3) { stack->where = 4; stack = new SFprint(tree->right, 0, stack); } else if (where==4) { stack=stack->prev; } } return false; } string get_current(SFprint * stack) { if (stack==NULL) return ""; else return stack->tree->data; } string inputs1[] = { "freddy", "colin", "earnest", "zebidee", "kenny", "douglas", "brenda", "yolanda" }; const int num_inputs1 = sizeof(inputs1)/sizeof(string); string inputs2[] = { "koala", "dog", "earwig", "yeti", "cat", "zebra", "bat", "frog" }; const int num_inputs2 = sizeof(inputs2)/sizeof(string); void main(void) { node * root1 = NULL, * root2 = NULL; for (int i=0; i<num_inputs1; i+=1) insert(root1, inputs1[i]); for (int i=0; i<num_inputs2; i+=1) insert(root2, inputs2[i]); SFprint * enumer1 = create_enumeration(root1); SFprint * enumer2 = create_enumeration(root2); while (more_to_come(enumer1) && more_to_come(enumer2)) { cout << get_current(enumer1) << " the " << get_current(enumer2) << "\n"; } } $ CC nonrec.cpp $ a.out brenda the bat colin the cat douglas the dog earnest the earwig freddy the frog kenny the koala yolanda the yeti zebidee the zebra