mirror of
https://github.com/aharabada/glitchy-engine-beef.git
synced 2026-09-05 13:01:52 +00:00
Added generic TreeNode class
This commit is contained in:
@@ -0,0 +1,48 @@
|
|||||||
|
using System.Collections;
|
||||||
|
|
||||||
|
namespace GlitchyEngine.Collections
|
||||||
|
{
|
||||||
|
public class TreeNode<T>
|
||||||
|
{
|
||||||
|
public T Value;
|
||||||
|
|
||||||
|
public List<Self> Children = new .() ~ DeleteContainerAndItems!(_);
|
||||||
|
|
||||||
|
public this() {}
|
||||||
|
|
||||||
|
public this(T value)
|
||||||
|
{
|
||||||
|
Value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Self AddChild(T value)
|
||||||
|
{
|
||||||
|
for (var child in Children)
|
||||||
|
{
|
||||||
|
if(child.Value == value)
|
||||||
|
return child;
|
||||||
|
}
|
||||||
|
|
||||||
|
Self newChild = new .(value);
|
||||||
|
|
||||||
|
Children.Add(newChild);
|
||||||
|
|
||||||
|
return newChild;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Self FindNode(T value)
|
||||||
|
{
|
||||||
|
if (Value == value)
|
||||||
|
return this;
|
||||||
|
|
||||||
|
for (var child in Children)
|
||||||
|
{
|
||||||
|
var childResult = child.FindNode(value);
|
||||||
|
if (childResult != null)
|
||||||
|
return childResult;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user