/
lyapdy
/
Object_oriented_programming_cpp_318
Обзор
Документация
Войти
/
lyapdy
/
Object_oriented_programming_cpp_318
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
lab8/task_4.cpp
50 строк
1 KB
danillyapunov
tasks 7 and 8 added
30 ноя 2025, 14:31
30 ноя 2025, 14:31
5a9b53a
Код
Авторство
О чём код?
#include "task_4.h" #include <iostream> #include <vector> #include <memory> class TreeNode::TreeNodeImpl { public: std::vector<TreeNode*> children; // ����� 㪠��⥫� ~TreeNodeImpl() { for (auto child : children) { delete child; } } }; TreeNode::TreeNode(int val) : value(val), impl(new TreeNodeImpl()) { } TreeNode::~TreeNode() { delete impl; } TreeNode* TreeNode::AddChild(int child_value) { TreeNode* node = new TreeNode(child_value); node->parent = this; impl->children.push_back(node); return node; } void TreeNode::Print(int depth) const { for (int i = 0; i < depth; ++i) { std::cout << " "; } std::cout << "- " << value << "\n"; for (const auto& child : impl->children) { child->Print(depth + 1); } } void task_4() { TreeNode root(1); auto left_son = root.AddChild(10); auto middle_son = root.AddChild(20); auto right_son = root.AddChild(30); left_son->AddChild(100); left_son->AddChild(200); root.Print(); }