二叉树代码适用于 Linux 但不适用于 Windows 10

Binary tree code works on Linux but not on Windows 10

我的代码在 Linux 上运行,没有内存错误,当 运行 在 Linux 上时树正确构建,但是当我 运行 在 Windows 卡住并终止。

struct node {
    char letter;
    char *string;
    int last_node;
    struct node *left;
    struct node *right;
};

static struct node nodes[] =
{
    {'[=12=]', ""},
    {'E', "."},
    {'T', "-"},
    {'I', ".."},
    {'A', ".-"},
    {'N', "-."},
    {'M', "--"},
    {'S', "..."},
    {'U', "..-"},
    {'R', ".-."},
    {'W', ".--"},
    {'D', "-.."},
    {'K', "-.-"},
    {'G', "--."},
    {'O', "---"},
    {'H', "...."},
    {'V', "...-"},
    {'F', "..-."},
    {'[=12=]', "..--"},
    {'L', ".-.."},
    {'[=12=]', ".-..-"},
    {'P', ".--."},
    {'J', ".---"},
    {'B', "-..."},
    {'X', "-..-"},
    {'C', "-.-."},
    {'Y', "-.--"},
    {'Z', "--.."},
    {'Q', "--.-"},
    {'[=12=]', "---."},
    {'[=12=]', "----"},
    {'5', "....."},
    {'4', "....-"},
    {'[=12=]', "...-."},
    {'3', "...--"},
    {'[=12=]', "..-.."},
    {'[=12=]', "..-.-"},
    {'[=12=]', "..--."},
    {'2', "..---"},
    {'[=12=]', ".-..."},
    {'[=12=]', ".-..-"},
    {'[=12=]', ".-.-."},
    {'[=12=]', ".-.--"},
    {'[=12=]', ".--.."},
    {'[=12=]', ".--.-"},
    {'[=12=]', ".---."},
    {'1', ".----"},
    {'6', "-...."},
    {'[=12=]', "-...-"},
    {'/', "-..-."},
    {'[=12=]', "-..--"},
    {'[=12=]', "-.-.."},
    {'[=12=]', "-.-.-"},
    {'[=12=]', "-.--."},
    {'[=12=]', "-.---"},
    {'7', "--..."},
    {'[=12=]', "--..-"},
    {'[=12=]', "--.-."},
    {'[=12=]', "--.--"},
    {'8', "---.."},
    {'[=12=]', "---.-"},
    {'9', "----."},
    {'0', "-----"},
    {.last_node = 1}
};

struct node *
tree_insert(struct node *root, struct node *selnode_addr, char *string)
{
    if (string[0] == '.')
    {
        if (string[1] == 0)
        {
            return root -> left = selnode_addr;
        }
        return tree_insert(root -> left, selnode_addr, string + 1);
    }

    if (string[1] == 0)
    {
        return root -> right = selnode_addr;
    }
    return tree_insert(root -> right, selnode_addr, string + 1);
}

int
main(void)
{
    // constructs the binary tree.
    for (struct node *nodeptr = nodes + 1; !nodeptr -> last_node; nodeptr++)
    {
        tree_insert(nodes, nodeptr, nodeptr -> string);
    }

    puts("test");
    return 0;
}

在 Linux 它 运行s 并打印 'test' 并通过 valgrind 没有内存错误我已经在 GDB 中验证树构建正确,但是在 Windows 它挂了一小段时间,然后似乎崩溃了。我不知道为什么。

更新

新数组:

static struct node nodes[] =
{
    {'[=13=]', ""},
    {'E', "."},
    {'T', "-"},
    {'I', ".."},
    {'A', ".-"},
    {'N', "-."},
    {'M', "--"},
    {'S', "..."},
    {'U', "..-"},
    {'R', ".-."},
    {'W', ".--"},
    {'D', "-.."},
    {'K', "-.-"},
    {'G', "--."},
    {'O', "---"},
    {'H', "...."},
    {'V', "...-"},
    {'F', "..-."},
    {'[=13=]', "..--"},
    {'L', ".-.."},
    {'P', ".--."},
    {'J', ".---"},
    {'B', "-..."},
    {'X', "-..-"},
    {'C', "-.-."},
    {'Y', "-.--"},
    {'Z', "--.."},
    {'Q', "--.-"},
    {'[=13=]', "---."},
    {'[=13=]', "----"},
    {'5', "....."},
    {'4', "....-"},
    {'3', "...--"},
    {'2', "..---"},
    {'1', ".----"},
    {'6', "-...."},
    {'/', "-..-."},
    {'7', "--..."},
    {'8', "---.."},
    {'9', "----."},
    {'0', "-----"},
    {.last_node = 1}
};

向树中插入字符串时,您的代码假定该字符串的每个前缀都已插入。这主要是因为字符串按长度排序。

然而,字符串 .-.-..-.-- 被插入到树中,而它们的前缀 .-.- 从未被插入。这会导致使用 root == NULL 递归调用 tree_insert。令我惊讶的是,您在 Linux 上并没有像我一样崩溃。

在您的固定代码中,您删除了 .-.-..-.-- 字符串,因此它可以正常工作。