#include<stdio.h> #include<stdlib.h> typedef struct TreeNode { char data; struct TreeNode* lch; struct TreeNode* rch; }TreeNode; typedef struct QueueNode { TreeNode* data; struct QueueNode* pre; struct QueueNode* next; }QueueNode; void createTree(TreeNode** T, char* data, int* index) { char ch; ch = data[*index]; *index += 1; if (ch == '#') { *T = NULL; } else { *T = (TreeNode*)malloc(sizeof(TreeNode)); (*T)->data = ch; createTree(&((*T)->lch), data, index); createTree(&((*T)->rch), data, index); } } void preOrder(TreeNode* T) { if (T == NULL) { return; } else { printf("%c ", T->data); preOrder(T->lch); preOrder(T->lch); } } int main(int argc,char* argv[]) { TreeNode* T; int index = 0; createTree(&T, argv[1], &index); preOrder(T); printf("\n"); return 0; }检查错误(编译器vs)
时间: 2024-03-17 22:40:19 浏览: 221
SeqList.h #include <stdio.h> //包含标准输入输出流的头文件 #include <assert.h
你的程序中存在一个小错误,即在 preOrder 函数中,递归调用时应该是 preOrder(T->rch),而不是 preOrder(T->lch)。下面是修改后的代码:
```
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode* lch;
struct TreeNode* rch;
}TreeNode;
typedef struct QueueNode {
TreeNode* data;
struct QueueNode* pre;
struct QueueNode* next;
}QueueNode;
void createTree(TreeNode** T, char* data, int* index) {
char ch;
ch = data[*index];
*index += 1;
if (ch == '#') {
*T = NULL;
}
else {
*T = (TreeNode*)malloc(sizeof(TreeNode));
(*T)->data = ch;
createTree(&((*T)->lch), data, index);
createTree(&((*T)->rch), data, index);
}
}
void preOrder(TreeNode* T) {
if (T == NULL) {
return;
}
else {
printf("%c ", T->data);
preOrder(T->lch);
preOrder(T->rch);
}
}
int main(int argc,char* argv[]) {
TreeNode* T;
int index = 0;
createTree(&T, argv[1], &index);
preOrder(T);
printf("\n");
return 0;
}
```
修改后的程序应该可以正常编译运行了。
阅读全文