Created
May 11, 2017 10:10
-
-
Save luke161/f0165d475f6f485202187e014d265139 to your computer and use it in GitHub Desktop.
Simple tree data structure for use with C# and Unity. Start with a root TreeNode and add children as required.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TreeNode.cs | |
* Author: Luke Holland (http://lukeholland.me/) | |
*/ | |
using System; | |
using System.Collections.Generic; | |
public class TreeNode<T> | |
{ | |
public delegate bool TraversalDataDelegate(T data); | |
public delegate bool TraversalNodeDelegate(TreeNode<T> node); | |
private readonly T _data; | |
private readonly TreeNode<T> _parent; | |
private readonly int _level; | |
private readonly List<TreeNode<T>> _children; | |
public TreeNode(T data) | |
{ | |
_data = data; | |
_children = new List<TreeNode<T>>(); | |
_level = 0; | |
} | |
public TreeNode(T data, TreeNode<T> parent) : this(data) | |
{ | |
_parent = parent; | |
_level = _parent!=null ? _parent.Level+1 : 0; | |
} | |
public int Level { get { return _level; } } | |
public int Count { get { return _children.Count; }} | |
public bool IsRoot { get { return _parent==null; }} | |
public bool IsLeaf { get { return _children.Count==0; }} | |
public T Data { get { return _data; }} | |
public TreeNode<T> Parent { get { return _parent; }} | |
public TreeNode<T> this[int key] | |
{ | |
get { return _children[key]; } | |
} | |
public void Clear() | |
{ | |
_children.Clear(); | |
} | |
public TreeNode<T> AddChild(T value) | |
{ | |
TreeNode<T> node = new TreeNode<T>(value,this); | |
_children.Add(node); | |
return node; | |
} | |
public bool HasChild(T data) | |
{ | |
return FindInChildren(data)!=null; | |
} | |
public TreeNode<T> FindInChildren(T data) | |
{ | |
int i = 0, l = Count; | |
for(; i<l; ++i){ | |
TreeNode<T> child = _children[i]; | |
if(child.Data.Equals(data)) return child; | |
} | |
return null; | |
} | |
public bool RemoveChild(TreeNode<T> node) | |
{ | |
return _children.Remove(node); | |
} | |
public void Traverse(TraversalDataDelegate handler) | |
{ | |
if(handler(_data)){ | |
int i = 0, l = Count; | |
for(; i<l; ++i) _children[i].Traverse(handler); | |
} | |
} | |
public void Traverse(TraversalNodeDelegate handler) | |
{ | |
if(handler(this)){ | |
int i = 0, l = Count; | |
for(; i<l; ++i) _children[i].Traverse(handler); | |
} | |
} | |
} |
Hey,
There’s an example here: https://github.com/luke161/Unity-IMGUI-TreeView/blob/master/Assets/TreeView/Example/Editor/AssetTreeEditorWindow.cs <https://github.com/luke161/Unity-IMGUI-TreeView/blob/master/Assets/TreeView/Example/Editor/AssetTreeEditorWindow.cs>
You can use the tree with any kind of data.
…---
https://lukeholland.me
@luke161
+44 7986689680
On 29 Sep 2019, at 18:22, Juan Diego Forero ***@***.***> wrote:
Hi luke nice code, how I can initialize this tree and start to add classes data inside ? this tree recive any type of data ?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub <https://gist.github.com/f0165d475f6f485202187e014d265139?email_source=notifications&email_token=AA36MEAJ5RR2V6YVIXZDPBDQMDP4PA5CNFSM4I3TIPXKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFZSSM#gistcomment-3040550>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AA36MEAJO244AJCLET3KAULQMDP4PANCNFSM4I3TIPXA>.
Good job. Thanks.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Luke nice code, how I can initialize this tree and start to add classes data inside ? this tree recive any type of data ?