C++ 模板化BST问题c++;

C++ 模板化BST问题c++;,c++,templates,binary-search-tree,C++,Templates,Binary Search Tree,嘿,伙计们,我正在尝试实现一个模板化的BST,我已经做了很多,但我在数据方面做比较时遇到了问题。因此,我无法插入、删除或进行比较。在做了研究之后,我意识到为了做到这一点,我必须重载一些函数,但我不确定我的BST和date类的实现是否足够。我的代码如下: 英国夏令时 使用名称空间std; 模板 BST级 { 私人: 模板 结构体类型 { ///T:数据库()的模板; T数据; ///leftChild:指向左侧的节点 节点*leftChild; ///rightChild:指向右侧子节点的节点 节

嘿,伙计们,我正在尝试实现一个模板化的BST,我已经做了很多,但我在数据方面做比较时遇到了问题。因此,我无法插入、删除或进行比较。在做了研究之后,我意识到为了做到这一点,我必须重载一些函数,但我不确定我的BST和date类的实现是否足够。我的代码如下:

英国夏令时

使用名称空间std;
模板
BST级
{
私人:
模板
结构体类型
{
///T:数据库()的模板;
T数据;
///leftChild:指向左侧的节点
节点*leftChild;
///rightChild:指向右侧子节点的节点
节点*右子节点;
};
公众:
节点*根;
BST();
bool isEmpty()常量;
作废打印顺序();
按顺序无效(节点*节点);
无效打印_预订单();
无效预订单(节点*节点);
作废打印_postorder();
作废后订单(节点*节点);
脱空节点(td);
void-insertNode(td);
};
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);
}
#包括“BST.h”
模板
BST::BST()
{
root=NULL;
}
模板
boolbst::isEmpty()常量
{
返回root==NULL;
}
模板
void BST::insertNode(td)
{
Node*nod=新节点;
节点*父节点;
nod->data=d;
nod->leftChild=NULL;
nod->rightChild=NULL;
parent=NULL;
//空树
if(isEmpty())
根=节点;
其他的
{
Node*curr;//创建对当前节点的引用
curr=根;
//查找节点的父节点
while(curr)
{
父项=当前值;
如果(节点->数据->当前->数据)
curr=curr->rightChild;
其他的
curr=curr->leftChild;
}
如果(节点->数据<父节点->数据)
父节点->左子节点=节点;
其他的
父节点->右子节点=节点;
}
}
模板
void BST::removeNode(td)
{
//检查是否找到数据
bool-found=false;
if(isEmpty())
{
库奇尔德;
其他的
curr=curr->leftChild;
}
}
如果(!找到)
{
coutrightChild)->leftChild!=NULL)
{
节点*lcurr;
节点*lcurrp;
lcurrp=curr->rightChild;
lcurr=(curr->rightChild)->leftChild;
while(lcurr->leftChild!=NULL)
{
lcurrp=lcurr;
lcurr=lcurr->leftChild;
}
当前->数据=当前->数据;
删除lcurr;
lcurrp->leftChild=NULL;
}
其他的
{
节点*tmp;
tmp=curr->rightChild;
当前->数据=tmp->数据;
curr->rightChild=tmp->rightChild;
删除tmp;
}
}
返回;
}
}
模板
void BST::索引(节点*p)
{
如果(p!=NULL)
{
if(p->leftChild)
顺序(p->leftChild);
coutrightChild);

CUT< P>您的代码>日期< /Cord>类需要一些比较运算符。C++语言不自动为每个结构或类提供这些。

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;
  }
};
结构日期 { 未签名整数年; 未签名整数月; 未签名整数日; 布尔运算符==(常量日期和其他)常量 { 回报率(年份==其他年份) &&(月份==其他月份) &&(日==其他日期); } bool操作员<(施工日期和其他)施工 { 如果(年)=其他年) { 回归年<其他年份; } 如果(月)=其他月份) { 返回月份<其他月份; } 返回日<其他日; } };
其他比较(排序) 您可以根据相等运算符和小于运算符定义其他比较运算符:

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); }
bool操作符!=(const Date&d)const{return!(*this==d);}
布尔运算符=(const Date&d)const{return!(*thisbool operator>(const Date&d)const(return!)(*如果您有编译器错误,您应该将这些错误包括在问题中。您也会遇到好的问题。因此,其中一个错误是:
|64 |错误:与“operator==”不匹配(操作数类型为“Date”和“Date”)|
您认为该错误试图告诉您什么?我无法插入、删除或与date类进行比较啊,这是另一个错误。typename不能与template类相同。为了使我的BST有效工作,我需要更改什么?谢谢Tom,我今天将尝试此put
// 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;
}*/
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;
  }
};
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); }