Interface ITreeDictionary<TKey, TValue>
- Namespace
- LfrlAnvil.Collections
- Assembly
- LfrlAnvil.Collections.dll
Represents a generic tree data structure with the ability to identify nodes by keys.
public interface ITreeDictionary<TKey, TValue> : IReadOnlyTreeDictionary<TKey, TValue>, IReadOnlyDictionary<TKey, TValue>, IReadOnlyCollection<KeyValuePair<TKey, TValue>>, IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IEnumerable where TKey : notnull
Type Parameters
TKeyKey type.
TValueValue type.
- Inherited Members
- Extension Methods
Properties
Count
Gets the number of elements contained in the ICollection<T>.
int Count { get; }
Property Value
- int
The number of elements contained in the ICollection<T>.
this[TKey]
Gets or sets the element with the specified key.
TValue this[TKey key] { get; set; }
Parameters
keyTKeyThe key of the element to get or set.
Property Value
- TValue
The element with the specified key.
Exceptions
- ArgumentNullException
keyis null.- KeyNotFoundException
The property is retrieved and
keyis not found.- NotSupportedException
The property is set and the IDictionary<TKey, TValue> is read-only.
Keys
Gets an ICollection<T> containing the keys of the IDictionary<TKey, TValue>.
IEnumerable<TKey> Keys { get; }
Property Value
- IEnumerable<TKey>
An ICollection<T> containing the keys of the object that implements IDictionary<TKey, TValue>.
Values
Gets an ICollection<T> containing the values in the IDictionary<TKey, TValue>.
IEnumerable<TValue> Values { get; }
Property Value
- IEnumerable<TValue>
An ICollection<T> containing the values in the object that implements IDictionary<TKey, TValue>.
Methods
Add(ITreeDictionaryNode<TKey, TValue>)
Adds the provided node to this tree.
void Add(ITreeDictionaryNode<TKey, TValue> node)
Parameters
nodeITreeDictionaryNode<TKey, TValue>Node to add.
Remarks
Created node will become this tree's root if it was empty, otherwise it will be attached to the current root node as its child.
Exceptions
- InvalidOperationException
When node already belongs to any tree.
- ArgumentException
When node's key already exists in this tree.
Add(TKey, TValue)
Adds the provided (key, value) pair to this tree.
ITreeDictionaryNode<TKey, TValue> Add(TKey key, TValue value)
Parameters
keyTKeyNode's key.
valueTValueNode's value.
Returns
- ITreeDictionaryNode<TKey, TValue>
Created ITreeDictionaryNode<TKey, TValue> instance.
Remarks
Created node will become this tree's root if it was empty, otherwise it will be attached to the current root node as its child.
Exceptions
- ArgumentException
When node's key already exists in this tree.
AddSubtree(ITreeDictionaryNode<TKey, TValue>)
Adds a sub-tree with its root at the provided node to this tree.
ITreeDictionaryNode<TKey, TValue> AddSubtree(ITreeDictionaryNode<TKey, TValue> node)
Parameters
nodeITreeDictionaryNode<TKey, TValue>Root of the sub-tree to add.
Returns
- ITreeDictionaryNode<TKey, TValue>
Created ITreeDictionaryNode<TKey, TValue> instance of the added sub-tree's root node.
Remarks
Created sub-tree's root node will become this tree's root if it was empty, otherwise it will be attached to the current root node as its child.
Exceptions
- InvalidOperationException
When node belongs to this tree.
- ArgumentException
When any of the sub-tree's node's keys already exist in this tree.
AddSubtreeTo(ITreeDictionaryNode<TKey, TValue>, ITreeDictionaryNode<TKey, TValue>)
Adds a sub-tree with its root at the provided node to this tree
and attaches it to the specified parent node as its child.
ITreeDictionaryNode<TKey, TValue> AddSubtreeTo(ITreeDictionaryNode<TKey, TValue> parent, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
nodeITreeDictionaryNode<TKey, TValue>Root of the sub-tree to add.
Returns
- ITreeDictionaryNode<TKey, TValue>
Exceptions
- InvalidOperationException
When node belongs to this tree.
- ArgumentException
When parent node does not belong to this tree or when any of the sub-tree's node's keys already exist in this tree.
AddSubtreeTo(TKey, ITreeDictionaryNode<TKey, TValue>)
Adds a sub-tree with its root at the provided node to this tree
and attaches it to the specified parent node as its child.
ITreeDictionaryNode<TKey, TValue> AddSubtreeTo(TKey parentKey, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentKeyTKeyParent node's key.
nodeITreeDictionaryNode<TKey, TValue>Root of the sub-tree to add.
Returns
- ITreeDictionaryNode<TKey, TValue>
Exceptions
- KeyNotFoundException
When parent node's key does not exist in this tree.
- InvalidOperationException
When node belongs to this tree.
- ArgumentException
When any of the sub-tree's node's keys already exist in this tree.
AddTo(ITreeDictionaryNode<TKey, TValue>, ITreeDictionaryNode<TKey, TValue>)
Adds the provided node to this tree and attaches it to the specified parent node as its child.
void AddTo(ITreeDictionaryNode<TKey, TValue> parent, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
nodeITreeDictionaryNode<TKey, TValue>Node to add.
Exceptions
- InvalidOperationException
When parent node does not belong to this tree or when node already belongs to any tree.
- ArgumentException
When node's key already exists in this tree.
AddTo(ITreeDictionaryNode<TKey, TValue>, TKey, TValue)
Adds the provided (key, value) pair to this tree
and attaches it to the specified parent node as its child.
ITreeDictionaryNode<TKey, TValue> AddTo(ITreeDictionaryNode<TKey, TValue> parent, TKey key, TValue value)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
keyTKeyNode's key.
valueTValueNode's value.
Returns
- ITreeDictionaryNode<TKey, TValue>
Created ITreeDictionaryNode<TKey, TValue> instance.
Exceptions
- InvalidOperationException
When parent node does not belong to this tree.
- ArgumentException
When node's key already exists in this tree.
AddTo(TKey, ITreeDictionaryNode<TKey, TValue>)
Adds the provided node to this tree and attaches it to the specified parent node as its child.
void AddTo(TKey parentKey, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentKeyTKeyParent node's key.
nodeITreeDictionaryNode<TKey, TValue>Node to add.
Exceptions
- InvalidOperationException
When node already belongs to any tree.
- KeyNotFoundException
When parent node's key does not exist in this tree.
- ArgumentException
When node's key already exists in this tree.
AddTo(TKey, TKey, TValue)
Adds the provided (key, value) pair to this tree
and attaches it to the specified parent node as its child.
ITreeDictionaryNode<TKey, TValue> AddTo(TKey parentKey, TKey key, TValue value)
Parameters
parentKeyTKeyParent node's key.
keyTKeyNode's key.
valueTValueNode's value.
Returns
- ITreeDictionaryNode<TKey, TValue>
Created ITreeDictionaryNode<TKey, TValue> instance.
Exceptions
- KeyNotFoundException
When parent node's key does not exist in this tree.
- ArgumentException
When node's key already exists in this tree.
ContainsKey(TKey)
Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
[Pure]
bool ContainsKey(TKey key)
Parameters
keyTKeyThe key to locate in the IDictionary<TKey, TValue>.
Returns
- bool
true if the IDictionary<TKey, TValue> contains an element with the key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.
MoveSubtreeTo(ITreeDictionaryNode<TKey, TValue>, ITreeDictionaryNode<TKey, TValue>)
Moves the sub-tree with the provided root node to a different parent node.
void MoveSubtreeTo(ITreeDictionaryNode<TKey, TValue> parent, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
nodeITreeDictionaryNode<TKey, TValue>Sub-tree's root node to move.
Remarks
This operation is equivalent to removing the sub-tree from this tree and re-adding it to the specified parent node.
Exceptions
- InvalidOperationException
When parent node or node do not belong to this tree or node is moved to itself or to one of its descendants.
MoveSubtreeTo(ITreeDictionaryNode<TKey, TValue>, TKey)
Moves the sub-tree with root node associated with the provided key to a different parent node.
ITreeDictionaryNode<TKey, TValue> MoveSubtreeTo(ITreeDictionaryNode<TKey, TValue> parent, TKey key)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
keyTKeyKey of the sub-tree's root node to move.
Returns
- ITreeDictionaryNode<TKey, TValue>
Node associated with the provided
key.
Remarks
This operation is equivalent to removing the sub-tree from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
keydoes not exist in this tree.- InvalidOperationException
When parent node does not belong to this tree or node is moved to itself or to one of its descendants.
MoveSubtreeTo(TKey, ITreeDictionaryNode<TKey, TValue>)
Moves the sub-tree with the provided root node to a different parent node.
void MoveSubtreeTo(TKey parentKey, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentKeyTKeyParent node's key.
nodeITreeDictionaryNode<TKey, TValue>Sub-tree's root node to move.
Remarks
This operation is equivalent to removing the sub-tree from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
parentKeydoes not exist in this tree.- InvalidOperationException
When node does not belong to this tree or it's moved to itself or to one of its descendants.
MoveSubtreeTo(TKey, TKey)
Moves the sub-tree with root node associated with the provided key to a different parent node.
ITreeDictionaryNode<TKey, TValue> MoveSubtreeTo(TKey parentKey, TKey key)
Parameters
parentKeyTKeyParent node's key.
keyTKeyKey of the sub-tree's root node to move.
Returns
- ITreeDictionaryNode<TKey, TValue>
Node associated with the provided
key.
Remarks
This operation is equivalent to removing the sub-tree from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
parentKeyorkeydoes not exist in this tree.- InvalidOperationException
When node is moved to itself or to one of its descendants.
MoveTo(ITreeDictionaryNode<TKey, TValue>, ITreeDictionaryNode<TKey, TValue>)
Moves the provided node to a different parent node.
void MoveTo(ITreeDictionaryNode<TKey, TValue> parent, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
nodeITreeDictionaryNode<TKey, TValue>Node to move.
Remarks
This operation is equivalent to removing the node from this tree and re-adding it to the specified parent node.
Exceptions
- InvalidOperationException
When parent node or node do not belong to this tree or node is moved to itself.
MoveTo(ITreeDictionaryNode<TKey, TValue>, TKey)
Moves the node associated with the provided key to a different parent node.
ITreeDictionaryNode<TKey, TValue> MoveTo(ITreeDictionaryNode<TKey, TValue> parent, TKey key)
Parameters
parentITreeDictionaryNode<TKey, TValue>Parent node.
keyTKeyKey of the node to move.
Returns
- ITreeDictionaryNode<TKey, TValue>
Node associated with the provided
key.
Remarks
This operation is equivalent to removing the node from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
keydoes not exist in this tree.- InvalidOperationException
When parent node does not belong to this tree or node is moved to itself.
MoveTo(TKey, ITreeDictionaryNode<TKey, TValue>)
Moves the provided node to a different parent node.
void MoveTo(TKey parentKey, ITreeDictionaryNode<TKey, TValue> node)
Parameters
parentKeyTKeyParent node's key.
nodeITreeDictionaryNode<TKey, TValue>Node to move.
Remarks
This operation is equivalent to removing the node from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
parentKeydoes not exist in this tree.- InvalidOperationException
When node does not belong to this tree or it's moved to itself.
MoveTo(TKey, TKey)
Moves the node associated with the provided key to a different parent node.
ITreeDictionaryNode<TKey, TValue> MoveTo(TKey parentKey, TKey key)
Parameters
parentKeyTKeyParent node's key.
keyTKeyKey of the node to move.
Returns
- ITreeDictionaryNode<TKey, TValue>
Node associated with the provided
key.
Remarks
This operation is equivalent to removing the node from this tree and re-adding it to the specified parent node.
Exceptions
- KeyNotFoundException
When
parentKeyorkeydoes not exist in this tree.- InvalidOperationException
When node is moved to itself.
Remove(ITreeDictionaryNode<TKey, TValue>)
Removes the provided node from this tree.
void Remove(ITreeDictionaryNode<TKey, TValue> node)
Parameters
nodeITreeDictionaryNode<TKey, TValue>Node to remove.
Remarks
When the removed node is the root node, then its first child becomes the new root node and the following children become that new root node's children, otherwise removed node's children become its parent's children.
Exceptions
- InvalidOperationException
When node does not belong to this tree.
Remove(TKey, out TValue)
Removes a node associated with the provided key from this tree.
bool Remove(TKey key, out TValue removed)
Parameters
keyTKeyKey of the node to remove.
removedTValueout parameter that returns removed node's value.
Returns
- bool
true when the node has been removed, otherwise false.
Remarks
When the removed node is the root node, then its first child becomes the new root node and the following children become that new root node's children, otherwise removed node's children become its parent's children.
RemoveSubtree(ITreeDictionaryNode<TKey, TValue>)
Removes the provided node and all of its descendants from this tree.
int RemoveSubtree(ITreeDictionaryNode<TKey, TValue> node)
Parameters
nodeITreeDictionaryNode<TKey, TValue>Root node of the sub-tree to remove.
Returns
- int
Number of removed nodes.
Exceptions
- InvalidOperationException
When node does not belong to this tree.
RemoveSubtree(TKey)
Removes the node associated with the provided key and all of its descendants from this tree.
int RemoveSubtree(TKey key)
Parameters
keyTKeyKee of the root node of the sub-tree to remove.
Returns
- int
Number of removed nodes.
SetRoot(ITreeDictionaryNode<TKey, TValue>)
Adds the provided node to this tree as a root node.
void SetRoot(ITreeDictionaryNode<TKey, TValue> node)
Parameters
nodeITreeDictionaryNode<TKey, TValue>Node to set as root.
Remarks
Old root node, if exists, will be attached to the new root node as its child.
Exceptions
- InvalidOperationException
When node already belongs to any tree.
- ArgumentException
When node's key already exists in this tree.
SetRoot(TKey, TValue)
Adds the provided (key, value) pair to this tree as a root node.
ITreeDictionaryNode<TKey, TValue> SetRoot(TKey key, TValue value)
Parameters
keyTKeyRoot's key.
valueTValueRoot's value.
Returns
- ITreeDictionaryNode<TKey, TValue>
Created ITreeDictionaryNode<TKey, TValue> instance.
Remarks
Old root node, if exists, will be attached to the new root node as its child.
Exceptions
- ArgumentException
When node's key already exists in this tree.
Swap(ITreeDictionaryNode<TKey, TValue>, ITreeDictionaryNode<TKey, TValue>)
Swaps position of two nodes of this tree.
void Swap(ITreeDictionaryNode<TKey, TValue> firstNode, ITreeDictionaryNode<TKey, TValue> secondNode)
Parameters
firstNodeITreeDictionaryNode<TKey, TValue>First node.
secondNodeITreeDictionaryNode<TKey, TValue>Second node.
Remarks
Positions of parents and children of swapped nodes are not modified.
Exceptions
- InvalidOperationException
When
firstNodeorsecondNodedo not belong to this tree.
Swap(TKey, TKey)
Swaps position of two nodes of this tree.
void Swap(TKey firstKey, TKey secondKey)
Parameters
firstKeyTKeyFirst node's key.
secondKeyTKeySecond node's key.
Remarks
Positions of parents and children of swapped nodes are not modified.
Exceptions
- KeyNotFoundException
When
firstKeyorsecondKeydo not exist in this tree.
TryGetValue(TKey, out TValue)
Gets the value associated with the specified key.
bool TryGetValue(TKey key, out TValue result)
Parameters
keyTKeyThe key whose value to get.
resultTValue
Returns
- bool
true if the object that implements IDictionary<TKey, TValue> contains an element with the specified key; otherwise, false.
Exceptions
- ArgumentNullException
keyis null.