-2

I've got to create small binary tree for class using smart pointers. Everything is okay when ChildL and ChildR are public members and I use them directly, but I am supposed to create tree with private members and access via functions. The result in console is just: root Can you help me ? I am using visual studio 2015.

#include <iostream>
#include <memory>
#include <string>
using namespace std;

class Node
{
public:
    Node(string n) : name(n) {}
    shared_ptr<Node> getChildL()
    {
        return ChildL;
    }
    shared_ptr<Node> getChildR()
    {
        return ChildR;
    }
    shared_ptr<Node> createChildL(string n)
    {
        return shared_ptr<Node>(new Node(n));
    }
    shared_ptr<Node> createChildR(string n)
    {
        return make_shared<Node>(n);
    }
    string getName() 
    { 
        return name;
    }
private:
    string name;
    shared_ptr<Node> ChildL, ChildR;
};

void PrintTree(shared_ptr<Node> tree)
{
    if (tree) 
    {
        cout << tree->getName() << endl;
        PrintTree(tree->getChildL());
        PrintTree(tree->getChildR());
    }
}

int main()
{
    shared_ptr<Node> root = make_shared<Node>("root");
    root->getChildL() = root->createChildL("leaf");
    PrintTree(root);
    cin.get();
}
haxpiotr
  • 1
  • 1
  • 3
    `getChildL` and `getChildR` return `shared_ptr`, but should return `shared_ptr&` if you expect this to work. (N.b. I don't think it's a good design.) – ildjarn Nov 13 '16 at 20:01
  • 1
    Nowhere in the shown code is `ChildL` or `ChildR` being assigned, so how exactly do you expect them to be set, to form an actual tree? P.S. [get rid of `using namespace std;`, it's a bad programming practice.](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Sam Varshavchik Nov 13 '16 at 20:06
  • Are your public functions allowed to return references to the private members or do you have to use get & set functions? – wally Nov 13 '16 at 20:21
  • I have to use get and set functions – haxpiotr Nov 13 '16 at 21:20

1 Answers1

1

This member:

shared_ptr<Node> createChildL(string n) {
    return shared_ptr<Node>(new Node(n));
}

Doesn't assign to ChildL. It just creates a new node embeds it in a shared pointer and then returns it.

You might (I suppose) implement:

std::shared_ptr<Node> createChildL(std::string n) {
    ChildL=std::make_shared<Node>(n);
    return ChildL;
}

However that's not the best way. If all you're trying to do is encapsulation consider implementing get/set pairs:

void setChildL(const std::shared_ptr<Node>& left) {
    ChildL=left;
}

You presumably want to build a BinaryTree class to handle the insertion / removal of nodes in a manner obeying the tree invariant (e.g. ChildL->n <= n and ChildR->n >n for every node.).

Persixty
  • 8,165
  • 2
  • 13
  • 35
  • 1
    Well I guess this is the correct way, because I have to use certain interface. Using your modified createChild seems to be solution :) Thanks! – haxpiotr Nov 13 '16 at 22:12