#include<iostream>
#include<cstdlib>
#include<cstring>
#include<malloc.h> // malloc free;
#include<stack>
#include<queue>
using namespace std;
template<class Type>
const Type & Max(const Type &a, const Type &b)
{
return a>b ? a : b;
}
template<class Type>
class BinaryTree
{
private:
struct BtNode
{
BtNode *leftchild;
BtNode *rightchild;
Type data;
};
private:
BtNode *root;
Type RefValue; // #define END '#'
static BtNode * _Buynode()
{
BtNode *s = (BtNode *)malloc(sizeof(BtNode));
if (NULL == s) exit(1);
memset(s, 0, sizeof(BtNode));
return s;
}
static void _Freenode(BtNode *p)
{
free(p);
}
BtNode * Create(Type *&str)
{
BtNode * s = NULL;
if (*str != RefValue)
{
s = _Buynode();
s->data = *str;
s->leftchild = Create(++str);
s->rightchild = Create(++str);
}
return s;
}
static void PreOrder(BtNode *ptr)
{
if (ptr != NULL)
{
cout << ptr->data << " ";
PreOrder(ptr->leftchild);
PreOrder(ptr->rightchild);
}
}
static void InOrder(BtNode *ptr)
{
if (ptr != NULL)
{
InOrder(ptr->leftchild);
cout << ptr->data << " ";
InOrder(ptr->rightchild);
}
}
static void PastOrder(BtNode *ptr)
{
if (ptr != NULL)
{
PastOrder(ptr->leftchild);
PastOrder(ptr->rightchild);
cout << ptr->data << " ";
}
}
static void NiceLevelOrder(BtNode *ptr)
{
if (ptr == NULL) return;
queue<BtNode *> qu;
qu.push(ptr);
while (!qu.empty())
{
ptr = qu.front(); qu.pop();
cout << ptr->data << " ";
if (ptr->leftchild != NULL)
qu.push(ptr->leftchild);
if (ptr->rightchild != NULL)
qu.push(ptr->rightchild);
}
}
static void Destroy(BtNode *ptr)
{
if (ptr != NULL)
{
Destroy(ptr->leftchild);
Destroy(ptr->rightchild);
_Freenode(ptr);
}
}
static int Size(BtNode * p)
{
if (p == NULL) return 0;
else return Size(p->leftchild) + Size(p->rightchild) + 1;
}
static int Depth(BtNode *p)
{
if (p == NULL) return 0;
else return Max(Depth(p->leftchild), Depth(p->rightchild)) + 1;
}
static BtNode * FindValue(BtNode *ptr, const Type &x)
{
if (ptr == NULL || ptr->data == x) return ptr;
else
{
BtNode *p = FindValue(ptr->leftchild, x);
if (NULL == p)
{
p = FindValue(ptr->rightchild, x);
}
return p;
}
}
static BtNode *FindParent(BtNode *ptr, const BtNode *child)
{
if (ptr == NULL || ptr->leftchild == child
|| ptr->rightchild == child) return ptr;
else
{
BtNode *p = FindParent(ptr->leftchild, child);
if (NULL == p)
{
p = FindParent(ptr->rightchild, child);
}
return p;
}
}
static BtNode * Copy(BtNode *ptr)
{
BtNode *s = NULL;
if (ptr != NULL)
{
s = _Buynode();
s->data = ptr->data;
s->leftchild = Copy(ptr->leftchild);
s->rightchild = Copy(ptr->rightchild);
}
return s;
}
static void Copy(BtNode *&p, BtNode *ptr)
{
if (ptr == NULL) p = NULL;
else
{
p = _Buynode();
p->data = ptr->data;
Copy(p->leftchild, ptr->leftchild);
Copy(p->rightchild, ptr->rightchild);
}
}
static bool equal(const BtNode *pa, const BtNode *pb)
{
return (pa == NULL && pb == NULL) ||
(pa != NULL && pb != NULL &&
pa->data == pb->data &&
equal(pa->leftchild, pb->leftchild) &&
equal(pa->rightchild, pb->rightchild));
}
public:
typedef BtNode * PBtNode;
BinaryTree(const Type &x) :root(NULL), RefValue(x) {}
BinaryTree(const BinaryTree<Type> &bt) :RefValue(bt.RefValue)
{
//root =Copy(bt.root);
Copy(root, bt.root);
}// BinaryTree<char> yout(myt);
BinaryTree<Type> & operator=(const BinaryTree<Type> &bt)
{
if (this != &bt)
{
Destroy(root);
root = Copy(bt.root);
}
return *this;
}
~BinaryTree()
{
Clear();
}
BtNode * GetRoot() const { return root; }
void Clear()
{
Destroy(root);
root = NULL;
}
void CreateTree(Type *str)
{
if (str != NULL)
{
root = Create(str);
}
}
void PreOrder() const
{
PreOrder(root);
cout << endl;
}
void InOrder() const
{
InOrder(root);
cout << endl;
}
void PastOrder() const
{
PastOrder(root);
cout << endl;
}
void LeverOrder()const
{
NiceLevelOrder(root);
cout << endl;
}
BtNode * FindValue(const Type &x) const
{
return FindValue(root, x);
}
BtNode * FindParent(const BtNode *child) const
{
if (root == NULL || child == NULL ||
root == child) return NULL;
else
{
return FindParent(root, child);
}
}
int Size() const { return Size(root); }
int Depth() const { return Depth(root); }
bool empty() const { return root == NULL; }
bool operator==(const BinaryTree<Type> &bt) const
{
return equal(this->root, bt.root);
}
bool operator != (const BinaryTree<Type> &bt) const
{
return !(*this == bt);
}
};
/
template<class Type>
class TreeIterator
{
protected:
BinaryTree<Type> &tree;
typename BinaryTree<Type>::PBtNode _Ptr;
public:
TreeIterator( BinaryTree<Type> &bt) :tree(bt), _Ptr(NULL)
{}
Type &operator*() { return _Ptr->data; }
const Type & operator*() const { return _Ptr->data; }
bool IsDone() const { return _Ptr == NULL; }
virtual void First() = 0;
virtual void operator++() = 0;
};
template<class Type>
class PreIterator : public TreeIterator<Type>
{
protected:
stack<typename BinaryTree<Type>::PBtNode > st;
public:
PreIterator(BinaryTree<Type> &bt) :TreeIterator<Type>(bt)
{}
virtual void First()
{
_Ptr = NULL;
if (tree.GetRoot() != NULL)
{
st.push(tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if (st.empty())
{
_Ptr = NULL;
return;
}
_Ptr = st.top(); st.pop();
if (_Ptr->rightchild != NULL)
st.push(_Ptr->rightchild);
if (_Ptr->leftchild != NULL)
st.push(_Ptr->leftchild);
}
};
template<class Type>
struct StkNode
{
typename BinaryTree<Type>::PBtNode pnode;
int popnum;
public:
StkNode(typename BinaryTree<Type>::PBtNode p = NULL) //如果解析器在template中遭遇一个嵌套从属名称,它便假设这个名称不是个类型,除非你告诉它是。所以缺省情况下嵌套从属名称不是类型
:pnode(p), popnum(0) {}
};
template<class Type>
class PastIterator : public TreeIterator<Type>
{
protected:
stack<StkNode<Type> > st;
public:
PastIterator(BinaryTree<Type> &bt) :TreeIterator<Type>(bt) {}
virtual void First()
{
_Ptr = NULL;
if (tree.GetRoot() != NULL)
{
st.push(StkNode<Type>(tree.GetRoot()));
operator++();
}
}
virtual void operator++()
{
if (st.empty())
{
_Ptr = NULL;
return;
}
StkNode<Type> node;
for (;;)
{
node = st.top(); st.pop();
if (++node.popnum == 3)
{
_Ptr = node.pnode;
return;
}
st.push(node);
if (node.popnum == 1 && node.pnode->leftchild != NULL)
{
st.push(StkNode<Type>(node.pnode->leftchild));
}
else if (node.popnum == 2 && node.pnode->rightchild != NULL)
{
st.push(StkNode<Type>(node.pnode->rightchild));
}
}
}
};
template<class Type>
class InIterator :public TreeIterator<Type>
{
protected:
stack<StkNode<Type> > st;
public:
InIterator(BinaryTree<Type> &bt):TreeIterator<Type>(bt){}
virtual void First()
{
_Ptr = NULL;
if (tree.GetRoot() != NULL)
{
st.push((StkNode<Type>)tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if (st.empty())
{
_Ptr = NULL;
return;
}
StkNode<Type> node;
while (1)
{
node = st.top(); st.pop();
if (++node.popnum == 2)
{
if (node.pnode->rightchild != NULL)
{
st.push((StkNode<Type>)node.pnode->rightchild);
}
_Ptr = node.pnode;
return;
}
st.push(node);
if (node.popnum == 1 && node.pnode->leftchild != NULL)
{
st.push(StkNode<Type>(node.pnode->leftchild));
}
}
}
};
template<class Type>
class LevelIterator:public TreeIterator<Type>
{
protected:
queue<typename BinaryTree<Type>::PBtNode > q;
public:
LevelIterator(BinaryTree<Type> &bt) :TreeIterator<Type>(bt) {}
virtual void First()
{
_Ptr = NULL;
if (tree.GetRoot() != NULL)
{
q.push(tree.GetRoot());
operator++();
}
}
virtual void operator++()
{
if (q.empty())
{
_Ptr = NULL;
return;
}
_Ptr = q.front(); q.pop();
if (_Ptr->leftchild != NULL)
{
q.push(_Ptr->leftchild);
}
if (_Ptr->rightchild != NULL)
{
q.push(_Ptr->rightchild);
}
}
};
//
template<class Type>
void Print_Iterator(TreeIterator<Type> &bt)
{
bt.First();
while (!bt.IsDone())
{
cout << *bt << " ";
++bt;
}
cout << endl;
}
void main()
{
char *str = "ABC##DE##F##G#H##";
BinaryTree<char> myt('#');
myt.CreateTree(str);
myt.PreOrder();
myt.InOrder();
myt.PastOrder();
myt.LeverOrder();
PreIterator<char> pi(myt);
InIterator<char> p(myt);
PastIterator<char> ps(myt);
LevelIterator<char> pl(myt);
Print_Iterator(pi);
Print_Iterator(p);
Print_Iterator(ps);
Print_Iterator(pl);
}
二叉树迭代器遍历
最新推荐文章于 2023-11-28 21:31:22 发布