求助

qq_29081801 2016-05-08 11:02:23
#include<iostream>
#include<string.h>
using namespace std;

//
template <class T>
class BinaryTreeNode
{
public:
BinaryTreeNode(){LeftChild=RightChild=0;}
BinaryTreeNode(const T& e)
{ data=e;
LeftChild=RightChild=0;
}
BinaryTreeNode(const T& e, BinaryTreeNode * l, BinaryTreeNode *r)
{
data=e;
LeftChild=l;
RightChild=r;
}
T data;
BinaryTreeNode<T> * LeftChild, * RightChild;
};


//
template<class T>
class BinaryTree
{
public:
BinaryTreeNode<T> *root;
BinaryTree(void){
root=NULL;}
~BinaryTree(void){}
bool IsEmpty(void)const
{return ((root)?FALSE:TRUE);}
bool Root(T&x)const;
BinaryTreeNode<T> *MakeTree(const T&element,BinaryTreeNode<T>*left=NULL,BinaryTreeNode<T>*right=NULL);
};

template<class T>
BinaryTreeNode<T> *BinaryTree<T>::MakeTree(const T&element,BinaryTreeNode<T>*left,BinaryTreeNode<T>*right)
{
root=new BinaryTreeNode<T>(element,left,right);
if(root==NULL)
{
cerr<<"Memory allocation failure!\n:";
exit(1);
}
return root;

};
//队列
template<class T>
class Queue
{
private:
int rear, front;
T * elements;
int MaxSize;
public:
Queue(int MaxSize);
~Queue(void){delete [ ] elements;}
int EnQueue(const T& item);
T DeQueue(void);
void MakeEmpty(void){front=rear=0;}
int IsEmpty(void) const {return front==rear;}
int IsFull(void) const{return front==(rear+1)%MaxSize;}
int Length(void) const {return (rear-front+MaxSize)%MaxSize;}
};
template<class T>
Queue<T>::Queue(int s)
{
MaxSize=s;
elements=new T [MaxSize];
front=rear=0;
}
template<class T>
int Queue<T>::EnQueue(const T& item)
{
if(!IsFull())
{
elements[rear]=item;
rear=(rear+1)%MaxSize;
return 0;
}
else return -1;
}
template<class T>
T Queue<T>::DeQueue(void)
{
if(!IsEmpty())
{
T item=elements[front];
front=(front+1)%MaxSize;
return item;
}
else return NULL;
}


//判断完全二叉树
bool is_complete(BinaryTreeNode<int> *root)
{
Queue< BinaryTreeNode <int> > q(100);
BinaryTreeNode *ptr;

q.EnQueue(*root);
while ((ptr = q.DeQueue()) != NULL)
{
q.EnQueue(ptr->left);
q.EnQueue(ptr->right);
}

while (!q.IsEmpty())
{
ptr = q.DeQueue();
if (NULL != ptr)
{
return false;
}
}

return true;
}
...全文
486 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_29081801 2016-05-08
  • 打赏
  • 举报
回复
这是个判断完全二叉树的代码 现在的问题是不知道如何衔接模板类和函数了 就是标红的地方报错 error C2679: binary '=' : no operator defined which takes a right-hand operand of type 'class BinaryTreeNode<int>' (or there is no acceptable conversion) cpp(109) : fatal error C1903: unable to recover from previous error(s); stopping compilation 求大神指教
paschen 2016-05-08
  • 打赏
  • 举报
回复
你BinaryTreeNode 是模板类 所以你的BinaryTreeNode *ptr; 这句要给一个模板参数

5,530

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 模式及实现
社区管理员
  • 模式及实现社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧