C 锯齿树印刷

C 锯齿树印刷,c,algorithm,data-structures,C,Algorithm,Data Structures,我想以之字形的顺序打印这棵树。这是我的代码,但它没有运行。有人能指出错误吗? 我已经创建了两个堆栈来执行同样的操作,当其中两个堆栈为空时,我的算法终止,如果其中任何一个包含一些节点,那么这些节点将被打印,其子节点将被推送到另一个堆栈中。此过程将继续,直到两个堆栈都变为空 #include<stdio.h> #define MAX_SIZE 100 struct TreeNode{ int value; struct TreeNode *left;

我想以之字形的顺序打印这棵树。这是我的代码,但它没有运行。有人能指出错误吗? 我已经创建了两个堆栈来执行同样的操作,当其中两个堆栈为空时,我的算法终止,如果其中任何一个包含一些节点,那么这些节点将被打印,其子节点将被推送到另一个堆栈中。此过程将继续,直到两个堆栈都变为空

#include<stdio.h>
#define MAX_SIZE 100

struct TreeNode{
      int value;
      struct TreeNode *left;
      struct TreeNode *right;
};


struct TreeNode stackA[MAX_SIZE]; 
struct TreeNode stackB[MAX_SIZE];

int topA = 0;
int topB = 0;


void push(struct TreeNode stack[], struct TreeNode *node, int *top){
     if(*top > MAX_SIZE){
             printf("stack is full\n");
     }
     else{
          stack[*top] = *node;
          *top = *top +1;
     }
     return;
}

struct TreeNode* pop(struct TreeNode stack[], int *top){
     if(*top == 0){
             printf("stack is empty\n");
               return NULL;
     }
     else{
          struct TreeNode *tmp = stack + *top;
          *top = *top - 1;
          return tmp;
     }


}

int isEmpty(int *top){
        if(*top == 0){
                return 1;
        }

        return 0;
}
void printZigZag(struct TreeNode* root){
     push(stackA,  root,&topA);
     printf("%d\n", topA);
     while(!isEmpty(&topA) || !isEmpty(&topB)){
        while(!isEmpty(&topA)){
           struct TreeNode* temp = pop(stackA,&topA);
           printf("%d", temp->value);
           if(temp->left) push(stackB,temp->left,&topB);
           if(temp->right) push(stackB,temp->right,&topB);
           printf("%d %d",topA,topB);
           return;
       }
       while(!isEmpty(&topB)){
           struct TreeNode *temp = pop(stackB,&topB);
           printf("%d", temp->value);
           if(temp->right) push(stackA,temp->right,&topA);
           if(temp->left) push(stackA,temp->left,&topB);
      }
   }
}


int main(){
    struct TreeNode* root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    root->value = 5;
    root->left = NULL;
    root->right = NULL;

    struct TreeNode* first = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    first->value = 15;
    first->left = NULL;
    first->right = NULL;
    root->left = first;


    struct TreeNode* second = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    second->value = 235;
    second->left = NULL;
    second->right = NULL;
    root->right = second;


    struct TreeNode *third = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    third->value = 45;
    third->left = NULL;
    third->right = NULL;
    first->left = third;


    struct TreeNode *fourth = (struct TreeNode *)malloc(sizeof(struct TreeNode));
    fourth->value = 55;
    fourth->left = NULL;
    fourth->right = NULL;
    first->right = fourth;

    printZigZag(root);

    system("pause");
    return 0;
}
#包括
#定义最大尺寸100
树状结构{
int值;
结构树节点*左;
结构树节点*右侧;
};
struct TreeNode stackA[最大尺寸];
struct TreeNode stackB[最大尺寸];
int-topA=0;
int-topB=0;
无效推送(结构TreeNode堆栈[],结构TreeNode*节点,int*顶部){
如果(*顶部>最大尺寸){
printf(“堆栈已满\n”);
}
否则{
堆栈[*顶部]=*节点;
*top=*top+1;
}
返回;
}
struct TreeNode*pop(struct TreeNode堆栈[],int*top){
如果(*top==0){
printf(“堆栈为空\n”);
返回NULL;
}
否则{
struct TreeNode*tmp=stack+*top;
*top=*top-1;
返回tmp;
}
}
int为空(int*top){
如果(*top==0){
返回1;
}
返回0;
}
void printZigZag(结构树节点*根){
推送(stackA、root和topA);
printf(“%d\n”,topA);
而(!isEmpty(&topA)| |!isEmpty(&topB)){
而(!isEmpty(&topA)){
struct TreeNode*temp=pop(stackA和topA);
printf(“%d”,温度->值);
如果(临时->左)推(堆栈B、临时->左和顶部B);
如果(临时->右侧)推送(堆栈B、临时->右侧和顶部B);
printf(“%d%d”,topA,topB);
返回;
}
而(!isEmpty(&topB)){
结构树节点*temp=pop(stackB和topB);
printf(“%d”,温度->值);
如果(临时->右侧)推送(stackA、临时->右侧和topA);
如果(临时->左)推(stackA、临时->左和topB);
}
}
}
int main(){
struct TreeNode*root=(struct TreeNode*)malloc(sizeof(struct TreeNode));
根->值=5;
根->左=空;
root->right=NULL;
struct TreeNode*first=(struct TreeNode*)malloc(sizeof(struct TreeNode));
第一->值=15;
第一->左=空;
第一->右=空;
根->左=第一;
struct TreeNode*second=(struct TreeNode*)malloc(sizeof(struct TreeNode));
第二->值=235;
第二->左=空;
第二->右=空;
根->右=秒;
struct TreeNode*third=(struct TreeNode*)malloc(sizeof(struct TreeNode));
第三->值=45;
第三->左=空;
第三->右=空;
第一->左=第三;
struct TreeNode*fourth=(struct TreeNode*)malloc(sizeof(struct TreeNode));
第四->值=55;
第四->左=空;
第四->右=空;
第一->右=第四;
打印之字形(根);
系统(“暂停”);
返回0;
}

我已经检查了您的代码。您犯的最大错误是,Pop函数是错误实现的。在获得堆栈的顶部值之前,应减小*pop的值。另一个错误是,如果(临时->左)推(stackA,临时->左,&topB)应该更改为如果(临时->左)推(stackA,临时->左,&topA)。下面是代码工作,我改变一些小地方,适合C++。
#include<cstdio>
#define MAX_SIZE 100

struct TreeNode{
      int value;
      struct TreeNode *left;
      struct TreeNode *right;
};


struct TreeNode stackA[MAX_SIZE]; 
struct TreeNode stackB[MAX_SIZE];

int topA = 0;
int topB = 0;


void push(struct TreeNode stack[], struct TreeNode *node, int *top){
     if(*top > MAX_SIZE){
             printf("stack is full\n");
     }
     else{
          stack[*top] = *node;
          *top = *top +1;
     }
     return;
}

struct TreeNode* pop(struct TreeNode stack[], int *top){
     if(*top == 0){
             printf("stack is empty\n");
               return NULL;
     }
     else{
          *top = *top - 1; 
         struct TreeNode *tmp = stack +*top;         
          return tmp;
     }


}

int isEmpty(int *top){
        if(*top == 0){
                return 1;
        }

        return 0;
}
void printZigZag(struct TreeNode* root){
     push(stackA,  root,&topA);
     while(!isEmpty(&topA) || !isEmpty(&topB)){
        while(!isEmpty(&topA)){
           struct TreeNode* temp = pop(stackA,&topA);
           printf("%d\n", temp->value);
           if(temp->left) push(stackB,temp->left,&topB);
           if(temp->right) push(stackB,temp->right,&topB);
       }
       while(!isEmpty(&topB)){
           struct TreeNode *temp = pop(stackB,&topB);
           printf("%d\n", temp->value);
           if(temp->right) push(stackA,temp->right,&topA);
           if(temp->left) push(stackA,temp->left,&topA);
      }
   }
}

int main(){
    struct TreeNode* root = new TreeNode();
    root->value = 5;
    root->left = NULL;
    root->right = NULL;

    struct TreeNode* first =new TreeNode();
    first->value = 15;
    first->left = NULL;
    first->right = NULL;
    root->left = first;


    struct TreeNode* second = new TreeNode();
    second->value = 235;
    second->left = NULL;
    second->right = NULL;
    root->right = second;


    struct TreeNode *third = new TreeNode();
    third->value = 45;
    third->left = NULL;
    third->right = NULL;
    first->left = third;


    struct TreeNode *fourth = new TreeNode();
    fourth->value = 55;
    fourth->left = NULL;
    fourth->right = NULL;
    first->right = fourth;

    printZigZag(root);
    return 0;
}
#包括
#定义最大尺寸100
树状结构{
int值;
结构树节点*左;
结构树节点*右侧;
};
struct TreeNode stackA[最大尺寸];
struct TreeNode stackB[最大尺寸];
int-topA=0;
int-topB=0;
无效推送(结构TreeNode堆栈[],结构TreeNode*节点,int*顶部){
如果(*顶部>最大尺寸){
printf(“堆栈已满\n”);
}
否则{
堆栈[*顶部]=*节点;
*top=*top+1;
}
返回;
}
struct TreeNode*pop(struct TreeNode堆栈[],int*top){
如果(*top==0){
printf(“堆栈为空\n”);
返回NULL;
}
否则{
*top=*top-1;
struct TreeNode*tmp=stack+*top;
返回tmp;
}
}
int为空(int*top){
如果(*top==0){
返回1;
}
返回0;
}
void printZigZag(结构树节点*根){
推送(stackA、root和topA);
而(!isEmpty(&topA)| |!isEmpty(&topB)){
而(!isEmpty(&topA)){
struct TreeNode*temp=pop(stackA和topA);
printf(“%d\n”,temp->value);
如果(临时->左)推(堆栈B、临时->左和顶部B);
如果(临时->右侧)推送(堆栈B、临时->右侧和顶部B);
}
而(!isEmpty(&topB)){
结构树节点*temp=pop(stackB和topB);
printf(“%d\n”,temp->value);
如果(临时->右侧)推送(stackA、临时->右侧和topA);
如果(临时->左)推(stackA、临时->左和topA);
}
}
}
int main(){
struct TreeNode*root=new TreeNode();
根->值=5;
根->左=空;
root->right=NULL;
struct TreeNode*first=new TreeNode();
第一->值=15;
第一->左=空;
第一->右=空;
根->左=第一;
struct TreeNode*second=new TreeNode();
第二->值=235;
第二->左=空;
第二->右=空;
根->右=秒;
struct TreeNode*third=new TreeNode();
第三->值=45;
第三->左=空;
第三->右=空;
第一->左=第三;
struct TreeNode*fourth=新TreeNode();
第四->值=55;
第四->左=空;
第四->右=空;
第一->右=第四;
打印之字形(根);
返回0;
}

希望有帮助

一个问题是您没有刷新打印的输出,这意味着您在程序完成之前不会看到任何输出。为
printf
调用添加换行符,或者为
stdout
显式调用
fflush
。如果您希望将树视为图像,我建议您使用Graphviz。使您的c程序将graphviz代码输出到一个文件中,并使用它获取ima