【问题标题】:Problem with Binary Search tree code for insert operation用于插入操作的二叉搜索树代码问题
【发布时间】:2019-11-27 08:16:16
【问题描述】:

我无法从我的插入函数中的根节点访问左、右节点,终端输出证明了这一点。是什么导致了错误。

代码主体:

#include<iostream>

class Node {
    public:
    int key; int data;
    Node * left;
    Node* right;
    Node (int key, int data):key(key),data(data),right(nullptr),left(nullptr){std::cout<<"ooga booga"<<std::endl;}

    //Recursive function to insert an key into BST
    void insert( int key,int data)
    {
        std::cout<<"reached here.Current node now has key="<<this->key<<" while input key is "<<key <<std::endl;
        // if the root is null, create a new node an return it
        if (this == nullptr)
        {
            std::cout<<"in 1st if block "<<this->key<<std::endl;    
            this->key=key;
            this->data=data;
            return;
        }

        // if given key is less than the root node, recur for left subtree
        if (key < this->key)
        {   
            std::cout<<"in 2nd if block "<<this->key<<std::endl;    
            left->insert(key, data);
            std::cout<<"in else block 2"<<this->key<<std::endl;
        }

        // if given key is more than the root node, recur for right subtree
        else
        {
            std::cout<<"in else block "<<this->key<<std::endl;  
            right->insert(key, data);}
            std::cout<<"in else block 2"<<this->key<<std::endl; 
            return;
        }

// A utility function to do inorder traversal of BST 
void inorder() 
{ 
    if (this != nullptr) 
    { 
        this->left->inorder(); 
        printf("%d \n", this->key); 
        this->right->inorder(); 
    } 
} 



};


// Driver Program to test above functions 
int main() 
{ 
    /* Let us create following BST 
              50 
           /     \ 
          30      70 
         /  \    /  \ 
       20   40  60   80 */
    Node *root=new Node(50,10); 
    std::cout<<root<<std::endl;
    std::cout<<root->left<<std::endl;
    //root=nullptr;
    std::cout<<"reached here"<<std::endl;
    std::cout<<"reached here.Root now has key="<<root->key<<std::endl;
    root->insert( 0,10); 
    root->insert( 20,10); 
    root->insert( 40,10); 
    root->insert( 70,10); 
    root->insert(  60,10); 
    root->insert( 80,10); 
    std::cout<<"reached here 2"<<std::endl;
    // print inoder traversal of the BST 
    root->inorder(); 

    return 0; 
} 

输出:

ooga booga
0x7fffc10c6e70
0
reached here
reached here.Root now has key=50
reached here.Current node now has key=50 while input key is 0
in 2nd if block 50
Segmentation fault (core dumped)

【问题讨论】:

  • 在检查this 是一个nullptr 之后,为什么你立即尝试访问它的数据成员this-&gt;key?您刚刚检查了它是nullptrthis-&gt;key 不存在。
  • 嗨,这只是我在遇到分段错误之前添加的额外调试语句。
  • 在单个节点树上尝试您的代码。您可能会看到它也不起作用,这意味着您确实没有在最简单的情况下(单个节点)测试您的树。
  • 在用left=nullptrright=nullptr 初始化每个Node 对象后,为什么还要尝试访问left-&gt;insert?左边是nullptrnullptr 没有 left 成员。

标签: c++ binary-search-tree


【解决方案1】:

一般总结:

您正在使用left=nullptrright=nullptr 创建Node 对象。

您实际上从未将这些leftright 指针初始化为new Node

在尝试访问left-&gt;insert之前,您必须先创建一个new Node

【讨论】:

  • 是的,这是根本原因。作为 nullptr,left 没有任何作为 node* 指针的身份。因此编译器无法正确分配 insert() 函数给它
【解决方案2】:

因此,这是基于 MPops 回复对插入功能所做的唯一更改,现在可以使用:

void insert( int key,int data)
{
    if (key < this->key) {
        if(this->left ==nullptr)
            { left=new Node(key,data); return;}
        else 
            left->insert(key,data);
    }

    else {
        if(this->right ==nullptr)
            { right=new Node(key,data); return;}
        else 
            right->insert(key,data);
    }


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多