SleakEngine
0.1.0
C++23 multi-backend game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
Tree.hpp
Go to the documentation of this file.
1
#ifndef _TREE_H_
2
#define _TREE_H_
3
4
namespace
Sleak
5
{
6
/**
7
* @class Tree
8
* @brief Implements a binary tree data structure for hierarchical data organization.
9
*
10
* The BinaryTree class organizes data in a hierarchical manner with nodes and edges, where
11
* each node has at most two children. It is suitable for scenarios where you need to
12
* represent hierarchical relationships, such as file systems, decision trees, or
13
* expression trees. Use this class when you need to implement tree-based algorithms,
14
* search trees, or hierarchical data storage.
15
*
16
* Example Use Cases:
17
* - Representing a file system directory structure.
18
* - Implementing decision trees in machine learning.
19
* - Storing and evaluating arithmetic expressions.
20
* - Implementing binary search trees for efficient searching.
21
*
22
* Implementation Details:
23
* - Uses a tree node structure with pointers to left and right children.
24
* - Provides methods for inserting, searching, and traversing nodes.
25
* - Supports various tree traversal algorithms (e.g., in-order, pre-order, post-order).
26
* @ingroup utility
27
*/
28
template
<
typename
T>
29
class
Tree
30
{
31
private
:
32
/// One binary search tree node.
33
struct
Node
34
{
35
T data;
36
Node* left;
37
Node* right;
38
39
Node(
const
T& value) : data(value), left(
nullptr
), right(
nullptr
) {}
40
};
41
42
Node* root;
43
44
/// Inserts value under node per BST ordering, returning the (possibly new) subtree root.
45
Node* insertRecursive(Node* node,
const
T& value)
46
{
47
if
(!node)
48
return
new
Node(value);
49
50
if
(value < node->data)
51
node->left = insertRecursive(node->left, value);
52
else
53
node->right = insertRecursive(node->right, value);
54
55
return
node;
56
}
57
58
/// Leftmost (minimum) node in the subtree rooted at node.
59
Node* findMin(Node* node)
60
{
61
while
(node->left)
62
node = node->left;
63
return
node;
64
}
65
66
/// Removes value from the subtree rooted at node, returning the (possibly new) subtree root.
67
Node* removeRecursive(Node* node,
const
T& value)
68
{
69
if
(!node)
70
return
nullptr
;
71
72
if
(value < node->data)
73
node->left = removeRecursive(node->left, value);
74
else
if
(value > node->data)
75
node->right = removeRecursive(node->right, value);
76
else
77
{
78
if
(!node->left)
79
{
80
Node* temp = node->right;
81
delete
node;
82
return
temp;
83
}
84
else
if
(!node->right)
85
{
86
Node* temp = node->left;
87
delete
node;
88
return
temp;
89
}
90
91
Node* temp = findMin(node->right);
92
node->data = temp->data;
93
node->right = removeRecursive(node->right, temp->data);
94
}
95
return
node;
96
}
97
98
/// In-order traversal of the subtree rooted at node.
99
void
inOrderRecursive(Node* node)
100
{
101
if
(!node)
102
return
;
103
inOrderRecursive(node->left);
104
// Print function can be added here
105
inOrderRecursive(node->right);
106
}
107
108
/// Deletes every node in the subtree rooted at node.
109
void
clearRecursive(Node* node)
110
{
111
if
(!node)
112
return
;
113
clearRecursive(node->left);
114
clearRecursive(node->right);
115
delete
node;
116
}
117
118
public
:
119
Tree
() : root(nullptr) {}
120
121
/// Deletes every node in the tree.
122
~Tree
()
123
{
124
clearRecursive(root);
125
}
126
127
/// Inserts value, keeping BST ordering (duplicates go right).
128
void
insert
(
const
T& value)
129
{
130
root = insertRecursive(root, value);
131
}
132
133
/// Removes value from the tree, if present.
134
void
remove
(
const
T& value)
135
{
136
root = removeRecursive(root, value);
137
}
138
139
/// True if value exists in the subtree rooted at node.
140
bool
contains
(Node* node,
const
T& value)
141
{
142
if
(!node)
143
return
false
;
144
if
(node->data == value)
145
return
true
;
146
return
contains
(value < node->data ? node->left : node->right, value);
147
}
148
149
/// True if value exists anywhere in the tree.
150
bool
contains
(
const
T& value)
151
{
152
return
contains
(root, value);
153
}
154
155
/// Removes every node, leaving an empty tree.
156
void
clear
()
157
{
158
clearRecursive(root);
159
root =
nullptr
;
160
}
161
162
/// Traverses the tree in-order (left, node, right).
163
void
inOrder
()
164
{
165
inOrderRecursive(root);
166
}
167
};
168
}
169
170
#endif
// _BINARY_TREE_H_
Sleak::Tree::insert
void insert(const T &value)
Inserts value, keeping BST ordering (duplicates go right).
Definition
Tree.hpp:128
Sleak::Tree::contains
bool contains(Node *node, const T &value)
True if value exists in the subtree rooted at node.
Definition
Tree.hpp:140
Sleak::Tree::contains
bool contains(const T &value)
True if value exists anywhere in the tree.
Definition
Tree.hpp:150
Sleak::Tree::clear
void clear()
Removes every node, leaving an empty tree.
Definition
Tree.hpp:156
Sleak::Tree::remove
void remove(const T &value)
Removes value from the tree, if present.
Definition
Tree.hpp:134
Sleak::Tree::inOrder
void inOrder()
Traverses the tree in-order (left, node, right).
Definition
Tree.hpp:163
Sleak::Tree::~Tree
~Tree()
Deletes every node in the tree.
Definition
Tree.hpp:122
Sleak::Tree::Tree
Tree()
Definition
Tree.hpp:119
Sleak
Root namespace for everything the engine exposes.
Definition
Camera.hpp:10
include
public
Utility
Container
Tree.hpp
Generated on
for SleakEngine by
1.18.0