目录
1、二叉树的克隆操作
二叉树的克隆操作
-SharedPointer< BTree<T> > clone() const
● 克隆当前树的一份拷贝
● 返回值为堆空间中的一棵新二叉树(与当前树相等)
二叉树的克隆
-定义功能: clone(node)
功能: 拷贝 node 为根结点的二叉树(数据元素在对应位置相等)
编程实验
二叉树的克隆 clone()
BTree.h
template < typename T >
class BTree : public Tree<T>
{
protected:
// ...
BTreeNode<T>* clone(BTreeNode<T>* node) const
{
BTreeNode<T>* ret = NULL;
if(node != NULL)
{
ret = BTreeNode<T>::NewNode();
if(ret != NULL)
{
ret->value = node->value;
ret->left = clone(node->left); // 递归克隆左子树,右子树
ret->right = clone(node->right);
if(ret->left != NULL)
{
ret->left->parent = ret;
}
if(ret->right != NULL)
{
ret->right->parent = ret;
}
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new node ...");
}
}
return ret;
}
// ...
public:
// ...
SharedPointer< BTree<T> > clone() const
{
BTree<T>* ret = new BTree<T>();
if(ret != NULL)
{
ret->m_root = clone(root());
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new tree ...");
}
return ret;
}
// ...
};
main.cpp
#include <iostream>
#include "BTree.h"
using namespace std;
using namespace DTLib;
int main()
{
BTree<int> bt;
BTreeNode<int>* n = NULL;
bt.insert(1, NULL);
n = bt.find(1);
bt.insert(2, n);
bt.insert(3, n);
n = bt.find(2);
bt.insert(4, n);
bt.insert(5, n);
n = bt.find(4);
bt.insert(8, n);
bt.insert(9, n);
n = bt.find(5);
bt.insert(10, n);
n = bt.find(3);
bt.insert(6, n);
bt.insert(7, n);
SharedPointer< BTree<int> > sp = bt.clone();
cout << "Clone : " << endl;
for((*sp).begin(); !(*sp).end(); (*sp).next())
{
cout << (*sp).current() << " ";
}
cout << endl << endl;
cout << "Old BTree: " << endl;
for(bt.begin(); !bt.end(); bt.next())
{
cout << bt.current() << " ";
}
cout << endl << endl;
return 0;
}
2、二叉树比较操作的定义
二叉树比较操作的定义
-判断两棵二叉树中的数据元素是否对应相等
bool operator == (const BTree<T>& btree)
bool operator ! = (const BTree<T>& btree)
二叉树的比较
-定义功能: equal(lh , rh)
功能: 判断 lh 为根结点的二叉树与 rh 为根结点的二叉树是否相等
编程实验
二叉树的相等比较 equal()
BTree.h
template < typename T >
class BTree : public Tree<T>
{
protected:
// ...
bool equal(BTreeNode<T>* lh, BTreeNode<T>* rh)
{
if(lh == rh)
{
return true;
}
else if((lh != NULL) && (rh != NULL))
{
return (lh->value == rh->value) && (equal(lh->left, rh->left)) && (equal(lh->right, rh->right));
}
else
{
return false;
}
}
public:
// ...
bool operator == (BTree<T>& btree)
{
return equal(root(), btree.root());
}
bool operator != (BTree<T>& btree)
{
return !(*this == btree);
}
// ...
};
main.cpp
cout << "*sp == bt : " << (*sp == bt) << endl;
3、二叉树的相加操作
二叉树的相加操作
-SharedPointer< BTree<T> > add( const BTree<T> & btree) const
● 将当前二叉树与参数 btree 中的数据元素在对应位置处相加
● 返回值(相加的结果)为堆空间中的一棵新二叉树
二叉树的加法
-定义功能: add(lh, rh)
功能: 将 lh 为根结点的二叉树与 rh 为根结点的二叉树相加
编程实验
二叉树的相加 add()
BTree.h
template < typename T >
class BTree : public Tree<T>
{
protected:
// ...
BTreeNode<T>* add(BTreeNode<T>* lh,BTreeNode<T>* rh) const
{
BTreeNode<T>* ret = NULL;
if((lh == NULL)&&(rh != NULL))
{
ret = clone(rh);
}
else if((lh != NULL)&&(rh == NULL))
{
ret = clone(lh);
}
else if((lh != NULL)&&(rh != NULL))
{
ret = BTreeNode<T>::NewNode();
if(ret != NULL)
{
ret->value = lh->value + rh->value;
ret->left = add(lh->left, rh->left);
ret->right = add(lh->right, rh->right);
if(ret->left != NULL)
{
ret->left->parent = ret;
}
if(ret->right != NULL)
{
ret->right->parent = ret;
}
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new node ...");
}
}
return ret;
}
public:
// ...
SharedPointer< BTree<T> > add(const BTree<T>& btree) const
{
BTree<T>* ret = new BTree<T>();
if(ret != NULL)
{
ret->m_root = add(root(), btree.root());
}
else
{
THROW_EXCEPTION(NoEnoughMemoryException, "No memory to create new tree ...");
}
return ret;
}
};
main.cpp
#include <iostream>
#include "BTree.h"
using namespace std;
using namespace DTLib;
int main()
{
BTree<int> bt;
BTreeNode<int>* n = NULL;
bt.insert(1, NULL);
n = bt.find(1);
bt.insert(2, n);
bt.insert(3, n);
n = bt.find(2);
bt.insert(4, n);
bt.insert(5, n);
n = bt.find(4);
bt.insert(8, n);
bt.insert(9, n);
n = bt.find(5);
bt.insert(10, n);
n = bt.find(3);
bt.insert(6, n);
bt.insert(7, n);
cout << "Old BTree: " << endl;
for(bt.begin(); !bt.end(); bt.next())
{
cout << bt.current() << " ";
}
cout << endl << endl;
BTree<int> nbt;
nbt.insert(0, NULL);
n = nbt.find(0);
nbt.insert(6, n);
nbt.insert(2, n);
n = nbt.find(2);
nbt.insert(7, n);
nbt.insert(8, n);
SharedPointer< BTree<int> > r = bt.add(nbt);
cout<<"Other Tree : "<<endl;
for(nbt.begin(); !nbt.end(); nbt.next())
{
cout << nbt.current() << " ";
}
cout << endl << endl;
cout << "Add Tree : "<<endl;
for((*r).begin(); !(*r).end(); (*r).next())
{
cout << (*r).current() << " ";
}
return 0;
}
小结
比较操作判断两棵二叉树中的数据元素是否对应相等
克隆操作将当前二叉树在堆空间中进行复制
相加操作将两棵二叉树中的数据元素在对应位置处相加
相加操作的结果保存在堆空间的一棵二叉树中