//Written by: Glib Smaga. Last modified: Feb 13th, 2008

//---------------------------------------------------------

//This function will print out a tree in a readable form

//There a public and private function (no prarmeters is public).

//Don't forget to make prototypes for them in *.h file

//Also, my class is names Tree, so if you have it set different way

//          dont forget to change that as well.

 

//I highly recommend using full screen mode, so there will be enough room

//to print out all the nodes.

 

/*IMPORTANT:

When compiling you would have to add '-l ncurses' to you makefile.

      Ex.   g++ -c Tree.cpp

            g++ -o tree Tree.o  main.cpp -lncurses

           

*/

 

//Note that i'm using get methods (getData, getRight and getLeft)

/* Here they are for your convenience:

int Node::getData(){

      return data;

}

 

Node* Node::getRight(){

      return right;

}

 

 

Node* Node::getLeft(){

      return left;

}

*/

 

 

#include <ncurses.h>

 

void Tree::print(){

      initscr();

      refresh();

      print(root, 0, 40, 40);

      getch();

      endwin();

}

 

void Tree::print(Node* curr, int lvl, int prevlvl, int where){

      if(lvl==0){

            move(1,5);

            cout<<"It is highy recommended to use it in full screen mode. PRESS ANY KEY TO QUIT."<<endl;

            refresh();

            move(1,60);

            refresh();

            cout<<"("<<root->getData()<<")";

            if(root->getRight()!=NULL)

                  print(root->getRight(), 1, 40, 80);

            if(root->getLeft()!=NULL)

                  print(root->getLeft(), 1, 40, 40);

      }

      else{

            move(lvl*2,where);

            refresh();

            cout<<"("<<curr->getData()<<")";

            lvl++;

            if(curr->getRight()!=NULL){

                  print(curr->getRight(), lvl, where, (where+(20/lvl)));

            }

            if(curr->getLeft()!=NULL){

                  print(curr->getLeft(), lvl, where, (where-(20/lvl)));

            }

      }

}