Class BaseTree<TValue, TTree, TTreeNode>
- Namespace
- Garyon.DataStructures.Trees
- Assembly
- Garyon.dll
Represents a tree.
public abstract class BaseTree<TValue, TTree, TTreeNode> : ITree<TValue, TTree, TTreeNode>, ITree<TValue>, ICollection<TValue>, IEnumerable<TValue>, IEnumerable where TTree : BaseTree<TValue, TTree, TTreeNode> where TTreeNode : BaseTreeNode<TValue, TTree, TTreeNode>
Type Parameters
TValueThe type of the elements the tree nodes store.
TTreeThe type of the tree that this type is used in.
TTreeNodeThe type of the tree nodes that are used in the
TTreetype.
- Inheritance
-
BaseTree<TValue, TTree, TTreeNode>
- Implements
-
ITree<TValue, TTree, TTreeNode>ITree<TValue>ICollection<TValue>IEnumerable<TValue>
- Derived
- Inherited Members
- Extension Methods
Constructors
BaseTree()
Initializes a new instance of the Tree<T> class with no root node.
public BaseTree()
BaseTree(TValue)
Initializes a new instance of the Tree<T> class with a root value.
public BaseTree(TValue rootValue)
Parameters
rootValueTValueThe value of the root node.
BaseTree(TTree)
Initializes a new instance of the Tree<T> class from a different tree.
public BaseTree(TTree tree)
Parameters
treeTTreeThe tree to create this tree from. Both trees remain independent.
BaseTree(TTreeNode)
Initializes a new instance of the Tree<T> class with a root node.
public BaseTree(TTreeNode root)
Parameters
rootTTreeNodeThe root node.
Fields
CachedCount
The cached count of elements within this tree.
It is null if the element count has never been calculated.
If the value is not null and any modification occurs, it will be set to null.
protected int? CachedCount
Field Value
- int?
InternalRoot
The internal field for the root of this tree. If no side-effects are bound to occur, prefer setting this over the Root property.
protected TTreeNode InternalRoot
Field Value
- TTreeNode
Properties
Breadth
Gets the breadth of this tree. It is equal to the breadth of the root node.
public int Breadth { get; }
Property Value
Count
Gets the number of elements that this tree has. The count is only calculated upon the first request after any adjustments that were made to the tree.
public int Count { get; }
Property Value
Height
Gets the height of this tree. It is equal to the height of the root node.
public int Height { get; }
Property Value
Root
Gets or sets the root node.
public TTreeNode Root { get; set; }
Property Value
- TTreeNode
This
Gets this tree node instance as a TTree.
protected TTree This { get; }
Property Value
- TTree
Methods
Clear()
Clears all this tree's nodes, including its root.
public void Clear()
ClearChildren(TValue)
Clears the children of a node with the specified value, that is within this tree.
public void ClearChildren(TValue value)
Parameters
valueTValueThe value of the parent node whose children to clear. The node must be within this tree.
ClearChildren(TValue, bool)
Clears the children of a node with the specified value, that is within this tree.
public void ClearChildren(TValue value, bool propagate)
Parameters
valueTValueThe value of the parent node whose children to clear. The node must be within this tree.
propagateboolDetermines whether this function should propagate to the Children property. Only set this to false if the new children are already set in the node before calling this function.
ClearChildren(TTreeNode)
Clears the children of a node within this tree.
public void ClearChildren(TTreeNode node)
Parameters
nodeTTreeNodeThe parent node whose children to clear. The node must be within this tree.
ClearChildren(TTreeNode, bool)
Clears the children of a node within this tree.
public abstract void ClearChildren(TTreeNode node, bool propagate)
Parameters
nodeTTreeNodeThe parent node whose children to clear. The node must be within this tree.
propagateboolDetermines whether this function should propagate to the Children property. Only set this to false if the new children are already set in the node before calling this function.
Contains(TValue)
Determines whether this tree contains a node with the specified value.
public virtual bool Contains(TValue value)
Parameters
valueTValueThe value to determine whether it is contained within the tree.
Returns
- bool
A value determining whether the requested value was found.
Equals(object?)
Determines whether the specified object is equal to the current object.
public override bool Equals(object? obj)
Parameters
objobjectThe object to compare with the current object.
Returns
Equals(TTree)
public bool Equals(TTree other)
Parameters
otherTTree
Returns
GetEnumerator()
Gets an enumerator that traverses the tree using pre-order traversal.
public IEnumerator<TValue> GetEnumerator()
Returns
- IEnumerator<TValue>
The IEnumerator<T> that traverses the tree using pre-order traversal.
GetHashCode()
Serves as the default hash function.
public override int GetHashCode()
Returns
- int
A hash code for the current object.
GetNode(TValue)
Gets a child node within this tree that has the specified value.
public virtual TTreeNode GetNode(TValue value)
Parameters
valueTValueThe value of the child tree node to find.
Returns
- TTreeNode
The TreeNode<T> with the specified value, if found; otherwise false.
GetTreeView(int)
Gets the tree view of this tree node as a subtree.
public string GetTreeView(int childIndent = 4)
Parameters
childIndentintThe length of the indentation for each subsequent child.
Returns
- string
The tree view.
InitializeNewNode(TValue)
Initializes a new instance of the TreeNode<T> class with no base tree or parent node.
protected abstract TTreeNode InitializeNewNode(TValue value = default)
Parameters
valueTValueThe value of the node.
Returns
- TTreeNode
InitializeNewNode(TTree, TValue)
Initializes a new instance of the TreeNode<T> class with a base tree, but no parent node.
protected abstract TTreeNode InitializeNewNode(TTree baseTree, TValue value = default)
Parameters
baseTreeTTreeThe base tree that contains this node.
valueTValueThe value of the node.
Returns
- TTreeNode
InitializeNewNode(TTreeNode, TValue)
Initializes a new instance of the TreeNode<T> class with a parent node. The base tree is considered to be that of the parent.
protected abstract TTreeNode InitializeNewNode(TTreeNode parentNode, TValue value = default)
Parameters
parentNodeTTreeNodeThe parent node.
valueTValueThe value of the node.
Returns
- TTreeNode
PrepareChildrenAdjustmentOperation(TTreeNode)
Performs some operations to prepare for any children adjustment operation (add, remove, set, clear).
protected bool PrepareChildrenAdjustmentOperation(TTreeNode node)
Parameters
nodeTTreeNodeThe node on which the children adjustment operation is to be performed.
Returns
- bool
true if the operation can be performed, otherwise false. This requires
nodeto not be null and belong to this tree.
Remove(TValue)
Removes a node from the tree that has the value that is equal to the provided value.
public bool Remove(TValue value)
Parameters
valueTValueThe value of the node to find and remove.
Returns
RemoveChild(TTreeNode, TTreeNode, bool)
Removes a child from a node within this tree.
public bool RemoveChild(TTreeNode node, TTreeNode childToRemove, bool propagate)
Parameters
nodeTTreeNodeThe parent node whose children to set. The node must be within this tree.
childToRemoveTTreeNodeThe child to remove from the node.
propagateboolDetermines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.
Returns
RemoveNode(TValue)
Removes a child within this tree.
public bool RemoveNode(TValue value)
Parameters
valueTValueThe value of the child to remove from the node.
Returns
RemoveNode(TValue, bool)
Removes a child within this tree.
public bool RemoveNode(TValue value, bool propagate)
Parameters
valueTValueThe value of the child to remove from the node.
propagateboolDetermines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.
Returns
RemoveNode(TTreeNode)
Removes a child within this tree.
public bool RemoveNode(TTreeNode childToRemove)
Parameters
childToRemoveTTreeNodeThe child to remove from the node.
Returns
RemoveNode(TTreeNode, bool)
Removes a child within this tree.
public bool RemoveNode(TTreeNode childToRemove, bool propagate)
Parameters
childToRemoveTTreeNodeThe child to remove from the node.
propagateboolDetermines whether this function should propagate to the RemoveChild(TTreeNode) function. Only set this to false if the new child was already added before calling this function.
Returns
RemoveNodes(IEnumerable<TValue>)
Removes nodes within this tree that contain the specified values.
public void RemoveNodes(IEnumerable<TValue> values)
Parameters
valuesIEnumerable<TValue>The values of the children to remove.
RemoveNodes(TTreeNode, IEnumerable<TValue>)
Removes nodes within a subtree with root as the specified node that contain the specified values.
public void RemoveNodes(TTreeNode node, IEnumerable<TValue> values)
Parameters
nodeTTreeNodeThe node which is the root of the subtree in which to remove the nodes at.
valuesIEnumerable<TValue>The values of the children to remove.
TraverseLevelOrder()
Traverses the tree from the root using level-order. The elements are
returned with yield return.
public IEnumerable<TValue> TraverseLevelOrder()
Returns
- IEnumerable<TValue>
The yielded values.
TraverseLevelOrderNodes()
Traverses the tree from the root using level-order. The nodes are
returned with yield return.
public IEnumerable<TTreeNode> TraverseLevelOrderNodes()
Returns
- IEnumerable<TTreeNode>
The yielded nodes.
TraversePostOrder()
Traverses the tree from the root using post-order. The elements are
returned with yield return.
public IEnumerable<TValue> TraversePostOrder()
Returns
- IEnumerable<TValue>
The yielded values.
TraversePostOrderNodes()
Traverses the tree from the root using post-order. The nodes are
returned with yield return.
public IEnumerable<TTreeNode> TraversePostOrderNodes()
Returns
- IEnumerable<TTreeNode>
The yielded nodes.
TraversePreOrder()
Traverses the tree from the root using pre-order. The elements are
returned with yield return.
public IEnumerable<TValue> TraversePreOrder()
Returns
- IEnumerable<TValue>
The yielded values.
TraversePreOrderNodes()
Traverses the tree from the root using pre-order. The nodes are returned
with yield return.
public IEnumerable<TTreeNode> TraversePreOrderNodes()
Returns
- IEnumerable<TTreeNode>
The yielded nodes.