Last active
March 11, 2019 16:36
-
-
Save Niakr1s/192977ef60ea18d7743bd84050120a22 to your computer and use it in GitHub Desktop.
Simple TreeNode list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "TreeNode.h" | |
int main(int argc, char *argv[]) | |
{ | |
TreeNode root("root"); | |
TreeNode node1("node1", &root); | |
{ | |
TreeNode node2("node2", &node1); | |
TreeNode node3("node3", &node2); | |
TreeNode node4("node4", &node3); | |
std::cout << "node2:\t\t"; | |
node2.print_to_end(); | |
node2.print() << endl; | |
TreeNode node2cpy(node2); | |
std::cout << "node2cpy:\t"; | |
node2cpy.print_to_end(); | |
node2cpy.print() << endl; | |
TreeNode node2assign = node2; | |
std::cout << "node2assign:\t"; | |
node2assign.print_to_end(); | |
node2.print() << "=="; | |
node2assign.print(); | |
node2.get_right()->print(); | |
std::cout << "=="; | |
node2assign.get_right()->print() << "\n"; | |
} | |
root.print_to_end(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "TreeNode.h" | |
int TreeNode::num = 0; | |
TreeNode::TreeNode(const string s, | |
TreeNode *l, TreeNode *r) : value(s), count(num++), left(l), right(r) | |
{ | |
if (l) | |
l->right = this; | |
}; | |
TreeNode::~TreeNode() | |
{ | |
cout << "destructor for: "; | |
print(); | |
if (left) | |
left->right = nullptr; | |
delete right; | |
}; | |
TreeNode::TreeNode(const TreeNode &other) : value(other.value), count(num++), left(nullptr), right(other.right) | |
{ | |
if (this == &other) | |
return; | |
right = new TreeNode(right->value, this, other.right); | |
auto r = right; | |
while (r) | |
{ | |
auto rr = r->right; | |
if (rr) | |
rr = new TreeNode(rr->value, r, rr->right); | |
r = rr; | |
} | |
}; | |
TreeNode &TreeNode::operator=(const TreeNode &other) | |
{ | |
if (this == &other) | |
return *this; | |
TreeNode tmp(other); | |
value = tmp.value; | |
count = num++; | |
left = tmp.left; | |
right = tmp.right; | |
return *this; | |
}; | |
ostream &TreeNode::print(ostream &out) | |
{ | |
out << value << ", count: " << count << ", addr: " << this << "\n"; | |
return out; | |
}; | |
ostream &TreeNode::print_to_end(ostream &out) | |
{ | |
cout << value; | |
TreeNode *r = right; | |
while (r) | |
{ | |
out << " - " << r->value; | |
r = r->right; | |
} | |
out << endl; | |
return out; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if !defined(TREE_NODE_H) | |
#define TREE_NODE_H | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
class TreeNode | |
{ | |
public: | |
// Constructor | |
TreeNode(const string s, TreeNode *l = nullptr, TreeNode *r = nullptr); | |
~TreeNode(); | |
// Copy control | |
TreeNode(const TreeNode &other); | |
TreeNode &operator=(const TreeNode &other); | |
ostream &print(ostream &out = cout); | |
ostream &print_to_end(ostream &out = cout); | |
// Getters | |
TreeNode *get_right() { return right; }; | |
TreeNode *get_left() { return left; }; | |
private: | |
static int num; | |
string value; | |
int count; | |
TreeNode *left, *right; | |
}; | |
#endif // TREE_NODE_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment