-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTree.cs
127 lines (125 loc) · 5.04 KB
/
Tree.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Linq;
using System.Collections.Generic;
namespace TEArts.Etc.CollectionLibrary
{
#region Tree
public interface IItem
{
string Text { get; }
}
public class ItemBase : IItem
{
public ItemBase() { }
public ItemBase(string i) { Text = i; }
public virtual string Text { get; internal set; }
}
public interface ILeaf<T, Q> : IItem
where T : IItem
where Q : IItem
{
INode<T, Q> Parent { get; }
Q Object { get; }
INode<T, Q> Move(INode<T, Q> newNode);
}
public class LeafBase<T, Q> : ItemBase, ILeaf<T, Q>
where T : IItem
where Q : IItem
{
public INode<T, Q> Parent { get; private set; }
internal LeafBase() { }
public LeafBase(Q q) { Text = q.Text; Object = q; }
public virtual Q Object { get; internal set; }
public INode<T, Q> Move(INode<T, Q> newNode)
{
Parent.RemoveLeaf(Object);
Parent = newNode;
Parent.AddLeaf(Object);
return Parent;
}
public override string ToString()
{
return string.Format("{0}-:>{1}", (Parent == null ? "+-" : Parent.ToString()), Text);
}
}
public interface INode<T, Q> : IItem
where T : IItem
where Q : IItem
{
INode<T, Q> Parent { get; set; }
INode<T, Q> this[string key] { get; }
INode<T, Q> this[int index] { get; }
IDictionary<string, INode<T, Q>> Child { get; }
IDictionary<string, ILeaf<T, Q>> Leafs { get; }
string Perfix { get; }
ILeaf<T, Q> Values(string key);
ILeaf<T, Q> Values(int index);
INode<T, Q> AddChild(T t);
bool RemoveChild(T t);
ILeaf<T, Q> AddLeaf(Q q);
bool RemoveLeaf(Q q);
INode<T, Q> Create(IItem i);
}
public class NodeBase<T, Q> : ItemBase, INode<T, Q>
where T : IItem
where Q : IItem
{
public NodeBase()
{
Child = new Dictionary<string, INode<T, Q>>();
Leafs = new Dictionary<string, ILeaf<T, Q>>();
}
public NodeBase(IItem i)
: this()
{
Text = i.Text;
}
public INode<T, Q> this[string key] { get { if (Child.ContainsKey(key))return Child[key]; else return null; } }
public INode<T, Q> this[int index] { get { if (Child.Count > index)return Child[(Child.Keys.ToArray<string>())[index]]; else return null; } }
public virtual IDictionary<string, INode<T, Q>> Child { get; internal set; }
public virtual IDictionary<string, ILeaf<T, Q>> Leafs { get; internal set; }
[Newtonsoft.Json.JsonIgnore]
public virtual INode<T, Q> Parent { get; set; }
public virtual string Perfix { get { return (Parent == null ? "+-+" : Parent.Perfix + "--+") + Text; } }
public virtual ILeaf<T, Q> Values(string key) { if (Leafs.ContainsKey(key))return Leafs[key]; else return null; }
public virtual ILeaf<T, Q> Values(int index) { if (Leafs.Count > index) { return Leafs[(Leafs.Keys.ToArray<string>())[index]]; } else return null; }
public virtual INode<T, Q> AddChild(T t) { if (Child.ContainsKey(t.Text))return null; else if (t is INode<T, Q>) { Child.Add(t.Text, (t as INode<T, Q>)); (t as INode<T, Q>).Parent = this; return (t as INode<T, Q>); } else { INode<T, Q> r = Create(t); if (r != null) { Child.Add(t.Text, r); r.Parent = this; return r; } else return null; } }
public virtual bool RemoveChild(T t) { if (Child.ContainsKey(t.Text)) { Child.Remove(t.Text); return true; } else return false; }
public virtual ILeaf<T, Q> AddLeaf(Q q) { if (Leafs.ContainsKey(q.Text))return null; else { ILeaf<T, Q> r = new LeafBase<T, Q>(q); Leafs.Add(q.Text, r); return r; } }
public virtual bool RemoveLeaf(Q q) { if (Leafs.ContainsKey(q.Text)) { Leafs.Remove(q.Text); return true; } else return false; }
public virtual INode<T, Q> Create(IItem i) { throw new NotImplementedException(); }
public override string ToString()
{
string s = string.Empty;
//r = (Parent == null ? "+-" : Parent.ToString());
foreach (var n in Child.Values)
{
s = string.Format("{0}{1}{2}-+-{4}", s, Environment.NewLine, Perfix, Text, n.ToString());
}
foreach (var l in Leafs.Values)
{
s = string.Format("{0}{1}{2}-=>{4}", s, Environment.NewLine, Perfix, Text, l.Text);
}
return s;
}
}
public interface ITree<T, Q>
where T : IItem
where Q : IItem
{
INode<T, Q> Root { get; }
}
public class Tree<T, Q> : ItemBase, ITree<T, Q>
where T : IItem
where Q : IItem
{
public Tree() { }
public Tree(INode<T,Q> root) : this() { Root = root; }
public virtual INode<T, Q> Root { get; internal set; }
public override string ToString()
{
return Text + Environment.NewLine + Root.ToString();
}
}
#endregion
}