模板化BST问题c ++

时间:2016-05-31 12:00:31

标签: c++ templates binary-search-tree

嘿伙计们,所以我试图实现一个模板化的BST,我已经走得很远了,但我在数据方面做了比较的问题。所以我无法插入,删除或进行比较。在做了研究后,我意识到我必须重载一些函数才能做到这一点,但我不确定我的BST和日期类的实现是否足够。我的代码如下:

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;
}*/

使用这个实现我可以创建一个类型为int或float或者字符串的BST并且很好但是如果我添加一个像Date这样的类我在尝试比较数据时会出错。任何帮助将不胜感激 谢谢:)

1 个答案:

答案 0 :(得分:0)

您的@Documented @Constraint(validatedBy = CompanyValidator.class) @Target({ElementType.METHOD, ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) public @interface ValidCompany { String message() default "{validator.company}";//Error message to be displayed if the CompanyValidator isValid returns false Class<?>[] groups() default {}; Class<? extends Payload>[] payload() default {}; } } 课程需要一些比较运算符。 C ++语言不会自动为每个结构或类提供这些。

Date

其他比较(订购)

您可以根据相等性和小于运算符来定义其他比较运算符:

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;
  }
};