试图制作二进制搜索树,但程序冻结你能检查我的密码吗?

问题描述

其中是否存在一些我不了解的东西,或者是我需要使用 alloc 函数的那一次?我以前从未使用过的> stdlib.h

struct node {
    int value;
    node* left; 
    node* right; 
    node* parent;
};

这是使程序崩溃的代码。

    // Is the value > current node?
    // If it is,then go right,// If not then go left.
    // Is there a node forward?
    // If not then create a node with the value.
    node currentNode = rootNode;
    bool newNodeCreated = false;
    while (newNodeCreated == false){

        if (value > currentNode.value){                     // If the value is greater than the currents node. 
            if (currentNode.right != NULL){                     // Check the child on the right. 
                currentNode = *currentNode.right;                   // Set the current node to the child and restart the loop. 
            } else {                                                
                currentNode.right->value = value;                   // Set a new node.
                currentNode.right->parent = &currentNode;

                newNodeCreated = true;                      // End loop. 
            }
        } else {                                            // If the value is less than the current node. 
            if (currentNode.left != NULL){                      // Check the child on the left. 
                currentNode = *currentNode.left;                    // Set the current node to the child and restart the loop. 
            } else {                                                
                currentNode.left->value = value;                    // Set a new node. 
                currentNode.left->parent = &currentNode;

                newNodeCreated = true;                      // End loop. 
            }
        }
    }
    return;

解决方法

如果currentNode.right == NULL,则执行的第一条指令是:

currentNode.right-> value = value;

您必须先分配currentNode.right。

if (currentNode.right != NULL){                     // Check the child on the right. 
                currentNode = *currentNode.right;                   // Set the current node to the child and restart the loop. 
            } else {                                                
                currentNode.right->value = value;                   // Set a new node.
                currentNode.right->parent = &currentNode;
,

创建节点时,内部的值将设置为无用(“垃圾值”),直到您明确设置它们为止。 C不会为您进行任何初始化,因此这些值就是运行代码之前RAM中的值。

因此,当您检查左/右子项是否为NULL时,如果随机值恰好为NULL,则它们可能为0,但它们也可能不是,可能只是指向到可能不在程序地址空间中的随机内存地址。无论如何,在您创建一个节点之前,实际上并没有另一个节点。

分配内存非常简单。要创建一个新节点,您要做的就是

node* newNode = malloc(sizeof(node));
if (!newNode) {
    // Your program has run out of memory!
    exit(1);
}

,当您要删除该节点时,请在其指针上调用free

free(newNode);

实际上已经分配了内存,就可以使用该指针将新节点添加到树中。

// ...
} else {  
    currentNode.right = newNode;                                            
    currentNode.right->value = value;
    currentNode.right->parent = currentNode;
    // Explicitly set right and left on the new node to null
    currentNode.right->right = NULL;
    currentNode.right->left = NULL;

    newNodeCreated = true;
}
// ...

您可能希望将currentNode更改为node*而不是node,以避免复制内存中的节点,但这与此处的答案无关。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...