#ifndef CA_UWATERLOO_ALUMNI_DWHARDER_BINARY_SERACH_TREE #define CA_UWATERLOO_ALUMNI_DWHARDER_BINARY_SERACH_TREE // Author: Douglas Wilhelm Harder // Copyright (c) 2011 by Douglas Wilhelm Harder. All rights reserved. #include "Binary_search_node.h" #include "Exception.h" namespace Data_structures { template class Binary_search_tree { protected: Binary_node *root_node; public: Binary_search_tree(); Binary_search_node *root() const; bool empty() const; int size() const; int height() const; Type front() const; Type back() const; bool find( const Type & ) const; Type next( const Type & ) const; Type previous( const Type & ) const; Type at( int ) const; Type operator[]( int ) const; void clear(); bool insert( Type const & ); bool erase( Type const & ); }; template Binary_search_tree::Binary_search_tree(): root_node( 0 ) { // Empty constructor } template Binary_search_node *Binary_search_tree::root() const { return reinterpret_cast *>( root_node ); } template bool Binary_search_tree::empty() const { return ( root() == 0 ); } template int Binary_search_tree::size() const { return empty() ? 0 : root()->size(); } template int Binary_search_tree::height() const { return empty() ? -1 : root()->height(); } template Type Binary_search_tree::front() const { if ( empty() ) { throw underflow(); } return root()->front(); } template Type Binary_search_tree::back() const { if ( empty() ) { throw underflow(); } return root()->back(); } template bool Binary_search_tree::find( Type const &obj ) const { return empty() ? false : root()->find( obj ); } template Type Binary_search_tree::at( int n ) const { if ( n < 0 || n >= size() ) { return Type(); } return root()->at( n ); } template Type Binary_search_tree::operator[]( int n ) const { return root()->at( n ); } template Type Binary_search_tree::next( Type const &obj ) const { if ( empty() ) { throw underflow(); } return root()->next( obj ); } template Type Binary_search_tree::previous( Type const &obj ) const { if ( empty() ) { throw underflow(); } return root()->previous( obj ); } template void Binary_search_tree::clear() { if ( !empty() ) { root()->clear(); } } template bool Binary_search_tree::insert( Type const &obj ) { if ( empty() ) { root_node = new Binary_search_node( obj ); return true; } return root()->insert( obj ); } template bool Binary_search_tree::erase( Type const &obj ) { return empty() ? false : root()->erase( obj, root_node ); } } #endif