定义:
typedef strcut bitnode{
int data;
strcut bitnode *left ,*rchlid;
}bitnode,*Bitnode;
先中后序遍历
void preorder(Bitree root){
if (root==NULL)
return;
visit(root)
preorder(root->lchlid);
preorder(root->rchlid); }
void inorder(Bitree root){
if (root==NULL)
return;
inorder(root->lchlid);
visit(root)
inorder(root->rchlid); }
void postorder(Bitree root){
if (root==NULL)
return;
postorder(root->lchlid);
postorder(root->rchlid);
visit(root->rchlid); }
层序遍历
void levelorder(bitree)
{
queue q;
initqueue (q);
bitree p;
enqueue (q,t);
while(isempty(q))
{
dequeue(q,p);
visit(p);
if(p->lchlid!=NULL)
enqueue(q,q->lchlid);
if(p->rchlid!=NULL)
enqueue(q,q->rchlid);
}
求树高
int height=0;
void preorder(bitree t,int n)
{
if(t==NULL)
return;
if(n>height)
height=n;
preorder(t->lchild,n+1);
preorder(t->rchild,n+1);
}
preorder(root,1);
int postorder(bitree t)
{
if(t==NULL)
return 0;
int left=postorder(t-lchild);
int right=postorder(t-rchild);
if(left>right) return left+1;
if(left<right) return right+1;
}
postorder(root);
求宽度
int width[max];
void preorder(bitree t,int level)
{
if(t==NULL) return;
width[level]++;
preorder(t->lchild,level+1);
preorder(t->rchild,level+1);
}
void treewidth(bitree t)
{
for(int i=0;i<n;i++)
{
width[i]=0;
}
preorder(t,0);
int maxwidth=0;
for(int i=0;i<max;i++)
{
求最大值
}
}
求wpl
int wpl=0;
void preorder(bitree T,int level)
{
if(t==NULL) return;
if(t->lchild==NULL&&t->rchild==NULL)
{
WPL+=level*t->weight;
}
preorder(t->lchild,level+1);
preorder(t->rchild,level+1);
}