Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- class Node {
- private:
- Node *parent, *left, *right;
- int value;
- public:
- // Constructors
- Node(const Node&);
- Node(const int&, Node* = nullptr, Node* = nullptr, Node* = nullptr);
- Node(const int&, Node*, Node*, const int&);
- Node(const int&, Node*, const int&, Node*);
- Node(const int&, Node*, const int&, const int&);
- Node(const int&, Node*, const int&);
- Node(const int&, const int&, Node* = nullptr);
- Node(const int&, const int&, const int&);
- Node(const int&, Node* = nullptr);
- Node(const int&);
- ~Node();
- // Set-methods
- void setparent(Node*);
- void setvalue(const int&);
- // Get-methods
- Node& getParent() const;
- Node& getLeft() const;
- Node& getRight() const;
- const int& getValue() const;
- };
- Node::Node(const Node& node) {
- this->value = node.getValue();
- this->parent = &node.getParent();
- this->left = &node.getLeft();
- this->right = &node.getRight();
- }
- Node::Node(const int& value, Node* parent, Node* left, Node* right) {
- this->value = value;
- if (parent != nullptr) {
- this->parent = parent;
- }
- if (left != nullptr) {
- this->left = left;
- this->left->setparent(this);
- }
- if (right != nullptr) {
- this->right = right;
- this->right->setparent(this);
- }
- }
- Node::Node(const int& value, Node* parent, Node* left, const int& right) {
- this->value = value;
- this->parent = parent;
- this->left = left;
- this->right = new Node(right, this);
- }
- Node::Node(const int& value, Node* parent, const int& left, Node* right) {
- this->value = value;
- this->parent = parent;
- this->left = new Node(left, this);
- this->right = right;
- }
- Node::Node(const int& value, Node* parent, const int& left, const int& right) {
- this->value = value;
- this->parent = parent;
- this->left = new Node(left, this);
- this->right = new Node(right, this);
- }
- Node::Node(const int& value, Node* left, const int& right) {
- this->value = value;
- this->left = left;
- this->right = new Node(right, this);
- }
- Node::Node(const int& value, const int& left, Node* right) {
- this->value = value;
- this->left = new Node(left, this);
- if (right != nullptr) {
- this->right = right;
- }
- }
- Node::Node(const int& value, const int& left, const int& right) {
- this->value = value;
- this->left = new Node(left, this);
- this->right = new Node(right, this);
- }
- Node::Node(const int& value, Node* parent) {
- this->value = value;
- if (parent != nullptr) {
- this->parent = parent;
- }
- }
- Node::~Node() {
- delete parent, left, right;
- }
- void Node::setparent(Node* parent) {
- this->parent = parent;
- }
- void Node::setvalue(const int& value) {
- this->value = value;
- }
- Node& Node::getParent() const {
- return *parent;
- }
- Node& Node::getLeft() const {
- return *left;
- }
- Node& Node::getRight() const {
- return *right;
- }
- const int& Node::getValue() const {
- return value;
- }
- void output(const Node& binaryTree) {
- std::cout << binaryTree.getValue() << std::endl;
- if (&binaryTree.getLeft() != nullptr) {
- output(binaryTree.getLeft());
- }
- if (&binaryTree.getRight() != nullptr) {
- output(binaryTree.getRight());
- }
- }
- int main() {
- Node binaryTree(1, new Node(10, 2, 11), new Node(8, new Node(3), 5));
- output(binaryTree);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement