示例#1
0
        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);
        }
示例#2
0
        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);
            }
        }
示例#3
0
        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);
        }
示例#4
0
        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);
        }
示例#5
0
 public bool Contains(T value)
 {
     return(NodesByValues.ContainsKey(value));
 }