Advanced Programming - Binary Search Tree
A simple Binary Search Tree implementation for the Advanced Programming 2019-2020 course @ SISSA.
Node.hpp
1 #ifndef NODE_HPP
2 #define NODE_HPP
3 
4 #include <iostream> /* To use std::cout & co. */
5 #include <iomanip> /* For std::setw() */
6 #include <memory> /* For std::unique_ptr */
7 #include <utility> /* For std::pair */
8 
9 
16 namespace APutils {
17 
25  template <typename T>
26  struct Node {
33  T data;
35  std::unique_ptr<Node<T>> left;
37  std::unique_ptr<Node<T>> right;
40 
46  Node() noexcept :
47  data{},
48  left{nullptr},
49  right{nullptr},
50  parent{nullptr} {}
51 
60  Node(const T& data, Node<T>* parent) noexcept :
61  data{data},
62  left{nullptr},
63  right{nullptr},
64  parent{parent} {}
65 
74  Node(T&& data, Node<T>* parent) noexcept :
75  data{std::move(data)},
76  left{nullptr},
77  right{nullptr},
78  parent{parent} {}
79 
88  void printNode(std::ostream& os, const bool& printChildren) {
89  os << "[" << this << "] ";
90  os << "Key: " << std::setw(4) << data.first
91  << ", Value: " << std::setw(4) << data.second << std::endl;
92  if (printChildren) {
93  if (left) {
94  os << " Left -> "
95  << "[" << left.get() << "] "
96  << "Key: " << std::setw(4) << left .get()->data.first
97  << ", Value: " << std::setw(4) << left .get()->data.second << std::endl;
98  }
99  if (right) {
100  os << " Right -> "
101  << "[" << right.get() << "] "
102  << "Key: " << std::setw(4) << right.get()->data.first
103  << ", Value: " << std::setw(4) << right.get()->data.second << std::endl;
104  }
105  }
106  }
113  void printNode() {
114  printNode(std::cout, true);
115  }
116  };
117 
118 }
119 
120 
121 #endif // NODE_HPP
void printNode()
Utility function that prints a Node.
Definition: Node.hpp:113
T data
Data stored in this Node.
Definition: Node.hpp:33
void printNode(std::ostream &os, const bool &printChildren)
Utility function that prints a Node.
Definition: Node.hpp:88
std::unique_ptr< Node< T > > right
Right child of this Node.
Definition: Node.hpp:37
std::unique_ptr< Node< T > > left
Left child of this Node.
Definition: Node.hpp:35
Node(const T &data, Node< T > *parent) noexcept
Copy constructor with data.
Definition: Node.hpp:60
Node< T > * parent
Parent of this Node.
Definition: Node.hpp:39
Node(T &&data, Node< T > *parent) noexcept
Move constructor with data.
Definition: Node.hpp:74
Class that implements a node of a tree.
Definition: Node.hpp:26
Namespace that stands for "Advanced Programming Utils".
Definition: Iterator.hpp:13
Node() noexcept
Void constructor.
Definition: Node.hpp:46