跟着写了一个二叉树层次遍历的代码
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode* lchild;
struct TreeNode* rchild;
}TreeNode;
typedef struct Queue {
TreeNode* data;
struct Queue* pre;
struct Queue* next;
}Queue;
void creatTree(TreeNode** T, char* data, int* index)
{
char ch = data[*index];
*index += 1;
if (ch == '#')
{
*T = NULL;
}
else {
*T = (TreeNode*)malloc(sizeof(TreeNode));
(*T)->data= ch;
}
creatTree(&((*T)->lchild), data, index);
creatTree(&((*T)->rchild), data, index);
}
Queue* initQueue()
{
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->data = NULL;
Q->next = NULL;
Q->pre = NULL;
return Q;
}
void enQueue(Queue* Q, TreeNode* data)
{
Queue* node = (Queue*)malloc(sizeof(Queue));
Queue* tag = Q;
while (tag->next != NULL)
{
tag = tag->next;//tag指向队尾
}
node->data = data;
node->next = NULL;
node->pre = tag;
tag->next = node;
}
int isEmpty(Queue* Q)
{
if (Q->next == NULL)
{
return 1;
}
else {
return 0;
}
}
Queue* deQueue(Queue* Q)
{
if (isEmpty)
{
return NULL;
}
Queue* first = Q->next;
Q->next = first->next;
return first;
}
void levelTraverse(Queue* Q,TreeNode* T)//层次遍历
{
enQueue(Q, T);
while (isEmpty(Q) == 0)
{
Queue* node = deQueue(Q);
printf("%c", node->data->data);
if (node->data->lchild)
{
enQueue(Q, node->data->lchild);
}
if (node->data->rchild)
{
enQueue(Q, node->data->rchild);
}
}
printf("\n");
}
void preOrder(TreeNode* T)
{
while (T)
{
printf("%c", T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
printf("\n");
}
int main()
{
TreeNode* T;
int index = 0;
char data[100];
printf("输入二叉树\n");
scanf("%s", data);
creatTree(&T, data, &index);
Queue* Q = initQueue();
preOrder(T);
printf("\n");
levelTraverse(Q, T);
return 0;
}
运行的时候提示creatTree函数里的*T = NULL;引发了未经处理的异常:写入访问权限冲突。编译器是visual studio2022
#include<stdio.h>
#include<stdlib.h>
typedef struct TreeNode {
char data;
struct TreeNode* lchild;
struct TreeNode* rchild;
}TreeNode;
typedef struct Queue {
TreeNode* data;
struct Queue* pre;
struct Queue* next;
}Queue;
void creatTree(TreeNode** T, char* data, int* index)
{
char ch = data[*index];
*index += 1;
if (ch == '#')
{
*T = NULL;
}
else {
*T = (TreeNode*)malloc(sizeof(TreeNode));
(*T)->data= ch;
}
creatTree(&((*T)->lchild), data, index);
creatTree(&((*T)->rchild), data, index);
}
Queue* initQueue()
{
Queue* Q = (Queue*)malloc(sizeof(Queue));
Q->data = NULL;
Q->next = NULL;
Q->pre = NULL;
return Q;
}
void enQueue(Queue* Q, TreeNode* data)
{
Queue* node = (Queue*)malloc(sizeof(Queue));
Queue* tag = Q;
while (tag->next != NULL)
{
tag = tag->next;//tag指向队尾
}
node->data = data;
node->next = NULL;
node->pre = tag;
tag->next = node;
}
int isEmpty(Queue* Q)
{
if (Q->next == NULL)
{
return 1;
}
else {
return 0;
}
}
Queue* deQueue(Queue* Q)
{
if (isEmpty)
{
return NULL;
}
Queue* first = Q->next;
Q->next = first->next;
return first;
}
void levelTraverse(Queue* Q,TreeNode* T)//层次遍历
{
enQueue(Q, T);
while (isEmpty(Q) == 0)
{
Queue* node = deQueue(Q);
printf("%c", node->data->data);
if (node->data->lchild)
{
enQueue(Q, node->data->lchild);
}
if (node->data->rchild)
{
enQueue(Q, node->data->rchild);
}
}
printf("\n");
}
void preOrder(TreeNode* T)
{
while (T)
{
printf("%c", T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
printf("\n");
}
int main()
{
TreeNode* T;
int index = 0;
char data[100];
printf("输入二叉树\n");
scanf("%s", data);
creatTree(&T, data, &index);
Queue* Q = initQueue();
preOrder(T);
printf("\n");
levelTraverse(Q, T);
return 0;
}
运行的时候提示creatTree函数里的*T = NULL;引发了未经处理的异常:写入访问权限冲突。编译器是visual studio2022
