Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Hierarchy.Core
- {
- using System;
- using System.Collections.Generic;
- using System.Collections;
- using System.Linq;
- public class Hierarchy<T> : IHierarchy<T>
- {
- private Node root;
- private Dictionary<T, Node> nodesByValue;
- public Hierarchy(T root)
- {
- this.root = new Node(root);
- this.nodesByValue = new Dictionary<T, Node>();
- this.nodesByValue.Add(this.root.Value, this.root);
- }
- public int Count
- {
- get
- {
- return this.nodesByValue.Count;
- }
- }
- public void Add(T parent, T child)
- {
- if (!this.nodesByValue.ContainsKey(parent))
- {
- throw new ArgumentException();
- }
- if (this.nodesByValue.ContainsKey(child))
- {
- throw new ArgumentException();
- }
- var parentNode = this.nodesByValue[parent];
- var childNode = new Node(child, parentNode);
- parentNode.Children.Add(childNode);
- this.nodesByValue[child] = childNode;
- }
- public void Remove(T element)
- {
- if (!this.nodesByValue.ContainsKey(element))
- {
- throw new ArgumentException();
- }
- var currentNode = this.nodesByValue[element];
- if (currentNode.Parent == null)
- {
- throw new InvalidOperationException();
- }
- var parentNode = currentNode.Parent;
- var currentNodeChildren = currentNode.Children;
- if (currentNodeChildren.Count > 0)
- {
- foreach (var node in currentNodeChildren)
- {
- node.Parent = parentNode;
- parentNode.Children.Add(node);
- }
- }
- parentNode.Children.Remove(currentNode);
- this.nodesByValue.Remove(element);
- }
- public IEnumerable<T> GetChildren(T item)
- {
- if (!this.nodesByValue.ContainsKey(item))
- {
- throw new ArgumentException();
- }
- var parent = this.nodesByValue[item];
- var children = parent.Children.Select(n => n.Value);
- return children;
- }
- public T GetParent(T item)
- {
- if (!this.nodesByValue.ContainsKey(item))
- {
- throw new ArgumentException();
- }
- var currentNode = this.nodesByValue[item];
- //if (currentNode.Parent == null)
- //{
- // return default(T);
- //}
- //return currentNode.Parent.Value;
- return currentNode.Parent == null ? default(T) : currentNode.Parent.Value;
- }
- public bool Contains(T value)
- {
- return this.nodesByValue.ContainsKey(value);
- }
- public IEnumerable<T> GetCommonElements(Hierarchy<T> other)
- {
- var result = this.nodesByValue.Keys.Intersect(other.nodesByValue.Keys);
- return result;
- }
- public IEnumerator<T> GetEnumerator()
- {
- var result = new Queue<Node>();
- var current = this.root;
- result.Enqueue(current);
- while (result.Count > 0)
- {
- current = result.Dequeue();
- yield return current.Value;
- foreach (var child in current.Children)
- {
- result.Enqueue(child);
- }
- }
- }
- IEnumerator IEnumerable.GetEnumerator()
- {
- return this.GetEnumerator();
- }
- private class Node
- {
- public Node(T value, Node parent = null)
- {
- this.Value = value;
- this.Parent = parent;
- this.Children = new List<Node>();
- }
- public T Value { get; set; }
- public Node Parent { get; set; }
- public List<Node> Children { get; set; }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment