题目
成员函数,使用栈的非递归算法,其他成员函数(构造或遍历)采用递归算法
6-12 bool operator==(BinaryTree<T> &bitree)//重载==,比较两棵二叉树是否相等
实验原始纪录
template <class T>
bool BinaryTree<T>::operator==(BinaryTree<T> &bitree) //重载==,比较两棵二叉树是否相等
{
LinkedStack<BinaryNode<T>*> a;
BinaryNode<T> *p = this->root;
LinkedStack<BinaryNode<T>*> b;
BinaryNode<T> *q = bitree.root;
while( p != NULL || q != NULL || !a.empty() || !b.empty() )
{
if( p != NULL && q != NULL )
{
if( p->data != q->data )
return false;
a.push(p);
b.push(q);
p = p->left;
q = q->left;
}
else
{
if( p == NULL && q == NULL )
{
p=a.pop();
q=b.pop();
p=p->right;
q=q->right;
}
else
return false;
}
}
return true;
}
一个在努力中的未来程序员,如果有更好的算法,欢迎评论。