-1

Hey guys so im trying to implement a templated BST, I've gotten quite far but im having issue doing comparisons in terms of the data. So i cant insert, delete or do comparisons. After doing research i realised I have to overload some functions in order to do this but I'm not sure if my implementation of my BST and a date class is suffice. My code is below:

BST.h

using namespace std;


template <class T>


class BST
{
private:

 template <typename T>
 struct Node
{
    ///T: Template for the dataBST();
    T data;
        ///leftChild: Node pointing to the left
    Node* leftChild;
        ///rightChild: Node pointing to the right child
    Node* rightChild;
};

public:

Node* root;

BST();


bool isEmpty() const;


void print_inorder();


void inorder(Node* nod);


void print_preorder();


void preorder(Node* nod);


void print_postorder();


void postorder(Node* nod);


void removeNode(T d);

void insertNode(T d);
};

BST.CPP

#include "BST.h"

template<class T>
BST<T>::BST()
{
    root=NULL;
}

template<class T>
bool BST<T>::isEmpty() const
{
    return root == NULL;
}

template<class T>
void BST<T>::insertNode(T d)
{
    Node* nod = new Node;
    Node* parent;
    nod->data = d;
    nod->leftChild = NULL;
    nod->rightChild = NULL;
    parent = NULL;
    //Empty tree
    if(isEmpty())
        root = nod;
    else
    {
        Node* curr;//Create reference to current node
        curr = root;
        // Find the Node's parent
        while(curr)
        {
            parent = curr;
            if(nod->data > curr->data)
                curr = curr->rightChild;
            else
                curr = curr->leftChild;
        }

        if(nod->data < parent->data)
            parent->leftChild = nod;
        else
            parent->rightChild = nod;
    }
}

template<class T>
void BST<T>::removeNode(T d)
{
    //Checks if data is found
    bool found = false;
    if(isEmpty())
    {
        cout<<" This Tree is empty! "<<endl;
        return;
    }
    Node* curr;
    Node* parent;
    curr = root;
    parent = (Node*)NULL;
    while(curr != NULL)
    {
        if(curr->data == d)
        {
            found = true;
            break;
        }
        else
        {
            parent = curr;
            if(d>curr->data)
                curr = curr->rightChild;
            else
                curr = curr->leftChild;
        }
    }
    if(!found)
    {
        cout<<" Data not found! "<<endl;
        return;
    }

    //Removal Process
    //1.)We're looking at a leaf node
    if( curr->leftChild == NULL && curr->rightChild == NULL)
    {
        if (parent == NULL)
        {
            delete curr;

        } else
            if(parent->leftChild == curr) parent->leftChild = NULL;
            else parent->rightChild = NULL;
            delete curr;
            return;
    }

    //2.)Parent with Single child node
    if((curr->leftChild == NULL && curr->rightChild != NULL)||
    (curr->leftChild != NULL && curr->rightChild == NULL))
    {
        if(curr->leftChild == NULL && curr->rightChild != NULL)
        {
            if(parent->leftChild == curr)
            {
                parent->leftChild = curr->rightChild;
                delete curr;
            }
            else
            {
                parent->rightChild = curr->rightChild;
                delete curr;
            }
        }
        else // left child present, no right child
        {
            if(parent->leftChild == curr)
            {
                parent->leftChild = curr->leftChild;
                delete curr;
            }
            else
            {
                parent->rightChild = curr->leftChild;
                delete curr;
            }
        }
        return;
    }

    //3.) Node with 2 children
    // replace node with smallest value in right subtree
    if (curr->leftChild != NULL && curr->rightChild != NULL)
    {
        Node* chkr;
        chkr = curr->rightChild;
        if((chkr->leftChild == NULL) && (chkr->rightChild == NULL))
        {
            curr = chkr;
            delete chkr;
            curr->rightChild = NULL;
        }
        else // right child has children
        {
            //if the node's right child has a left child
            // Move all the way down left to locate smallest element

            if((curr->rightChild)->leftChild != NULL)
            {
                Node* lcurr;
                Node* lcurrp;
                lcurrp = curr->rightChild;
                lcurr = (curr->rightChild)->leftChild;
                while(lcurr->leftChild != NULL)
                {
                    lcurrp = lcurr;
                    lcurr = lcurr->leftChild;
                }
                curr->data = lcurr->data;
                delete lcurr;
                lcurrp->leftChild = NULL;
            }
            else
            {
                Node* tmp;
                tmp = curr->rightChild;
                curr->data = tmp->data;
                curr->rightChild = tmp->rightChild;
                delete tmp;
            }

        }
        return;
    }

}

template<class T>
void BST<T>::inorder(Node* p)
{
    if(p != NULL)
    {
        if(p->leftChild)
            inorder(p->leftChild);
        cout<<" "<<p->data<<" ";
        if(p->rightChild)
            inorder(p->rightChild);
    }
    else return;
}

template<class T>
void BST<T>::print_inorder()
{
    inorder(root);
}

template<class T>
void BST<T>::preorder(Node* p)
{
    if(p != NULL)
    {
        cout<<" "<<p->data<<" ";
        if(p->leftChild)
            preorder(p->leftChild);
        if(p->rightChild)
            preorder(p->rightChild);
    }
    else return;
}

template<class T>
void BST<T>::print_preorder()
{
    preorder(root);
}

template<class T>
void BST<T>::postorder(Node* p)
{
    if(p != NULL)
    {
        if(p->leftChild)
            postorder(p->leftChild);
        if(p->rightChild)
            postorder(p->rightChild);
        cout<<" "<<p->data<<" ";
    }
    else return;
}

template<class T>
void BST<T>::print_postorder()
{
    postorder(root);
}

Date.h

//DATE.h
#ifndef DATE_H
#define DATE_H


#include <iostream>
#include <string>


using namespace std;



class Date {
public:



  Date();


  Date(unsigned da, unsigned mon, unsigned yr, unsigned h, unsigned m);


  unsigned GetDay() const;
  unsigned GetMonth() const;
  unsigned GetYear() const;
  unsigned GetHour() const;
  unsigned GetMinute() const;


  void SetDay(unsigned da);
  void SetMonth(unsigned mo);
  void SetYear(unsigned yr);
  void SetHour(unsigned h);
  void SetMinute(unsigned m);

private:

    unsigned Day;
    unsigned Month;
    unsigned Year;
    unsigned Hour;
    unsigned Minute;

};
istream & operator >>( istream & input, Date & D );
#endif // Date_H

Date.cpp

// Date.CPP - Date class implementation

#include "Date.h"

Date::Date()
{
    Day=0;
    Month=0;
    Year=0;
    Hour=0;
    Minute=0;
}

Date::Date( unsigned da, unsigned mon, unsigned yr, unsigned h, unsigned m)
{
    Day=da;
    Month=mon;
    Year=yr;
    Hour=h;
    Minute=m;
}

  unsigned Date::GetDay() const
  {
    return Day;
  }

  unsigned Date::GetMonth() const
  {
    return Month;
  }

  unsigned Date::GetYear() const
  {
        return Year;
  }

  unsigned Date::GetHour() const
  {
        return Hour;
  }

  unsigned Date::GetMinute() const
  {
      return Minute;
  }

  void Date::SetDay(unsigned da)
  {
      Day=da;
  }

  void Date::SetMonth(unsigned mo)
  {
      Month=mo;
  }

  void Date::SetYear(unsigned yr)
  {
      Year=yr;
  }

  void Date::SetHour(unsigned h)
  {
      Hour=h;
  }

void Date::SetMinute(unsigned m)
  {
      Minute=m;
  }

istream & operator >>( istream & input, Date & D )
{

    string tempDay, tempMonth, tempYear, tempHour, tempMinute;
    string garbage;
    unsigned da, mon, yr, h, m;
    getline(input,tempDay,'/');
    da=stoi(tempDay,nullptr,0);
    getline(input,tempMonth,'/');
    mon=stoi(tempMonth,nullptr,0);
    getline(input,tempYear,' ');
    yr=stoi(tempYear,nullptr,0);
    getline(input,tempHour,':');
    h=stoi(tempHour,nullptr,0);
    getline(input,tempMinute,',');
    m=stoi(tempMinute,nullptr,0);
    getline(input,garbage);
    D.SetDay(da);
    D.SetMonth(mon);
    D.SetYear(yr);
    D.SetHour(h);
    D.SetMinute(m);
    cout << D.GetYear() << D.GetMonth() << D.GetDay() << D.GetHour() << D.GetMinute()<< endl;

    return input;
}

/*ostream & operator <<( ostream & os, const Date & D )
{
    os << "  Date:" << D.GetDate();

    return os;
}*/

with this implementation I can create a BST of type int or float or strings and its fine but if I add a class like Date i get errors when I try and compare the data. Any help will be appreciated THANKS :)

Zayd Bhyat
  • 101
  • 1
  • 2
  • 19

1 Answers1

0

Your Date class needs some comparison operators. The C++ language does not automatically provide these for every structure or class.

struct Date
{
  unsigned int Year;
  unsigned int Month;
  unsigned int Day;
  bool operator==(const Date& other) const
  {
    return (Year == other.Year)
         && (Month == other.Month)
         && (Day == other.Day);
  }
  bool operator< (const Date& other) const
  {
     if (Year != other.Year)
     {
         return Year < other.Year;
     }
     if (Month != other.Month)
     {
         return Month < other.Month;
     }
     return Day < other.Day;
  }
};

Other Comparisons (ordering)

You can define other comparison operators in terms of the equality and less than operators:

bool operator!=(const Date& d) const { return !(*this == d);}
bool operator<=(const Date& d) const { return (*this == d) || (*this < d); }
bool operator>=(const Date& d) const { return !(*this < d); }
bool operator> (const Date& d) const ( return !(*this <= d); }
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154