作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试解决有关二叉树的问题,但我在构建树时可能出了问题。这似乎与我联系在一起,为什么当我尝试打印节点的数据时出现段错误:
// 3
// / \
// 5 2
// / \ / \
// 1 4 6 7
// \ /
// 9 8
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
void top_view(struct node * root) {
int array[100] = {0};
int count = 0;
struct node * temp;
if (root) {
if (root->left) {
temp = root;
while (temp->left) {
printf("%d ", temp->left->data);
// array[count] = temp->left->data;
count++;
temp = temp->left;
}
}
array[count] = root->data;
printf("%d\n", array[count]);
if (root->right != NULL) {
printf("right %d\n", root->right->data);
temp = root;
printf("%p\n", temp->right->right);
printf("%d\n", temp->right->right->data);
}
// for (int i = 0; i < 100; i++) {
// printf("%d ", array[i]);
// }
} else {
return ;
}
}
int main(int argc, char const *argv[])
{
// struct node *nine = (struct node*)malloc(sizeof(struct node*));
// nine->data = 9;
// nine->left = NULL;
// nine->right = NULL;
// struct node *ten = (struct node*)malloc(sizeof(struct node*));
// ten->data = 10;
// ten->left = NULL;
// ten->right = NULL;
// struct node *one = (struct node*)malloc(sizeof(struct node*));
// one->data = 1;
// one->left = ten;
// one->right = nine;
// struct node *four = (struct node*)malloc(sizeof(struct node*));
// four->data = 4;
// four->left = NULL;
// four->right = NULL;
// struct node *five = (struct node*)malloc(sizeof(struct node*));
// five->data = 5;
// five->left = one;
// five->right = four;
struct node *eight = (struct node*)malloc(sizeof(struct node*));
eight->data = 8;
eight->left = NULL;
eight->right = NULL;
struct node *six = (struct node*)malloc(sizeof(struct node*));
six->data = 6;
six->left = NULL;
six->right = NULL;
struct node *seven = (struct node*)malloc(sizeof(struct node*));
seven->data = 7;
seven->left = eight;
seven->right = NULL;
struct node *two = (struct node*)malloc(sizeof(struct node*));
two->data = 2;
two->left = six;
two->right = seven;
struct node *three = (struct node*)malloc(sizeof(struct node*));
three->data = 3;
three->left = NULL;
three->right = two;
top_view(three);
return 0;
}
输出是这样的:
3
right 2
0x7fc800000003
[1] 2091 segmentation fault ./a.out
我哪里出错了?
最佳答案
我刚刚对代码进行了编辑,它编译并且没有给出任何错误。我更改了使用 malloc
初始化新节点的方式,并删除了 cast
。您还必须检查 root->right->right
是否存在。这是您在代码顶部显示的三个。
/* 3
* / \
* 5 2
* / \ / \
* 1 4 6 7
* \ /
* 9 8
*/
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node* left;
struct node* right;
};
void top_view(struct node *root) {
int array[100] = {0};
int count = 0;
struct node *temp;
if (root) {
if (root->left) {
temp = root;
while (temp->left) {
printf("%d ", temp->left->data);
// array[count] = temp->left->data;
count++;
temp = temp->left;
}
}
array[count] = root->data;
printf("%d\n", array[count]);
if (root->right != NULL) {
printf("right %d\n", root->right->data);
temp = root;
/*You must check it*/
if(root->right->right != NULL){
printf("%p\n", temp->right->right);
printf("%d\n", temp->right->right->data);
}
}
/*for (int i = 0; i < 100; i++) {
printf("%d ", array[i]);
}*/
} else {
return ;
}
}
int main(int argc, char const *argv[])
{
struct node *nine = malloc(sizeof(struct node));
nine->data = 9;
nine->left = NULL;
nine->right = NULL;
/* struct node *ten = (struct node*)malloc(sizeof(struct node*));
ten->data = 10;
ten->left = NULL;
ten->right = NULL;*/
struct node *one = malloc(sizeof(struct node));
one->data = 1;
one->left = NULL;
one->right = nine;
struct node *four = malloc(sizeof(struct node));
four->data = 4;
four->left = NULL;
four->right = NULL;
struct node *five = malloc(sizeof(struct node));
five->data = 5;
five->left = one;
five->right = four;
/*You should cast the result of a malloc just if you need it*/
struct node *eight = malloc(sizeof(struct node));
eight->data = 8;
eight->left = NULL;
eight->right = NULL;
struct node *six = malloc(sizeof(struct node));
six->data = 6;
six->left = NULL;
six->right = NULL;
struct node *seven = malloc(sizeof(struct node));
seven->data = 7;
seven->left = eight;
seven->right = NULL;
struct node *two = malloc(sizeof(struct node));
two->data = 2;
two->left = six;
two->right = seven;
struct node *three = malloc(sizeof(struct node));
three->data = 3;
three->left = five;
three->right = two;
top_view(three);
return 0;
}
关于c - 二叉树顶 View 分割错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44287551/
我目前正在尝试编写一个 Treetop 语法来解析简单游戏格式文件,并且到目前为止它大部分都在工作。但是,出现了一些问题。 我不确定如何实际访问 Treetop 在解析后生成的结构。 有没有比我的字符
我正在实现具有语法的 DSL: "[keyword] or ([other keyword] and not [one more keyword])" 每个关键字都将转换为 bool 值(true,
我是一名优秀的程序员,十分优秀!