gpt4 book ai didi

c - 上下二叉树显示

转载 作者:行者123 更新时间:2023-11-30 15:29:11 26 4
gpt4 key购买 nike

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

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

}

好的,所以我上传了完整的代码,因为我在接受建议时遇到了麻烦,也许我应该从一开始就这样做。

最佳答案

再试一次,现在就实际尝试一下。为我工作

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

关于c - 上下二叉树显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26326391/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com