/*
  Travis Gadberry
  Patrick Hesser
  Chris Ladewig

  BalancedBinaryTree.h
  21Mar05
*/

#ifndef BALANCED_BINARY_TREE_H
#define BALANCED_BINARY_TREE_H

        #include "dsexceptions.h"
        #include "QuickSort.h"
        #include <iostream>       // For NULL
        #include <time.h>
        #include <vector>

        using namespace std;

          // Binary node and forward declaration because g++ does
          // not understand nested classes.
        template <class Comparable>
        class BalancedBinaryTree;

        template <class Comparable>
        class BinaryNode
        {
            Comparable element;
            BinaryNode *left;
            BinaryNode *right;

            BinaryNode( const Comparable & theElement, BinaryNode *lt, BinaryNode *rt )
              : element( theElement ), left( lt ), right( rt ) { }
            friend class BalancedBinaryTree<Comparable>;
        };


        // BalancedBinaryTree class
        //
        // CONSTRUCTION: with ITEM_NOT_FOUND object used to signal failed finds
        //
        // ******************PUBLIC OPERATIONS*********************
        // void insert( x )       --> Insert x
        // void remove( x )       --> Remove x
        // Comparable find( x )   --> Return item that matches x
        // Comparable findMin( )  --> Return smallest item
        // Comparable findMax( )  --> Return largest item
        // boolean isEmpty( )     --> Return true if empty; else false
        // void makeEmpty( )      --> Remove all items
        // void printTree( )      --> Print tree in sorted order

        template <class Comparable>
        class BalancedBinaryTree
        {
          public:
            explicit BalancedBinaryTree( const Comparable & notFound );
            BalancedBinaryTree( const BalancedBinaryTree & rhs );
            ~BalancedBinaryTree( );

            const Comparable & findMin( ) const;
            const Comparable & findMax( ) const;
            const Comparable & find( const Comparable & x );
            bool isEmpty( ) const;
            void printTree( ) const;
			void BalancedTreeRecursion(int* A, int P, int Q);

            void makeEmpty( );
            void insert( const Comparable & x );
            void remove( const Comparable & x );
            int getIncrements();
            double getCreationTime();
            double getSearchTime();
            void insertIntArray(int *, int);
            void insertIntVector(const vector<int>);
            void resetIncrements();
            void rotateRight(BinaryNode<Comparable> *);
            void rotateLeft(BinaryNode<Comparable> *);
            int getHeight(BinaryNode<Comparable> *);
            void balance(BinaryNode<Comparable> *);

            const BalancedBinaryTree & operator=( const BalancedBinaryTree & rhs );

          private:
            BinaryNode<Comparable> *root;
            const Comparable ITEM_NOT_FOUND;

            const Comparable & elementAt( BinaryNode<Comparable> *t ) const;

            void insert( const Comparable & x, BinaryNode<Comparable> * & t );
            void remove( const Comparable & x, BinaryNode<Comparable> * & t ) const;
            BinaryNode<Comparable> * findMin( BinaryNode<Comparable> *t ) const;
            BinaryNode<Comparable> * findMax( BinaryNode<Comparable> *t ) const;
            BinaryNode<Comparable> * find( const Comparable & x, BinaryNode<Comparable> *t );
            void makeEmpty( BinaryNode<Comparable> * & t ) const;
            void printTree( BinaryNode<Comparable> *t ) const;
            BinaryNode<Comparable> * clone( BinaryNode<Comparable> *t ) const;

			int increments;
			double creationTime, searchTime;
        };


#endif

