-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathTreeNode.cs
99 lines (79 loc) · 1.93 KB
/
TreeNode.cs
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
using System;
using System.Collections.Generic;
/**
* TreeNode.cs
* Author: Luke Holland (http://lukeholland.me/)
*/
namespace TreeView {
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);
}
}
}
}