#ifndef MYBINARYTREE_H
#define MYBINARYTREE_H
template <class T>
class BinaryTree
{
protected:
struct TNode
{
T val;
TNode* parent;
TNode* left;
TNode* right;
TNode(T t, TNode* p = 0, TNode* l = 0, TNode* r = 0):val(t), parent(p), left(l), right(r)
{
if (!parent) parent = this;
}
};
private:
TNode* _root;
public:
class TIterator
{
BinaryTree* tree;
TNode* node;
public:
friend class BinaryTree;
TIterator():tree(0), node(0) {}
TIterator(const TIterator& it):tree(it.tree), node(it.node) {}
TIterator(BinaryTree* t, TNode* n = 0):tree(t), node(n) {}
TIterator& operator = (const TIterator& it)
{
tree = it.tree;
node = it.node;
return *this;
}
bool operator == (const TIterator& it) {return (tree == it.tree && node == it.node);}
bool operator != (const TIterator& it) {return !(*this == it);}
TIterator& operator ++ ()
{
node = preorderSuccessor(node);
return *this;
}
TIterator operator ++ (int)
{
TIterator it(*this);
node = preorderSuccessor(node);
return it;
}
T& operator *() {return node->val;}
TNode* operator ->() {return node;}
const T& operator *() const {return node->val;}
const TNode* operator ->() const {return node;}
bool operator !() {return node == 0;}
protected:
};
BinaryTree():_root(0) {}
BinaryTree(const BinaryTree& b)
{
_root = clone(b._root, 0);
}
BinaryTree(const T& t):_root(new TNode(t)) {_root->parent = _root;}
BinaryTree(const T& t, const
自己动手写数据结构:二叉树BinaryTree类模板C++实现(功能较全)
最新推荐文章于 2024-04-10 11:10:32 发布