采用二叉链表存储,在console显示倒悬树的形态。
typedef struct BTreeNode
{
int data;
struct BTreeNode *left,*right;
}Bitree;
int depth_tree(Bitree *root)
{
if(root==NULL)
{
return 0;
}
else
{
int l,r;
l=depth_tree(root->left);
r=depth_tree(root->right);
if(l>r)
return l+1;
else
return r+1;
}
}
void show_tree(Bitree *root)
{
Bitree *myqueue[100];
int f,r,total,h,i,j,k,dis,maxnode;
Bitree *p;
f=r=0;
if(root==NULL)
{
printf("NULL tree\n");
return;
}
myqueue[++r]=root;
h=depth_tree(root);
maxnode=pow(2,h-1);
printf("\n");
for(i=0;i<h;i++)
{ total=pow(2,i);
dis=maxnode/total;
for(k=0;k<total;k++)
{
for(j=0;j<dis-1;j++)
printf(" ");
p=myqueue[++f];
if(p==NULL)
{ printf("空");
myqueue[++r]=NULL;
myqueue[++r]=NULL;
}
else
{
printf("%2d ",p->data);
myqueue[++r]=p->left;
myqueue[++r]=p->right;
}
for(j=0;j<dis;j++)
printf(" ");
}
printf("\n");
}
}