//David Powers, djpowers@ecst.csuchico.edu, CSCI 311, 3/28/07

 

 

// all mathematical operations and actions were carried out

//assuming the proper libraries were included in the main file

//that called this function

 

 

 

/**************************************************

the following function is used to count all the nodes

in the tree in a ranked order and then find how many nodes are in the median.

located in the private portion of the tree class.

**************************************************/

 

void inorder(node* root, int &count)

{

      if(root->m_left)

            inorder(root->m_left, count);

      count++;

      if(root->m_right)

            inorder(root->m_right, count);

}

 

 

/*************************************************

the following function goes back through the tree

doing an inorder traversal and prints out the node that is the median.

located in the private portion of the tree class.

*************************************************/

 

void findmedian(node* root, int &count, bool &check)

{

      if(check)

      {

            while(count != 0)

            {

                  if(root->m_left)

                        findmedian(root->m_left, count, check);

                  count--;

           

                  if(count == 0)

                        break;

           

                  if(root->m_right)

                        findmedian(root->m_right, count, check);

            }

      }

      if(count == 0)

      {    

            cout<< root->m_value;

            count = -1;

            check == false;

      }

}

 

 

/************************************************

the following function is the driver for the whole algorithm and is the one that gets called

from main in order to find the median.

located in the public portion of the tree class.

************************************************/

 

void median()

{

      int count;

      bool check = true;

      inorder(m_root, count);

     

      count= count/2 +1;

     

      findmedian(m_root, count, check);

}