Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- template <typename T>
- class smart_pointer {
- T* m_obj;
- public:
- // Отдаем ему во владение некий объект
- smart_pointer(T* obj)
- : m_obj(obj)
- { }
- // По выходу из области видимости этот объект мы уничтожим
- ~smart_pointer() {
- delete m_obj;
- }
- // Перегруженные операторы<
- // Селектор. Позволяет обращаться к данным типа T посредством "стрелочки"
- T* operator->() { return m_obj; }
- // С помощью такого оператора мы можем разыменовать указатель и получить ссылку на
- // объект, который он хранит
- T& operator* () { return *m_obj; }
- };
- struct BinaryTree
- {
- BinaryTree* l = nullptr;
- BinaryTree* r = nullptr;
- int* data = nullptr;
- BinaryTree() {}
- BinaryTree(const int& a);
- void DeleteData();
- void AddNode(const int& a);
- void PrintTree();
- ~BinaryTree();
- };
- BinaryTree::BinaryTree(const int& a) {
- data = new int;
- *data = a;
- }
- void BinaryTree::PrintTree() {
- if (l != nullptr) {
- l->PrintTree();
- }
- if (data != nullptr) {
- std::clog << *data << '\n';
- }
- if (r != nullptr) {
- r->PrintTree();
- }
- }
- void BinaryTree::DeleteData() {
- if (l != nullptr) {
- delete l;
- }
- if (r != nullptr) {
- delete r;
- }
- delete data;
- data = nullptr;
- return;
- }
- void BinaryTree::AddNode(const int& a) {
- if (data == nullptr) {
- data = new int;
- *data = a;
- return;
- }
- if (*data < a) {
- if (r != nullptr) {
- r->AddNode(a);
- return;
- }
- r = new BinaryTree(a);
- return;
- }
- else {
- if (l != nullptr) {
- l->AddNode(a);
- return;
- }
- l = new BinaryTree(a);
- return;
- }
- }
- BinaryTree::~BinaryTree() {
- DeleteData();
- }
- void BalanceTree() {
- //
- }
- int main() {
- int a = 5;
- BinaryTree t(a);
- t.AddNode(6);
- t.AddNode(4);
- t.AddNode(3);
- t.PrintTree();
- std::clog << *t.data << '\n';
- smart_pointer<BinaryTree> kek(new BinaryTree(3));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement