//  File: main2.C
//
//  Testing the BSTree class.

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include "bstree.h"

main() { try {
   BSTree<int> *Tptr, *result ;
   int x ;

   Tptr = new BSTree<int> ;
   Tptr->insert(5) ;
   Tptr->insert(2) ;
   Tptr->insert(3) ;
   Tptr->insert(8) ;
   Tptr->insert(6) ;
   Tptr->insert(9) ;
   Tptr->insert(4) ;
   Tptr->insert(1) ;

   // Dump
   cout << "\nInorder Walk\n" ;
   Tptr->inorder() ;
   cout << endl ;


   // Search & Edit tree
   cout << "\nEdit tree (enter 'q' to quit):" << endl ;
   while(true) {
      cout << "Integer to search: " ;
      cin >> x ;

      if (!cin.good()) break ;

      result = Tptr->find(x) ;

      if (result == NULL) {
         cout << "Not found" << endl  ;
      } else {
         cout << "Found: "<< result->root->data << endl ;
         cout << "Replace with: " ;
         cin >> x ;
         result->removeroot() ;
         Tptr->insert(x) ;
         Tptr->inorder() ;
         cout << "\n" << endl ;
      }
   }

   // Dump again
   cout << "\nInorder Walk" << endl ;
   Tptr->inorder() ;
   cout << endl ;

   delete Tptr ;
}
catch (MemoryError& e) {
   cerr << "Out of memory" << endl ;
   exit(1) ;
} 
catch (...) {
   cerr << "Caught Unknown error" << endl ;
   exit(1) ;
}}
