【问题标题】:Trying to make Binary Search Tree, but program freezes. Can you check my code?试图制作二叉搜索树,但程序冻结。你能检查我的代码吗?
【发布时间】:2020-08-12 22:25:50
【问题描述】:

这里面有什么我不明白的地方吗,或者这是我需要使用 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;

【问题讨论】:

  • 它可能不会“冻结”,它只是进入无限循环,除非您得到特定的崩溃结果。你有没有把它放到调试器中看看它卡在哪里?
  • 我试过了,但它还是冻结了。我把断点放在循环的顶部。
  • 当你说“冻结”是什么意思?代码不会“冻结”,计算机会。
  • 您在问题中使用了“崩溃”这个词,它是死机还是崩溃?
  • 它停止工作并在我的 IDE 窗口后面。没有错误。

标签: c pointers binary-search-tree


【解决方案1】:

当你创建一个节点时,里面的值被设置为无意义的(“垃圾值”),直到你明确地设置它们。 C 不会为您进行任何初始化,因此这些值是您运行代码之前 RAM 中的任何值。

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

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

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 以避免复制内存中的节点,但这与此处的答案无关。

【讨论】:

  • 哇,你让我觉得很特别 Elan Hamburger :) 围绕谁得到正确答案的文化是什么?您付出了更多努力,但 MLeblanc 在您刷新页面以查看最近的活动之前回答了。
  • 复选标记是“最佳”答案。最佳答案是最快的还是最完整的(或介于两者之间,或完全不同的标准)取决于您。您可以随时通过投票来表示您对您认为“有用”的答案(或问题)的支持。
  • 好的,你告诉我将左右节点设置为 NULL,对我的帮助更大。所以我把你的答案标记为正确,因为最后它有更多有用的细节。
【解决方案2】:

如果 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;

【讨论】:

  • 好的,我试试这个。只是我以前从未使用过堆,我希望我不需要。
  • 左边节点也是一样的,你必须先分配它,然后再设置它的值
  • 谢谢!我无法检查它是否有效,但程序没有崩溃,所以我认为它有效。我想使用堆毕竟不是那么复杂。知道为什么有人反对这个问题吗? @MLeblanc
猜你喜欢
  • 2019-04-09
  • 1970-01-01
  • 2013-04-14
  • 2011-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 1970-01-01
相关资源
最近更新 更多