-1

I used row pointers to implement the binary search tree data structure and it worked perfectly, but when I replaced the row pointers with shared_ptr it compiles successfully but the program crashes due to unknown run-time error. Could you please help in this?

#include<iostream>
#include<memory>
class node{
public:
int data;
std::shared_ptr<node>left = std::make_shared<node>();
std::shared_ptr<node>right = std::make_shared<node>();
};

std::shared_ptr<node>CreateNode(int x);
void print_tree(const std::shared_ptr<node>&x);

int main(){
    auto root = std::make_shared<node>();
    root = CreateNode(12);
    root->left = CreateNode(9);
    root->right = CreateNode(14);
    root->left->right = CreateNode(10);
    root->left->right = CreateNode(11);
    print_tree(root);
    return 0;
}

std::shared_ptr<node>CreateNode(int x){
    std::shared_ptr<node>NewNode = std::make_shared<node>();
    NewNode->data = x;
    NewNode->left = NewNode->right = nullptr;
    return NewNode;
}

void print_tree(const std::shared_ptr<node>&x){ 
    if(x==nullptr) return;
    std::cout<<x->data<<std::endl;
    print_tree(x->left);
    print_tree(x->right);
}
  • 5
    Each node you create creates infinitely many more nodes, becase each node tries to create both `left` and `right` by itself. – Yksisarvinen May 20 '22 at 14:28
  • 4
    If ever there was a [explain-to-your-rubber-duck](https://en.wikipedia.org/wiki/Rubber_duck_debugging) moment, this is it. The moment you create the root node, it auto-creates a left and right node, each of which create a left and right node, each of which create a left and right node, each of which.... Seeing the problem? – WhozCraig May 20 '22 at 14:30
  • 2
    To fix change `std::shared_ptrleft = std::make_shared();` to `std::shared_ptrleft;` and do the same for `right`. This way you won't try to create an infinite number of `node`s. – drescherjm May 20 '22 at 14:31
  • The essential skill of [using a debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) will identify problems like these. – Drew Dormann May 20 '22 at 14:34
  • @drescherjm That's right! thx it works fine now – Zerocool_m12 May 20 '22 at 14:41
  • Also `CreateNode` should not modify the members of the new node. Write a `Node(int x)` constructor to initialize the `Node` properly. `CreateNode` then simply becomes `return std::make_shared(x);` – Goswin von Brederlow May 20 '22 at 17:11

1 Answers1

2
#include<iostream>
#include<memory>

class node
{
public:
    int data;
    std::shared_ptr<node>left;
    std::shared_ptr<node>right;
};

std::shared_ptr<node>CreateNode(int x);
void print_tree(const std::shared_ptr<node>&x);

int main()
{
    auto root = CreateNode(12);
    root->left = CreateNode(9);
    root->right = CreateNode(14);
    root->left->right = CreateNode(10);
    root->left->right = CreateNode(11);
    print_tree(root);
    return 0;
}

std::shared_ptr<node>CreateNode(int x)
{
    std::shared_ptr<node>NewNode = std::make_shared<node>();
    NewNode->data = x;
    NewNode->left = NewNode->right = nullptr;
    return NewNode;
}

void print_tree(const std::shared_ptr<node>&x)
{
    if(x==nullptr) return;
    std::cout<<x->data<<std::endl;
    print_tree(x->left);
    print_tree(x->right);
}

This works on my machine. I made left and right pointers in node class initially equal to nullptr, instead of creating new node because you can't know is it going to be used. And root to be initialized by result of CreateNode function.

DimitrijeCiric
  • 446
  • 1
  • 10