I implemented a binary search tree with methods of insert, search, size and print using the <<
operator. All the methods works with template
. main
is a simple demonstration of the methods and templates working correctly. Please also review the code formatting.
Node.h
#pragma once
#ifndef Node_h
#define Node_h
template < class T >
class Node
{
public :
Node ( T val ) : m_val( val ) , m_left( nullptr ) , m_right( nullptr ){}
T m_val;
Node<T> *m_left;
Node<T> *m_right;
};
#endif
BST.h
#ifndef BST_h
#define BST_h
#include "Node.h"
#include <iostream>
template < class T >
class BST;
template < class T >
std::ostream &operator<< ( std::ostream &os , const BST<T> ¤t ) ;
template < class T >
class BST
{
public :
BST ();
~BST ();
void insertBST ( const T &val );
void insertBST ( Node <T>*cuurent , const T &val );
void destroyBST ( Node <T>*current );
bool searchVal ( const T &val ) const;
bool searchVal ( Node<T> *current , const T &val ) const;
int sizeBST ( ) const;
int sizeBST ( const Node <T> *current ) const;
void inorderBST () const ;
void inorderBST ( const Node <T>*current ) const;
friend std::ostream &operator<< <>( std::ostream &os , const BST ¤t ) ;
private :
Node <T>*m_root;
};
template < class T >
std::ostream &operator<< ( std::ostream &os , const BST<T> ¤t )
{
current.inorderBST();
return os;
}
template < class T >
BST<T>::BST ()
{
m_root = nullptr;
}
template < class T >
BST<T>::~BST ()
{
destroyBST( m_root );
delete m_root;
}
template < typename T >
void BST<T>::insertBST ( Node <T>*current , const T &val )
{
if ( current->m_val > val )
{
if( current->m_left == nullptr )
{
current->m_left = new Node<T>( val );
}
else
{
insertBST( current->m_left , val );
}
}
else
{
if( current->m_right == nullptr )
{
current->m_right = new Node<T>( val );
}
else
{
insertBST( current->m_right , val );
}
}
}
template < typename T >
void BST<T>::insertBST ( const T &val )
{
if ( m_root == nullptr )
{
m_root = new Node<T>( val );
}
else
{
insertBST( m_root , val );
}
}
template < typename T >
void BST<T>::destroyBST( Node <T>*current )
{
if ( current->m_left != nullptr )
{
destroyBST( current->m_left );
delete current->m_left;
}
if ( current->m_right != nullptr )
{
destroyBST( current->m_right );
delete current->m_right;
}
}
template < typename T >
bool BST<T>::searchVal ( const T &val ) const
{
bool b ;
if ( m_root == nullptr)
{
return false;
}
else
{
b = searchVal(m_root,val);
}
return b ;
}
template < typename T >
bool BST<T>::searchVal ( Node <T> *current, const T &val ) const
{
if( current->m_val == val )
{
return true ;
}
else
{
bool right , left ;
right = left = false;
if ( current->m_right != nullptr )
{
right = searchVal( current->m_right , val );
}
if ( current->m_left != nullptr )
{
left = searchVal( current->m_left , val );
}
if ( right || left )
{
return true;
}
else
{
return false ;
}
}
}
template < typename T >
int BST<T>::sizeBST () const
{
if( m_root == nullptr )
{
return -1 ;
}
else
{
return ( sizeBST ( m_root ) );
}
}
template < typename T >
int BST<T>::sizeBST ( const Node <T> *current ) const
{
if ( current == nullptr )
{
return 0 ;
}
int left , right ;
left = sizeBST(current->m_left);
right = sizeBST(current->m_right);
return ( left > right ? left + 1 : right + 1 );
}
template < typename T >
void BST<T>::inorderBST () const
{
if ( m_root == nullptr )
{
std::cout <<"The tree is empty \n" ;
}
else
{
inorderBST ( m_root );
std::cout<<"\n";
}
}
template < typename T >
void BST<T>::inorderBST ( const Node <T> *current ) const
{
if ( current != nullptr )
{
inorderBST ( current->m_left );
std::cout<< current->m_val << " " ;
inorderBST ( current->m_right );
}
}
#endif
main.cpp
#include "BST.h"
#include <iostream>
int main ()
{
BST <int> testInt;
int tempInt;
std::cout << "Please enter values to BST type int ( enter -1 to stop and print inorder and size) \n";
std::cin >> tempInt;
while ( tempInt != -1 )
{
testInt.insertBST( tempInt );
std::cin >> tempInt;
}
std::cout << testInt ;
tempInt = testInt.sizeBST();
std::cout << "Tree size is " << tempInt << "\n" ;
std::cout << "Please enter value you want to search (enter -1 to stop)\n";
std::cin >>tempInt;
while ( tempInt != -1 )
{
bool tempSearch = testInt.searchVal( tempInt );
if ( tempSearch)
{
std::cout << "The value exsit in the tree ! \n";
}
else
{
std::cout <<"The value does not exsit in the tree!\n";
}
std::cin >> tempInt;
}
std::cout <<"Congratulations you finished to check INT type lets check double \n";
BST <double> testDouble;
double tempDouble;
std::cout << "Please enter values to BST type double ( enter -1 to stop and print inorder and size) \n";
std::cin >> tempDouble;
while ( tempDouble != -1 )
{
testDouble.insertBST( tempDouble );
std::cin >> tempDouble;
}
std::cout << testDouble ;
tempDouble = testDouble.sizeBST();
std::cout << "Tree size is " << tempDouble << "\n" ;
std::cout << "Please enter value you want to search (enter -1 to stop)\n";
std::cin >>tempDouble;
while ( tempDouble != -1 )
{
bool tempSearch = testDouble.searchVal( tempDouble );
if ( tempSearch)
{
std::cout << "The value exsit in the tree ! \n";
}
else
{
std::cout <<"The value does not exsit in the tree!\n";
}
std::cin >> tempDouble;
}
std::cout <<"Congratulations you finished to check Double type \n";
return 0;
}