kema

Hierarchy

Jan 13th, 2019
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. namespace Hierarchy.Core
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Collections;
  6.     using System.Linq;
  7.  
  8.     public class Hierarchy<T> : IHierarchy<T>
  9.     {
  10.         private Node root;
  11.         private Dictionary<T, Node> nodesByValue;
  12.  
  13.         public Hierarchy(T root)
  14.         {
  15.             this.root = new Node(root);
  16.             this.nodesByValue = new Dictionary<T, Node>();
  17.             this.nodesByValue.Add(this.root.Value, this.root);
  18.         }
  19.  
  20.         public int Count
  21.         {
  22.             get
  23.             {
  24.                 return this.nodesByValue.Count;
  25.             }
  26.         }
  27.  
  28.         public void Add(T parent, T child)
  29.         {
  30.  
  31.             if (!this.nodesByValue.ContainsKey(parent))
  32.             {
  33.                 throw new ArgumentException();
  34.             }
  35.  
  36.             if (this.nodesByValue.ContainsKey(child))
  37.             {
  38.                 throw new ArgumentException();
  39.             }
  40.  
  41.             var parentNode = this.nodesByValue[parent];
  42.             var childNode = new Node(child, parentNode);
  43.  
  44.             parentNode.Children.Add(childNode);
  45.             this.nodesByValue[child] = childNode;
  46.         }
  47.  
  48.         public void Remove(T element)
  49.         {
  50.             if (!this.nodesByValue.ContainsKey(element))
  51.             {
  52.                 throw new ArgumentException();
  53.             }
  54.  
  55.             var currentNode = this.nodesByValue[element];
  56.  
  57.             if (currentNode.Parent == null)
  58.             {
  59.                 throw new InvalidOperationException();
  60.             }
  61.  
  62.             var parentNode = currentNode.Parent;
  63.             var currentNodeChildren = currentNode.Children;
  64.  
  65.             if (currentNodeChildren.Count > 0)
  66.             {
  67.                 foreach (var node in currentNodeChildren)
  68.                 {
  69.                     node.Parent = parentNode;
  70.                     parentNode.Children.Add(node);
  71.                 }
  72.             }
  73.  
  74.             parentNode.Children.Remove(currentNode);
  75.             this.nodesByValue.Remove(element);
  76.         }
  77.  
  78.         public IEnumerable<T> GetChildren(T item)
  79.         {
  80.             if (!this.nodesByValue.ContainsKey(item))
  81.             {
  82.                 throw new ArgumentException();
  83.             }
  84.  
  85.             var parent = this.nodesByValue[item];
  86.             var children = parent.Children.Select(n => n.Value);
  87.  
  88.             return children;
  89.         }
  90.  
  91.         public T GetParent(T item)
  92.         {
  93.             if (!this.nodesByValue.ContainsKey(item))
  94.             {
  95.                 throw new ArgumentException();
  96.             }
  97.  
  98.             var currentNode = this.nodesByValue[item];
  99.  
  100.             //if (currentNode.Parent == null)
  101.             //{
  102.             //    return default(T);
  103.             //}
  104.  
  105.             //return currentNode.Parent.Value;
  106.  
  107.             return currentNode.Parent == null ? default(T) : currentNode.Parent.Value;
  108.  
  109.         }
  110.  
  111.         public bool Contains(T value)
  112.         {
  113.  
  114.             return this.nodesByValue.ContainsKey(value);
  115.         }
  116.  
  117.         public IEnumerable<T> GetCommonElements(Hierarchy<T> other)
  118.         {
  119.             var result = this.nodesByValue.Keys.Intersect(other.nodesByValue.Keys);
  120.  
  121.             return result;
  122.         }
  123.  
  124.         public IEnumerator<T> GetEnumerator()
  125.         {
  126.             var result = new Queue<Node>();
  127.  
  128.             var current = this.root;
  129.             result.Enqueue(current);
  130.  
  131.             while (result.Count > 0)
  132.             {
  133.                 current = result.Dequeue();
  134.  
  135.                 yield return current.Value;
  136.  
  137.                 foreach (var child in current.Children)
  138.                 {
  139.                     result.Enqueue(child);
  140.                 }
  141.             }
  142.         }
  143.  
  144.         IEnumerator IEnumerable.GetEnumerator()
  145.         {
  146.             return this.GetEnumerator();
  147.         }
  148.  
  149.         private class Node
  150.         {
  151.             public Node(T value, Node parent = null)
  152.             {
  153.                 this.Value = value;
  154.                 this.Parent = parent;
  155.                 this.Children = new List<Node>();
  156.             }
  157.  
  158.             public T Value { get; set; }
  159.  
  160.             public Node Parent { get; set; }
  161.  
  162.             public List<Node> Children { get; set; }
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment