// // Sample application program for binary search tree // // --------------------------------------------------------- #include "bstree.h" // --------------------------------------------------------- main () { BTree bt; cout << endl << "Building binary tree." << endl << endl; srand(77); for (UINT i=0; i<20; ++i) bt.insert (rand()%2000); // ------------------------------------------------------- bt.print (); cout << endl << "Number of nodes: " << bt.nNodes () << '\t' << "Height: " << bt.height () << endl << endl; cout << "Search for data:" << endl << endl; cout << "Is 732 in the tree? " << bt.search(732) << endl << "Is 253 in the tree? " << bt.search(253) << endl << endl; cout << "Removal of nodes:" << endl << endl; cout << "1589 successfully removed? " << bt.remove (1589) << endl << "749 successfully removed? " << bt.remove (749) << endl << "597 successfully removed? " << bt.remove (597) << endl << "123 successfully removed? " << bt.remove (123) << endl << endl; bt.print (); cout << endl << "Number of nodes: " << bt.nNodes () << '\t' << "Height: " << bt.height () << endl << endl; // ------------------------------------------------------- cout << endl << "Using iterators:" << endl << endl; PreorderIterator pre(bt); cout << "Traversing the tree in PREORDER:" << endl << endl; for (pre.reset(); !pre.atEnd(); ++pre) cout << " " << bt[pre]; cout << endl << endl; InorderIterator in(bt); cout << "Traversing the tree in INORDER:" << endl << endl; for (in.reset(); !in.atEnd(); ++in) cout << " " << bt[in]; cout << endl << endl; PostorderIterator post(bt); cout << "Traversing the tree in POSTORDER:" << endl << endl; for (post.reset(); !post.atEnd(); ++post) cout << " " << bt[post]; cout << endl << endl; LevelIterator lev(bt); cout << "Traversing the tree in LEVEL order:" << endl << endl; for (lev.reset(); !lev.atEnd(); ++lev) cout << " " << bt[lev]; cout << endl << endl; } // ---------------------------------------------------------