上下二叉树显示

时间:2014-10-12 14:52:19

标签: 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, 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);
                } 

 } 

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

2 个答案:

答案 0 :(得分:1)

再给它一次,实际上现在尝试了。适合我

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);
}

答案 1 :(得分:0)

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