Class SimpleTreeBase<TKey, TValue>
- Namespace
- Dynamicweb.Core
- Assembly
- Dynamicweb.Core.dll
Base implementation of a generic tree, that does not expose it's internal workings or nodes.
public class SimpleTreeBase<TKey, TValue> where TKey : struct, IEquatable<TKey> where TValue : class
Type Parameters
TKey
Key type in tree
TValue
Object type in the tree
- Inheritance
-
SimpleTreeBase<TKey, TValue>
- Derived
- Inherited Members
Constructors
SimpleTreeBase(TKey, bool)
Creates an empty tree.
public SimpleTreeBase(TKey key, bool makeDummyRoot = false)
Parameters
key
TKeyId of tree
makeDummyRoot
boolDefines if a dummy root should be created (this root has id 0)
Properties
Id
Id of tree.
public TKey Id { get; }
Property Value
- TKey
id
IsDummyRoot
Indicates if the root of the tree is a dummy root.
protected bool IsDummyRoot { get; set; }
Property Value
Methods
AddChild(SimpleTreeNode)
Adds the node as a child to this node.
protected void AddChild(SimpleTreeBase<TKey, TValue>.SimpleTreeNode node)
Parameters
node
SimpleTreeBase<TKey, TValue>.SimpleTreeNode
GetAll()
Gets all nodes in tree.
public IEnumerable<TValue> GetAll()
Returns
- IEnumerable<TValue>
allNodes
GetAncestors(TKey, bool)
Yields the ancestors of specified node.
public IEnumerable<TValue> GetAncestors(TKey nodeId, bool includingSelf = true)
Parameters
nodeId
TKeyId of node
includingSelf
boolSpecifies if the node should be included in the result as the youngest
Returns
- IEnumerable<TValue>
ancestors
Remarks
Oldest ancestors are at the front of the list, youngest at the back.
GetChildren(TKey, bool)
Gets all children of the specified node.
public IEnumerable<TValue> GetChildren(TKey nodeId, bool isRecursive = false)
Parameters
nodeId
TKeyId of the parent of the nodes to fetch
isRecursive
boolInclude all children of children recursively
Returns
- IEnumerable<TValue>
GetNode(TKey)
Gets the node matching the id.
protected SimpleTreeBase<TKey, TValue>.SimpleTreeNode? GetNode(TKey key)
Parameters
key
TKeyId of node to fetch
Returns
- SimpleTreeBase<TKey, TValue>.SimpleTreeNode
node
GetRoot()
Root of tree.
public TValue? GetRoot()
Returns
- TValue
root
MakeSubTree(TKey, bool)
Makes a subtree from the specified location in the tree.
public SimpleTreeBase<TKey, TValue> MakeSubTree(TKey key, bool isShallow = true)
Parameters
key
TKeysubtree starting location
isShallow
boolDefines if a deep clone of the tree should be performed, for use when the subtree needs to have modified the parent of nodes
Returns
- SimpleTreeBase<TKey, TValue>
MakeSubTree(TKey, TKey, bool)
Makes a subtree from the specified location in the tree.
public SimpleTreeBase<TKey, TValue> MakeSubTree(TKey treeId, TKey key, bool isShallow)
Parameters
treeId
TKeyId of newly created tree
key
TKeySubtree starting location
isShallow
boolDefines if a deep clone of the tree should be performed, for use when the subtree needs to modify the parent of nodes
Returns
- SimpleTreeBase<TKey, TValue>
subTree
SetRoot(SimpleTreeNode)
Sets the root of the tree.
protected void SetRoot(SimpleTreeBase<TKey, TValue>.SimpleTreeNode node)
Parameters
node
SimpleTreeBase<TKey, TValue>.SimpleTreeNodeNode
SetRoot(TKey, TValue, bool)
Sets the root node. If another rootnode exists, this node is made into a child of the new root.
public void SetRoot(TKey elementId, TValue element, bool isRootPartOfTree = false)
Parameters
elementId
TKeyid of root
element
TValueelement in root. May be null.
isRootPartOfTree
boolspecifies if the root should be included in lookup inside the tree
TryFill(Dictionary<TKey, TKey>, Dictionary<TKey, TValue>)
Inserts all elements provided in the tree such that they are children as specified.
public void TryFill(Dictionary<TKey, TKey> parentIdByNodeId, Dictionary<TKey, TValue> nodesById)
Parameters
parentIdByNodeId
Dictionary<TKey, TKey>Dictionary of parents for nodes to be filled into the tree
nodesById
Dictionary<TKey, TValue>Dictionary of nodes and their Ids to be filled into the tree
TryGetValue(TKey, out TValue?)
Tries to get the node with the given key.
public bool TryGetValue(TKey key, out TValue? value)
Parameters
key
TKeynode id
value
TValuenode value
Returns
- bool
node
Remarks
O(1) lookup
TryRemoveNode(TKey)
Removes the node from the tree.
public bool TryRemoveNode(TKey nodeId)
Parameters
nodeId
TKeyId of node to remove from tree
Returns
- bool
success
TryReplaceNode(TKey, TValue)
Tries to replace the node content.
public bool TryReplaceNode(TKey key, TValue item)
Parameters
key
TKeyId of node that needs to have it's content replaced.
item
TValueNew content
Returns
TrySetNode(TKey, TKey, TValue)
Tries to ensure that the tree contains a node with the specified parent and key.
public bool TrySetNode(TKey parentKey, TKey childKey, TValue child)
Parameters
parentKey
TKeyid of node that should be the parent
childKey
TKeyid of inserted node
child
TValueelement contained in node