## CSDN博客

iterator的结构是线性的前驱，后继。

1。二叉树就是二叉树， 老老实实地把二叉树的功能做好。不要考虑iterator.

namespace trit{
/*

template<typename NODE_PTR>
protocol Tree{
NODE_PTR getLeft();
NODE_PTR getRight();
NODE_PTR getParent();
}

template<typename NODE_PTR>
protocol Iterator{
operator bool();
NODE_PTR deref();
Self next();
Self prev();
}
*/

template<typename NODE_PTR>
class Iterator{
protected:
typedef Iterator<NODE_PTR> It;
public:
NODE_PTR deref()const{
return node;
}
operator bool()const{
return node!=NODE_PTR(0);
}
Iterator(NODE_PTR const n):node(n){}
friend bool operator==(const It& a, const It& b){
return a.deref()==b.deref();
}
friend bool operator!=(const It& a, const It& b){
return !(a==b);
}
It& operator=(const It& other){
this->node = other.node;
return *this;
}
It end(){
return NODE_PTR(0);
}
static NODE_PTR getRoot(NODE_PTR node){
NODE_PTR ret(node);
NODE_PTR tmp(ret->getParent());
for(;tmp!=NODE_PTR(0);ret=tmp, tmp=ret->getParent());
return ret;
}
static NODE_PTR getLeftmost(NODE_PTR node){
NODE_PTR ret(node);
NODE_PTR tmp(ret->getLeft());
for(;!isNull(tmp);ret=tmp, tmp=ret->getLeft());
return ret;
}
static NODE_PTR getLeftDeep(NODE_PTR node){
NODE_PTR ret(node);
for(;;){
NODE_PTR tmpl(ret->getLeft());
if(isNull(tmpl)){
NODE_PTR tmpr(ret->getRight());
if(isNull(tmpr)){
return ret;
}
else{
ret = tmpr;
}
}
else{
ret = tmpl;
}
}
}
static bool isNull(NODE_PTR ptr){
return ptr==NODE_PTR(0);
}
private:
NODE_PTR node;

};

template<typename NODE_PTR>
class Preorder;

template<typename NODE_PTR>
class Postorder;
template<typename NODE_PTR>
class Inorder;

template<typename NODE_PTR>
class Preorder /*supports Iterator<NODE_PTR>*/: public Iterator<NODE_PTR>{

typedef Preorder<NODE_PTR> Self;
public:
Self next()const{
return goLeft(deref());
}
Self prev()const{
}
Self begin()const{
return Self(getRoot(deref()));
}
private:

static Self ret(NODE_PTR me, NODE_PTR from){
if(isNull(me)){
return Self(NODE_PTR(0));
}
else if(from==me->getLeft()){
return goRight(me);
}
else{
return goParent(me);
}
}

static Self goLeft(NODE_PTR const node){
NODE_PTR const left(node->getLeft());
if(isNull(left)){
return goRight(node);
}
else{
return Self(left);
}
}
static Self goRight(NODE_PTR const node){
NODE_PTR const right(node->getRight());
if(isNull(right)){
return goParent(node);
}
else{
return Self(right);
}
}
static Self goParent(NODE_PTR const node){
return ret(node->getParent(), node);
}
public:
Preorder(NODE_PTR const n):It(n){}
Self& operator=(const It& other){
return (Self&) It::operator=(other);
}
};

template<typename NODE_PTR>
class Inorder: public Iterator<NODE_PTR>{
typedef Inorder<NODE_PTR> Self;
public:
Self next()const{
return goRight(deref());
}
Self prev()const{
}
Self begin()const{
return Self(getLeftmost(deref()));
}
private:
static Self ret(NODE_PTR const me, NODE_PTR const from){
if(isNull(me)){
return Self(NODE_PTR(0));
}
else if(me->getLeft()==from){
return Self(me);
}
else{
return goParent(me);
}
}

static Self goRight(NODE_PTR const node){
NODE_PTR const right(node->getRight());
if(isNull(right)){
return goParent(node);
}
else{
return Self(getLeftmost(right));
}
}
static Self goParent(NODE_PTR const node){
return ret(node->getParent(), node);
}
public:
Inorder(NODE_PTR const n):It(n){}
Self& operator=(const It& other){
return (Self&) It::operator=(other);
}
};

template<typename NODE_PTR>
class Postorder: public Iterator<NODE_PTR>{
typedef Postorder<NODE_PTR> Self;
public:
Self next(){
return goParent(deref());
}
Self prev(){
}
Self begin(){
return Self(getLeftDeep(deref()));
}
static Self ret(NODE_PTR const me, NODE_PTR const from){
if(isNull(me)){
return Self(NODE_PTR(0));
}
if(me->getLeft()==from){
return goRight(me);
}
else{
return Self(me);
}
}
private:
static Self goRight(NODE_PTR const node){
NODE_PTR const right(node->getRight());
if(isNull(right)){
return Self(node);
}
else{
return Self(getLeftDeep(right));
}
}
static Self goParent(NODE_PTR const node){
return ret(node->getParent(), node);
}
public:
Postorder(NODE_PTR const n):It(n){}
Self& operator=(const It& other){
return (Self&) It::operator=(other);
}

};

template<typename NODE_PTR>
class RevTree/*supports Tree<NODE_PTR>*/{
typedef RevTree<NODE_PTR> Self;
public:
RevTree(NODE_PTR const t):tr(t){}
Self getLeft()const{
return Self(tr->getRight());
}
Self getRight()const{
return Self(tr->getLeft());
}
Self getParent()const{
return Self(tr->getParent());
}
const Self* operator->()const {return this;}

bool operator == (const Self& b)const{
return this->tr==b.tr;
}
bool operator== (const NODE_PTR b){
return this->tr==b;
}
friend bool operator== (const NODE_PTR a, const Self& b){
return a==b.tr;
}
Self& operator=(const Self& other){
tr = other.tr;
return *this;
}

private:
NODE_PTR tr;
};

}

#include <iostream.h>
#include "trit.h"
using namespace trit;
class TreeCons{
public:
TreeCons* getLeft()const{return left;}
TreeCons* getRight()const{return right;}
TreeCons* getParent()const{return parent;}
void setParent(TreeCons* p){this->parent = p;}
const char* get(){return val;}
TreeCons(TreeCons* l, TreeCons* r, const char* v)
:left(l), right(r), parent(0), val(v){}
private:
TreeCons* left;
TreeCons* right;
TreeCons* parent;
const char* val;
};
typedef Preorder<TreeCons*> Pre;
typedef Inorder<TreeCons*> In;
typedef Postorder<TreeCons*> Post;

template<typename IT>
void printIt(IT it){
for(;it; it=it.next()){
cout << it.deref()->get();
}
cout <<endl;
}
template<typename IT>
void printRev(IT it){
for(;it; it=it.prev()){
cout << it.deref()->get();
}
cout <<endl;
}
int main(){
TreeCons* d = new TreeCons(0, 0, "D");
TreeCons* e = new TreeCons(0, 0, "E");
TreeCons* b = new TreeCons(d, e, "B");
d->setParent(b);
e->setParent(b);
TreeCons* f = new TreeCons(0, 0, "F");
TreeCons* c = new TreeCons(0, f, "C");
f->setParent(c);
TreeCons* a = new TreeCons(b, c, "A");
b->setParent(a);
c->setParent(a);
printIt(Pre(a));
printIt(Pre(c).begin());
printIt(In(a));
printIt(In(a).begin());
printIt(Post(a));
printIt(Post(a).begin());
printRev(Post(a));
printRev(In(a));
printRev(Pre(a));
delete a;
delete b;
delete c;
delete d;
delete e;
return 1;

}

：〉

0 0