简体   繁体   English

C ++如果指针指向nullptr,我可以将其替换为数字吗?

[英]C++ if a pointer point to nullptr, can I replace it with number?

Hey guys, I encountered some problems about C++. 大家好,我遇到了一些有关C ++的问题。 Actually it's not the problem of language feature, otherwise it is related to kind of coding style. 实际上这不是语言功能的问题,否则与编码风格有关。
OK, Let's get to the point! 好的,让我们开始吧!
I try to write the AVL Tree and want to calculate the balance factors, and according the rule, the subtree without nodes(just a empty tree), its height should be treated as -1 . 我尝试编写AVL树并想要计算平衡因子,并且根据规则,没有节点的子树(只是一个空树),其高度应视为-1 Yeah, everything thinks fine, but when I write the code, use pointer to read Node class member, I cannot read the nullptr BAD ACCESS , so I add lots of conditions, which makes my code look bad. 是的,一切都很好,但是当我编写代码时,使用指针读取Node类成员时,无法读取nullptr BAD ACCESS ,因此添加了许多条件,这使我的代码看起来很糟糕。 Here is some parts of my code. 这是我的代码的某些部分。

struct Node{
    int key;
    int height;
    Node* left;
    Node* right;
    Node* parent;
    Node(void);
    Node(int key);
};

    while((parent_node->left->height - parent_node->right->height) <= 1
          ||(parent_node->left->height - parent_node->right->height) >= (-1))
    {
        parent_node = parent_node->parent;
        if(parent_node == nullptr) break;
    }

The result I want is that when the parent_node's left subtree is empty, its height will be treated as -1. 我想要的结果是,当parent_node的左子树为空时,其高度将被视为-1。 And the fact is, though it's empty, its height should not exist. 事实是,尽管它是空的,但它的高度不应该存在。 So in the code I only list four cases 所以在代码中我只列出了四种情况
1. left subtree == nullptr && right subtree == nullptr 1.左子树== nullptr &&右子树== nullptr
2. left subtree != nullptr && right subtree == nullptr 2.左子树!= nullptr &&右子树== nullptr
3. left subtree != nullptr && right subtree != nullptr 3.左子树!= nullptr &&右子树!= nullptr
4. left subtree == nullptr && right subtree != nullptr 4.左子树== nullptr &&右子树!= nullptr
Then I replace the code of height part with the value -1 respectively. 然后,我分别用值-1替换高度部分的代码。 It feels painful. 感觉很痛苦。 And this condition happens in my coding time many times, I want to find the better solution. 而且这种情况在我的编码时间中多次发生,我想找到更好的解决方案。

My English is not that good, so my description maybe sort of misleading, I will appreciate it if you help me in any way. 我的英语不好,所以我的描述可能有点误导,如果您能以任何方式帮助我,我将不胜感激。

Create a function that compute the height of a subtree including the special cases, and use that instead of accessing the ->height data member: 创建一个函数来计算包括特殊情况的子树的高度,并使用该函数代替访问->height数据成员:

int heightOfSubtree(Node* tree) {
    if (tree == nullptr) {
        return -1;
    }
    else {
        return tree-> height;
    }
}

then your code becomes: 那么您的代码将变为:

while((heightOfSubtree(parent_node->left) - heightOfSubtree(parent_node->right)) <= 1
      ||((heightOfSubtree(parent_node->left) - heightOfSubtree(parent_node->right)) >= (-1))
{
    ...
}

or better, you can define a member function in the Node structure such as this: 或者更好的是,您可以在Node结构中定义一个成员函数,如下所示:

bool Node::isBalanced() {
    int unb = heightOfSubtree(left) - heightOfSubtree(right);
    return (unb <= 1) || (unb >=-1);
} 

and your while condition becomes: 而您的while条件变为:

while(parent_node->isBalanced()) {
    ...
}

ps: I believe there is a logical error in your code: I am not sure the condition you are checking is correct, since it is always true (any number is either bigger than -1 or smaller than 1, for some both are true) ps:我相信您的代码中存在逻辑错误:我不确定您要检查的条件是否正确,因为它始终为真(任何数字都大于-1或小于1,其中有些都为真)

Unless I misunderstand, you could point to a sentinel node in stead of null as the terminator link. 除非我误会,否则您可以指向哨兵节点(而不是null)作为终结器链接。 Set the height of the sentinel to -1 and it doesn't need to be handled differently for that part of the algorithm. 将前哨的高度设置为-1,对于该算法的该部分无需进行其他处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM