public void Remove(T element) { if (!NodesByValues.ContainsKey(element)) { throw new ArgumentException("element not exist!!!"); } var node = NodesByValues[element]; if (node.Parent == null) { throw new InvalidOperationException("cannon remove the root!!!"); } var parent = node.Parent; parent.Children.AddRange(node.Children); parent.Children.Remove(node); foreach (var nodes in node.Children) { nodes.Parent = parent; } NodesByValues.Remove(element); }
public IEnumerable <T> GetChildren(T item) { if (!NodesByValues.ContainsKey(item)) { throw new ArgumentException("element not exists!!!"); } var node = NodesByValues[item]; for (int i = 0; i < node.Children.Count; i++) { yield return(node.Children[i].Value); } }
public T GetParent(T item) { if (!NodesByValues.ContainsKey(item)) { throw new ArgumentException("element not exist!!!"); } var node = NodesByValues[item]; if (node.Parent == null) { return(default(T)); } return(node.Parent.Value); }
public void Add(T element, T child) { if (!NodesByValues.ContainsKey(element)) { throw new ArgumentException("Parrent not exists!!!"); } if (NodesByValues.ContainsKey(child)) { throw new ArgumentException("Child exists!!!"); } Node <T> newChildNode = new Node <T>(child); NodesByValues.Add(child, newChildNode); var parent = NodesByValues[element]; newChildNode.Parent = parent; parent.Children.Add(newChildNode); }
public bool Contains(T value) { return(NodesByValues.ContainsKey(value)); }