- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我编写了这段代码来使用给定的前序和中序遍历创建二叉树。根据我的试运行,没有问题,树应该正确创建。但事实并非如此。我打印了树的顺序只是为了检查树是否已创建。但打印的不是中序,而是后序。经过进一步调查,我发现树本身没有正确创建,这就是错误出现的原因。创建的根的右侧部分没有任何内容。但根据试运行来看,没有问题。有人可以帮我找出我的错误吗?
示例输入:A乙deFCGHj我kd乙FeAGC我jHk
输出:dfebgljkhca
#include <stdio.h>
typedef struct Node
{
int data;
struct node *left;
struct node *right;
}node;
typedef node *tree;
tree root=NULL;
char pre[11];
char in[11];
static int i;
void create( tree temp, int start_left, int end_left, int start_right, int end_right )
{
if ( start_left <= end_left )
{
temp->left= (tree) malloc(sizeof( node ));
temp=temp->left;
temp->left=NULL;
temp->right=NULL;
int j;
for ( j=start_left; j<=end_left; j++)
{
if ( pre[i]==in[j] )
{
temp->data=pre[i];
break;
}
}
i++;
create( temp, start_left, j-1, j+1, end_left );
}
if ( start_right <= end_right )
{
temp->right= (tree) malloc(sizeof( node ));
temp=temp->right;
temp->left=NULL;
temp->right=NULL;
int j;
for ( j=start_right; j<=end_right; j++)
{
if ( pre[i]==in [j] )
{
temp->data=pre[i];
break;
}
}
i++;
create( temp, start_right, j-1, j+1, end_right );
}
return ;
}
void inorder_print(tree temp)
{
if ( temp!=NULL )
{
inorder_print(temp->left);
printf("%c",temp->data);
inorder_print(temp->right);
}
}
int main()
{
for( i=0; i<11; i++)
{
scanf("%c", &pre[i]);
fflush(stdin);
}
for( i=0; i<11; i++)
{
scanf("%c", &in[i]);
fflush(stdin);
}
int j;
for( j=0; j<11; j++)
{
if( pre[0]==in[j] )
{
root= (tree) malloc(sizeof( node ));
root->data=pre[0];
root->left=NULL;
root->right=NULL;
break;
}
}
i=1;
create( root, 0, j-1, j+1, 10);
inorder_print(root);
return 0;
}
最佳答案
更一般化的东西怎么样?我的建议有以下输出:
prorder:
a a b b d c c d e e f f g g h h j j l k k l
inorder:
a a b b c c d d e e f f g g h h j j k k l l
[a]
+--+-----+
[a] [b]
+--+-----------+
[b] [d]
+-----+-----+
[c] [e]
+--+--+ +--+-----+
[c] [d] [e] [f]
+--+-----+
[f] [g]
+--+-----+
[g] [h]
+--+-----+
[h] [j]
+--+-----------+
[j] [l]
+-----+
[k]
+--+--+
[k] [l]
代码是:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct s_tree
{
void *content;
size_t content_size;
struct s_tree *left;
struct s_tree *right;
} t_tree;
/*
** Tree content casted to given type
*/
#define TCONT(tree, t_type) ((t_type)((tree)->content))
/*
** Helpers
*/
void *ft_memdup(void const *ptr, size_t size)
{
void *result;
result = (void*)malloc(size);
if (result == NULL)
return (NULL);
memcpy(result, ptr, size);
return (result);
}
t_tree *ft_tree_new(void const *content, size_t content_size)
{
t_tree *result;
result = (t_tree*)malloc(sizeof(t_tree));
if (result == NULL)
return (NULL);
result->left = NULL;
result->right = NULL;
if (content == NULL)
{
result->content = NULL;
result->content_size = 0;
}
else
{
result->content = ft_memdup(content, content_size);
result->content_size = content_size;
}
return (result);
}
void ft_tree_add(
t_tree **root,
t_tree *new_branch,
int (*cmp)(void*, void*))
{
if (*root == NULL)
*root = new_branch;
else
{
if (cmp((*root)->content, new_branch->content) >= 0)
ft_tree_add(&(*root)->left, new_branch, cmp);
else
ft_tree_add(&(*root)->right, new_branch, cmp);
}
}
void ft_tree_map(t_tree *root, void (*f)(void*), char *map)
{
int i;
if (root != NULL)
for (i = 0; map[i]; i++)
{
switch (map[i])
{
case 'R':
f(root->content);
break;
case 'r':
ft_tree_map(root->right, f, map);
break;
case 'l':
ft_tree_map(root->left, f, map);
break;
}
}
}
/*
** Print tree function
*/
int _print_t(t_tree *tree, int is_left, int offset, int depth, char s[50][256])
{
char b[50];
int width = 3;
int i;
if (!tree)
return 0;
sprintf(b, "[%s]", TCONT(tree, char*));
int left = _print_t(tree->left, 1, offset, depth + 1, s);
int right = _print_t(tree->right, 0, offset + left + width, depth + 1, s);
for (i = 0; i < width; i++)
s[2 * depth][offset + left + i] = b[i];
if (depth && is_left)
{
for (i = 0; i < width + right; i++)
s[2 * depth - 1][offset + left + width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset + left + width + right + width/2] = '+';
} else if (depth && !is_left)
{
for (int i = 0; i < left + width; i++)
s[2 * depth - 1][offset - width/2 + i] = '-';
s[2 * depth - 1][offset + left + width/2] = '+';
s[2 * depth - 1][offset - width/2 - 1] = '+';
}
return left + width + right;
}
void print_tree(t_tree *tree)
{
char s[50][256];
for (int i = 0; i < 50; i++)
sprintf(s[i], "%100s", " ");
_print_t(tree, 0, 0, 0, s);
for (int i = 0; i < 50; i++)
printf("%s\n", s[i]);
}
//end of print tree function
void print_str(char *str)
{
printf("%s ", str);
}
int main()
{
t_tree *root;
char input[] = "abdefcghjlkdbfeagcljhk";
char tmp[2];
int i;
tmp[1] = 0;
root = NULL;
for (i = 0; input[i]; i++)
{
tmp[0] = input[i];
ft_tree_add(&root,
ft_tree_new(tmp, strlen(tmp) + 10),
(int (*)(void*, void*))(&strcmp));
}
//Preorder
printf("preorder:\n");
ft_tree_map(root, (void (*)(void*))(&print_str), "Rlr");
printf("\n");
//Inorder
printf("inorder:\n");
ft_tree_map(root, (void (*)(void*))(&print_str), "lRr");
printf("\n");
print_tree(root);
}
如果您想更改插入方式,请制作自己的比较函数,并将 &strcmp
更改为 &your_function
。
关于有人可以告诉我为什么我在这段代码中使用它的前序和后序创建的二叉树没有以所需的方式创建吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43401053/
我正在使用 Gunicorn 为 Django 应用程序提供服务,它工作正常,直到我将其超时时间从 30 秒更改为 900000 秒,我不得不这样做,因为我有一个用例需要上传和处理一个巨大的文件(过程
我有一个带有非常基本的管道的Jenkinsfile,它可以旋转docker容器: pipeline { agent { dockerfile { args '-u root' } } stag
在学习 MEAN 堆栈的过程中,我遇到了一个问题。每当我尝试使用 Passport 验证方法时,它都不会返回任何响应。我总是收到“localhost没有发送任何数据。ERR_EMPTY_RESPONS
在当今的大多数企业堆栈中,数据库是我们存储所有秘密的地方。它是安全屋,是待命室,也是用于存储可能非常私密或极具价值的物品的集散地。对于依赖它的数据库管理员、程序员和DevOps团队来说,保护它免受所
是否可以创建像图片上那样的边框?只需使用 css 边框属性。最终结果将是没 Angular 盒子。我不想添加额外的 html 元素。我只想为每个 li 元素添加 css 边框信息。 假设这是一个 ul
我是一名优秀的程序员,十分优秀!