// outputDotFiles.cc   write dot files  for binary trees  
// P. Conrad for CISC220, 06J

#include "runTests.h"
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;

// Now also do #include <string> and #include <sstream>
// This allows us to write to a string (C++ style string) as if it were 
// a file.  That way, we can test our print functions

#include <fstream>
using std::ofstream;
using std::cerr;
using std::cout;
using std::endl;

#include "bintree.h"

#include <sys/types.h> // for chmod
#include <sys/stat.h> // for chmod



void outputTree(const char * const filename, 
		const BinTree & t)
{
  ofstream outf(filename,ios::out);

  if (!outf)
    {
      cerr << "Error: could not open " << filename << " for output" << endl;
      exit(1);
    }

  t.dotPrint(outf);

  outf.close();

  chmod(filename,0755);

  cout << "\nCheck http://blackseal.pc.cis.udel.edu/cgi-bin/webdot/http://www.udel.edu/CIS/220/pconrad/06J/lect/08.01/bintree.03/" << filename << ".png for the graphical output" << endl;
  
}

int main(void)
{


  BinTree t1(1);
  outputTree("t1.dot",t1);

  BinTree t2(1,
	     new BinTree(2),
	     new BinTree(3));

  outputTree("t2.dot",t2);

	     
  BinTree t3(1,
	     new BinTree(2,
			 new BinTree(3, 
				     new BinTree(4),
				     NULL),
			 new BinTree(5,
				     NULL,
				     new BinTree(6,
						 new BinTree(7),
						 new BinTree(8)
						 )
				     )
			 ),
	     new BinTree(9)
	     );

  outputTree("t3.dot",t3);

  // outputTree also works on anonymous objects, i.e. temporary objects

  outputTree("t4.dot",
	     BinTree (1,
	      new BinTree(2, NULL, new BinTree(3) ),
		      new BinTree(4, new BinTree(5), NULL)));

}




