-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeNode.cpp
More file actions
48 lines (40 loc) · 1 KB
/
TreeNode.cpp
File metadata and controls
48 lines (40 loc) · 1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include "TreeNode.h"
template <typename T>
TreeNode<T>::TreeNode(T val) : value(val), children(nullptr), childrenCount(0)
{ }
template <typename T>
void TreeNode<T>::AddChild(TreeNode<T>* node)
{
TreeNode<T>** newChildren = new TreeNode<T>*[childrenCount + 1];
if (children != nullptr)
{
for (size_t i = 0; i < childrenCount; i++)
{
newChildren[i] = children[i];
}
delete[] children; // Deallocate the old children array
}
newChildren[childrenCount] = node;
childrenCount++;
children = newChildren; // Assign the new array to the children pointer
}
template <typename T>
void TreeNode<T>::AddChild(T value)
{
TreeNode<T>* node = new TreeNode<T>(value);
this->AddChild(node);
}
template <typename T>
void TreeNode<T>::Display()
{
std::cout << this->value << std::endl;
}
template <typename T>
void TreeNode<T>::DisplayChildren()
{
for (auto i = 0; i < childrenCount; i++)
{
children[i]->Display();
}
}