Skip to content

Instantly share code, notes, and snippets.

@b26168
Created December 22, 2016 09:19
Show Gist options
  • Save b26168/a09cd8b3e52a21044f6611c82739acd2 to your computer and use it in GitHub Desktop.
Save b26168/a09cd8b3e52a21044f6611c82739acd2 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InfraStructureSpace.LayerTree
{
public class Leaf : Tree
{
public override List<Tree> Children
{
get
{
return new List<Tree>();
}
}
public Leaf(String key)
: base(key)
{
}
public override void Add(Tree child)
{
}
public override void Remove(Tree child)
{
}
public override void RemoveSelf(Tree newParentForChildren)
{
parent.Remove(this);
Parent = null;
}
public override void UpdateLevel()
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
namespace InfraStructureSpace.LayerTree
{
public class Node : Tree
{
private List<Tree> _children = new List<Tree>();
public override List<Tree> Children
{
get
{
return _children;
}
}
public Node(String key)
: base(key)
{
}
public override void Add(Tree child)
{
if (_children.Contains(child))
return;
child.Parent = this;
_children.Add(child);
UpdateLevel();
}
public override void Remove(Tree child)
{
child.Parent = null;
_children.Remove(child);
}
public override void RemoveSelf(Tree newParentForChildren)
{
foreach (Tree child in Children)
newParentForChildren.Add(child);
_children.Clear();
_children = null;
parent.Remove(this);
Parent = null;
}
public override void UpdateLevel()
{
int childLevel = level + 1;
foreach (Tree child in _children)
{
child.Level = childLevel;
child.UpdateLevel();
}
}
public Tree GetChild(String key)
{
return _children.Find(item => item.Key == key);
}
/// <summary>
/// 搜尋一個節點, 使用廣度優先搜尋法 (Breadth-first search).
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public Tree Search(String key)
{
if (this.key.Equals(key))
return this;
Queue<Tree> nodeQueue = new Queue<Tree>();
nodeQueue.Enqueue(this);
while (nodeQueue.Count() > 0)
{
Tree tmpNode = nodeQueue.Dequeue();
foreach (Tree child in tmpNode.Children)
{
if (child.Key.Equals(key))
return child;
nodeQueue.Enqueue(child);
}
}
return null;
}
/// <summary>
/// 取得包含本身以及以下的所有子孫節點.
/// </summary>
/// <returns></returns>
public List<Tree> GetAllNodes()
{
List<Tree> list = new List<Tree>();
Queue<Tree> nodeQueue = new Queue<Tree>();
nodeQueue.Enqueue(this);
while (nodeQueue.Count() > 0)
{
Tree tmpNode = nodeQueue.Dequeue();
list.Add(tmpNode);
foreach (Tree child in tmpNode.Children)
nodeQueue.Enqueue(child);
}
return list;
}
}
}
using System;
using System.Collections.Generic;
namespace InfraStructureSpace.LayerTree
{
public abstract class Tree
{
protected String key;
public String Key
{
get
{
return key;
}
}
protected Tree parent;
public Tree Parent
{
get
{
return parent;
}
set
{
parent = value;
}
}
public abstract List<Tree> Children { get; }
protected int level;
public int Level
{
get
{
return level;
}
set
{
level = value;
}
}
public Tree(String key)
{
this.key = key;
parent = null;
level = 0;
}
public abstract void Add(Tree child);
public abstract void Remove(Tree child);
public abstract void RemoveSelf(Tree newParentForChildren);
public abstract void UpdateLevel();
public void MoveTo(Tree newParent)
{
if (parent == newParent)
return;
parent.Remove(this);
newParent.Add(this);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment