The doubly-linked list used in the book is defined as follows:
template <typename T>
struct BSTNode {
T data;
shared_ptr<BSTNode<T>> left, right;
};
However, this creates a cyclic dependency which will not allow any of the nodes to be destroyed. As an example:
template <typename T>
struct BSTNode {
T data;
shared_ptr<BSTNode<T>> left, right;
BSTNode(T data) : data(data) {
cout << "Created: " << data << endl;
}
~BSTNode() {
cout << "Destroyed: " << data << endl;
}
};
int main() {
auto a = make_shared<BSTNode<char>>('a');
auto b = make_shared<BSTNode<char>>('b');
a->right = b;
b->left = a;
return 0;
}
This can be resolved by making one of the pointers weak or raw.