网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
08月07日漏签0天
c语言吧 关注:798,950贴子:4,358,146
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 3回复贴,共1页
<<返回c语言吧
>0< 加载中...

向大佬们请教

  • 只看楼主
  • 收藏

  • 回复
  • 丁丁车车长
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
跟着写了一个二叉树层次遍历的代码
#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


  • 丁丁车车长
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
问了一下chatgpt这是他帮我调整的可以运行了,我两个对比了一下感觉没有太大变化呀为什么会这样
#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 createTree(TreeNode** T, char* data, int* index) {
char ch = data[*index];
if (ch == '#') {
*T = NULL;
(*index)++;
return;
}
*T = (TreeNode*)malloc(sizeof(TreeNode));
if (*T == NULL) {
printf("Memory allocation failed\n");
exit(1);
}
(*T)->data = ch;
(*index)++;
createTree(&((*T)->lchild), data, index);
createTree(&((*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* tail = Q;
while (tail->next != NULL) {
tail = tail->next; // 找到队列尾部
}
node->data = data;
node->next = NULL;
node->pre = tail;
tail->next = node;
}
// 判断队列是否为空
int isEmpty(Queue* Q) {
return Q->next == NULL;
}
// 出队(返回头部数据并释放节点)
TreeNode* deQueue(Queue* Q) {
if (isEmpty(Q)) {
return NULL;
}
Queue* head = Q->next; // 头节点(第一个有效数据)
TreeNode* treeNode = head->data;
Q->next = head->next;
if (head->next) {
head->next->pre = Q;
}
free(head);
return treeNode;
}
// 层次遍历
void levelTraverse(TreeNode* T) {
if (T == NULL) {
return;
}
Queue* Q = initQueue();
enQueue(Q, T);
while (!isEmpty(Q)) {
TreeNode* node = deQueue(Q);
printf("%c ", node->data);
if (node->lchild) enQueue(Q, node->lchild);
if (node->rchild) enQueue(Q, node->rchild);
}
printf("\n");
free(Q);
}
// 先序遍历
void preOrder(TreeNode* T) {
if (T) {
printf("%c ", T->data);
preOrder(T->lchild);
preOrder(T->rchild);
}
}
// 释放二叉树
void freeTree(TreeNode* T) {
if (T == NULL) {
return;
}
freeTree(T->lchild);
freeTree(T->rchild);
free(T);
}
// 测试
int main() {
TreeNode* T = NULL;
int index = 0;
char data[100]; // 分配足够的空间
printf("输入二叉树(例如:ABD##E##CF##G##):\n");
scanf("%s", data);
createTree(&T, data, &index);
printf("先序遍历: ");
preOrder(T);
printf("\n");
printf("层次遍历: ");
levelTraverse(T);
freeTree(T);
return 0;
}


2025-08-07 00:24:14
广告
不感兴趣
开通SVIP免广告
  • 油炸不良人
  • 帕秋莉糕
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
起码人家多了一个T == NULL的判断和处理,避免使用空指针


  • GTA小鸡
  • 吧主
    14
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
*T = NULL之后需要return,否则递归没有出口。


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 3回复贴,共1页
<<返回c语言吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示