From c04cd8a9565d34b0d7b9fb7c7654517991cf6d25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20L=C3=BCbe=C3=9F?= Date: Wed, 15 Sep 2021 13:25:57 +0200 Subject: [PATCH] Added generic TreeNode class --- GlitchyEngine/src/Collections/TreeNode.bf | 48 +++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 GlitchyEngine/src/Collections/TreeNode.bf diff --git a/GlitchyEngine/src/Collections/TreeNode.bf b/GlitchyEngine/src/Collections/TreeNode.bf new file mode 100644 index 0000000..ab91281 --- /dev/null +++ b/GlitchyEngine/src/Collections/TreeNode.bf @@ -0,0 +1,48 @@ +using System.Collections; + +namespace GlitchyEngine.Collections +{ + public class TreeNode + { + public T Value; + + public List 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; + } + } +}