C 上下二叉树显示

C 上下二叉树显示,c,binary-tree,C,Binary Tree,下面的代码在控制台窗口上从左到右显示我创建的二叉树。如何像在纸上一样从上到下打印 // Binary Trees Implementation #include<stdio.h> #include<stdlib.h> typedef struct BTNode { int key; struct BTNode *left, *right; }; void displayBT(BTNode *p, in

下面的代码在控制台窗口上从左到右显示我创建的二叉树。如何像在纸上一样从上到下打印

// Binary Trees Implementation

#include<stdio.h>
#include<stdlib.h>

typedef struct BTNode
    { 

           int key;
           struct BTNode *left, *right;

    };

void displayBT(BTNode *p, int level);

BTNode *buildBT();

void RSD(BTNode *p);

void SRD(BTNode *p);

void SDR(BTNode *p);

int main()
{ 

  int op;
  BTNode *root;
  do
       { 
             printf("\n 1. Build Binary Tree");
             printf("\n 2. Display Binary Tree");
             printf("\n 3. Display Traversals");
             printf("\n 0. Exit");
             printf("\n........................\n");

             scanf("\n %d", &op);

             switch(op)
                       { 
                            case 1:
                                 root=buildBT();
                                 break;
                            case 2:
                                 displayBT(root,0);
                                 break;
                            case 3:
                                 printf("\n Pre-Order:");
                                 RSD(root);
                                 printf("\n In-Order:");
                                 SRD(root);
                                 printf("\n Post-Order:");
                                 SDR(root);
                                 printf("\n.....................\n");
                                 break;        
                       }   

       } while(op); 

} 

BTNode *buildBT()
   { 
       int value;
       BTNode *p;

       printf("\n k=");
       scanf("%d",&value);

       if(value!=0)
                   { 
                       p=(BTNode *)malloc(sizeof(BTNode));
                       p->key = value;
                       p->left = buildBT();
                       p->right = buildBT();

                   } else p=NULL; 

       return p;

   }  

void displayBT(BTNode *p, int level)
 { 
     if(p!=NULL)
                { 
                    displayBT(p->right, level+1);

                    for(int j=0; j<=level;j++)
                             printf(" ");
                    printf("%d \n", p->key);
                    displayBT(p->left, level+1);

                } 

 } 

void RSD(BTNode *p)
 { 
     if(p!=NULL)
                { 
                    printf("%d ", p->key);
                    RSD(p->left);
                    RSD(p->right);
                } 

 } 

void SRD(BTNode *p)
 { 
     if(p!=NULL)
                { 
                    SRD(p->left);
                    printf("%d ", p->key);
                    SRD(p->right);
                } 

 } 

void SDR(BTNode *p)
 { 
     if(p!=NULL)
                { 
                    SDR(p->left);
                    SDR(p->right);
                    printf("%d ", p->key);
                } 

 } 
//二叉树的实现
#包括
#包括
类型定义结构BTNode
{ 
int键;
结构BTNode*左、*右;
};
void displayBT(BTNode*p,int级别);
BTNode*buildBT();
无效RSD(BTNode*p);
无效SRD(BTNode*p);
无效SDR(BTNode*p);
int main()
{ 
int op;
BTNode*根;
做
{ 
printf(“\n 1.构建二叉树”);
printf(“\n 2.显示二叉树”);
printf(“\n 3.显示遍历”);
printf(“\n 0.Exit”);
printf(“\n………\n”);
scanf(“\n%d”,&op);
开关(op)
{ 
案例1:
root=buildBT();
打破
案例2:
显示BT(根,0);
打破
案例3:
printf(“\n预订单:”);
RSD(根);
printf(“\n按顺序:”);
SRD(根);
printf(“\n邮政订单:”);
SDR(根);
printf(“\n……。\n”);
打破
}   
}while(op);
} 
BTNode*buildBT()
{ 
int值;
BTNode*p;
printf(“\n k=”);
scanf(“%d”,和值);
如果(值!=0)
{ 
p=(BTNode*)malloc(sizeof(BTNode));
p->key=value;
p->left=buildBT();
p->right=buildBT();
}否则p=NULL;
返回p;
}  
void displayBT(BTNode*p,整数级)
{ 
如果(p!=NULL)
{ 
显示BT(p->右侧,级别+1);
对于(int j=0;jkey);
显示BT(p->左,级别+1);
} 
} 
无效RSD(BTNode*p)
{ 
如果(p!=NULL)
{ 
printf(“%d”,p->key);
相对标准偏差(p->左);
RSD(p->右侧);
} 
} 
无效SRD(BTNode*p)
{ 
如果(p!=NULL)
{ 
SRD(p->左);
printf(“%d”,p->key);
SRD(p->右侧);
} 
} 
无效SDR(BTNode*p)
{ 
如果(p!=NULL)
{ 
SDR(p->左);
特别提款权(p->右);
printf(“%d”,p->key);
} 
} 

好的,我上传了完整的代码,因为我对建议有问题,也许我应该从一开始就这样做。

在右/左递归之前打印当前节点的值

又试了一次,实际上现在已经试过了。为我工作

void print_box(FILE* out, int i)
{
    static char buff[16] = {0};
    sprintf(buff,"%d",i);
    int n = strlen(buff);
    static char buf2[16] = {0};
    strcpy(buf2,"[   ]");
    int nn = 2 - (n-1)/2 ;
    for(i=0;i<n;++i)
        buf2[nn+i] = buff[i];
    fprintf(out,"%s",buf2);
}

void print_empty_box(FILE* out) { fprintf(out,"%s","[ - ]"); }

typedef struct NodeRowTag
{
    struct NodeRowTag* nxt;
    BTNode* node;
} NodeRow;

NodeRow* make_head()
{
    NodeRow* nr;
    nr = (NodeRow*) malloc(sizeof(NodeRow));
    nr->node = 0;
    nr->nxt = 0;
    return nr;
}

void push_back( NodeRow* nr, BTNode* n )
{
    while( nr->nxt )
    {
        nr = nr->nxt;
    }
    nr->nxt = (NodeRow*) malloc(sizeof(NodeRow));
    nr->nxt->node = n;
    nr->nxt->nxt = 0;
}

void del_all( NodeRow* nr )
{
    if( nr->nxt )
        del_all(nr->nxt);
    free( nr );
}

NodeRow* print_and_next( FILE* out, NodeRow* nr, int rownum, int maxnum )
{
    // init spacing
    int spacing = 0;
    int stride = 3;
    for(int i=rownum; i<maxnum; ++i)
    {
        spacing += stride;
        stride *= 2;
    }
    for(int i=0;i<spacing;++i)
        fprintf(out, " " );

    // inbetween spacing
    spacing = 1;
    stride = 6;
    for(int i=rownum; i<maxnum; ++i)
    {
        spacing += stride;
        stride *= 2;
    }

    // 
    NodeRow* nxt = make_head();
    NodeRow* n = nr->nxt;
    while(n)
    {
        BTNode* p = n->node;
        if(p) {
            print_box(out,p->key);
            push_back(nxt,p->left);
            push_back(nxt,p->right);
        } else {
            print_empty_box(out);
            push_back(nxt,0);
            push_back(nxt,0);
        }
        for(int i=0;i<spacing;++i)
            fprintf(out, " " );
        n=n->nxt;
    }

    fprintf(out, "\n" );

    del_all(nr);

    return nxt;
}

int max(int a,int b) { return (a>b)?a:b; }

int max_depth( BTNode* p )
{
    if(!p) return 0;
    return 1 + max( max_depth(p->left), max_depth(p->right) );
}

void PrittyPrint( FILE* out )
{
    int n = max_depth(root);

    NodeRow* nr = make_head();
    push_back(nr,root);

    for(int i=1; i<=n; ++i)
    {
        nr = print_and_next( out, nr, i, n );
    }
    del_all(nr);
}
void打印框(文件*out,int i)
{
静态字符buff[16]={0};
sprintf(buff,“%d”,i);
int n=strlen(浅黄色);
静态字符buf2[16]={0};
strcpy(buf2,“[]”);
int nn=2-(n-1)/2;
对于(i=0;inode=0;
nr->nxt=0;
返回nr;
}
无效推回(NodeRow*nr,BTNode*n)
{
而(nr->nxt)
{
nr=nr->nxt;
}
nr->nxt=(NodeRow*)malloc(sizeof(NodeRow));
nr->nxt->node=n;
nr->nxt->nxt=0;
}
全部作废(NodeRow*nr)
{
如果(nr->nxt)
全部删除(nr->nxt);
免费(nr);
}
NodeRow*打印和下一步(文件*out,NodeRow*nr,int rownum,int maxnum)
{
//初始间距
整数间距=0;
int步长=3;
for(int i=rownum;ikey);
向后推(nxt,p->左);
向后推(nxt,p->右侧);
}否则{
打印空框(输出);
推回(nxt,0);
推回(nxt,0);
}
对于(int i=0;inxt;
}
fprintf(out,“\n”);
德鲁阿勒(nr);
返回nxt;
}
intmax(inta,intb){返回(a>b)?a:b;}
int最大深度(BTNode*p)
{
如果(!p)返回0;
返回1+max(max_depth(p->left),max_depth(p->right));
}
void PrittyPrint(文件*out)
{
int n=最大深度(根);
NodeRow*nr=make_head();
向后推(nr,根部);

对于(inti=1;iIt不起作用。而且,它现在根本不打印任何内容。刚刚意识到这有很多错误。好吧,这是一个起点。否则,只需旋转显示器?