Templated Binary Search Tree
Node.h
1 #ifndef Node_h
2 #define Node_h
3 
4 
5 #include <iostream>
6 #include <memory>
7 
8 template <typename T>
9 struct Node{
11  T data;
13  std::unique_ptr<Node<T>> left;
14 
16  std::unique_ptr<Node<T>> right;
17 
20 
25  explicit Node(const T& _data): data{_data}, left{nullptr}, right{nullptr},parent{nullptr}{}
26 
30  Node(): data{},left{nullptr}, right{nullptr},parent{nullptr}{}
31 
36  ~Node()=default;
37 
42  Node(const T& _data, Node<T>* _parent) noexcept:
43  data{_data},
44  left{nullptr},
45  right{nullptr},
46  parent{_parent} {}
47 
48 
55  Node(const std::unique_ptr<Node<T>> &ptn, Node<T> *_parent) : data{ptn->data}, parent{_parent}
56  {
57  if(ptn->right){
58  right = std::make_unique<Node<T>>(ptn->right, this);
59  }
60  if(ptn->left){
61  left = std::make_unique<Node<T>>(ptn->left, this);
62  }
63  }
64 
69  // Node(const T&& _data, Node<T>* _parent) noexcept:
70  // data{std::move(_data)},
71  // left{nullptr},
72  // right{nullptr},
73  // parent{_parent} {}
74 
79  void print(){
80  std::cout <<"Node has value: " << data.second <<"\n";
81  if (left) {
82  std::cout << "Left child has value: " <<left->data.second <<"\n";
83  }if(right){
84  std::cout << "Right child has value: " << right->data.second << "\n";
85  }if(parent){
86  std::cout << "Parent is Node with value: " <<parent->data.second <<"\n";
87  }
88 
89  }
90 
91 
92 
93 };
94 
95 
96 #endif /* Node_h */
Definition: Node.h:9
Node()
Default constructor.
Definition: Node.h:30
Node(const T &_data, Node< T > *_parent) noexcept
Copy constructor.
Definition: Node.h:42
std::unique_ptr< Node< T > > right
Unique pointer to the right child.
Definition: Node.h:16
Node< T > * parent
Raw pointer to the parent Node.
Definition: Node.h:19
void print()
Move constructor.
Definition: Node.h:79
Node(const T &_data)
Custom constructor.
Definition: Node.h:25
T data
Data to be stored in the Node.
Definition: Node.h:11
std::unique_ptr< Node< T > > left
Unique pointer to the left child.
Definition: Node.h:13
Node(const std::unique_ptr< Node< T >> &ptn, Node< T > *_parent)
Helper recursive function that, starting from a Node and its parent, copy all the tree recursively.
Definition: Node.h:55
~Node()=default
Default-generated destructor.