二叉樹
定義struct node
{
Item item;
link l, r;
};
{
if(h == NULL) return;
(*visit)(h);
traverse(h->l, visit);
traverse(h->r, visit);
}
{
STACKinit(max);
STACKpush(h);
while(!STACKempty())
{
(*visit)(h = STACKpop());
if(h->r != NULL) STACKpush(h->r); //右子樹入棧
if(h->l != NULL) STACKpush(h->l); //左子樹入棧
}
}
{
QUEUEinit(max);
QUEUEput(h);
while(!QUEUEempty())
{
(*visit)(h = QUEUEget());
if(h->l != NULL) QUEUEput(h->l);
if(h->r != NULL) QUEUEput(h->r);
}
}
{
if(h == NULL) return 0;
return count(h->l)+count(h->r)+1;
}
{
int u, v;
if(h == NULL) return -1;
u = height(h->l);
v = height(h->r);
if(u > v) return u+1;
else return v+1;
}