< Summary

Information
Class: MoreStructures.PriorityQueues.BinomialHeap.TreeNode<T>
Assembly: MoreStructures
File(s): /home/runner/work/MoreStructures/MoreStructures/MoreStructures/PriorityQueues/BinomialHeap/TreeNode.cs
Line coverage
100%
Covered lines: 31
Uncovered lines: 0
Coverable lines: 31
Total lines: 115
Line coverage: 100%
Branch coverage
100%
Covered branches: 22
Total branches: 22
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
get_PrioritizedItem()100%1100%
get_Children()100%1100%
get_Parent()100%1100%
get_RootsListNode()100%1100%
get_ParentListNode()100%1100%
get_IsALoser()100%1100%
get_IsInAHeap()100%2100%
AddChild(...)100%6100%
DetachFromParent()100%6100%
DeepCopy()100%4100%
ToString()100%4100%

File(s)

/home/runner/work/MoreStructures/MoreStructures/MoreStructures/PriorityQueues/BinomialHeap/TreeNode.cs

#LineLine coverage
 1namespace MoreStructures.PriorityQueues.BinomialHeap;
 2
 3/// <summary>
 4/// A node of a tree, root or non-root, in the underlying forest representing the heap.
 5/// </summary>
 6public class TreeNode<T>
 7{
 8    /// <summary>
 9    /// The item of type <typeparamref name="T"/>, with its priority and push timestamp.
 10    /// </summary>
 44428411    public PrioritizedItem<T> PrioritizedItem { get; set; }
 12
 13    /// <summary>
 14    /// A <see cref="LinkedList{T}"/> of the children of this node. Empty if leaf.
 15    /// </summary>
 50405016    public LinkedList<TreeNode<T>> Children { get; private set; } = new();
 17
 18    /// <summary>
 19    /// A back-reference to the parent node. Null if a root.
 20    /// </summary>
 43933021    public TreeNode<T>? Parent { get; set; } = null;
 22
 23    /// <summary>
 24    /// A back-reference to the <see cref="LinkedListNode{T}"/> wrapper, in the <see cref="LinkedList{T}"/> of
 25    /// tree roots in the underlying forest representing the heap. Null if not a root.
 26    /// </summary>
 40957627    public LinkedListNode<TreeNode<T>>? RootsListNode { get; set; } = null;
 28
 29    /// <summary>
 30    /// A back-reference to the <see cref="LinkedListNode{T}"/> wrapper, in the <see cref="LinkedList{T}"/> of
 31    /// children of the <see cref="Parent"/> of this node. Null if a root.
 32    /// </summary>
 43946833    public LinkedListNode<TreeNode<T>>? ParentListNode { get; set; } = null;
 34
 35    /// <summary>
 36    /// Whether this node has lost a child since last reset, and will be promoted to roots next time they will lose a
 37    /// child. Only applies to, and it is taken into account from, Fibonacci heaps.
 38    /// </summary>
 10451239    public bool IsALoser { get; set; } = false;
 40
 41    /// <summary>
 42    /// Whether this node is in the heap, either as a root or as a non-root node in a tree of the forest, or it is
 43    /// a dangling or detached node.
 44    /// </summary>
 41345    public bool IsInAHeap => ParentListNode != null || RootsListNode != null;
 46
 47    /// <summary>
 48    /// Add the provides <paramref name="treeNode"/> to the <see cref="Children"/> of this instance.
 49    /// </summary>
 50    /// <param name="treeNode">The <see cref="TreeNode{T}"/> instance to become a child.</param>
 51    public void AddChild(TreeNode<T> treeNode)
 8599052    {
 8599053        if (treeNode.Parent != null || treeNode.ParentListNode != null)
 354            throw new InvalidOperationException($"{nameof(treeNode)} cannot be already a child of another node.");
 8598755        if (treeNode.RootsListNode != null)
 156            throw new InvalidOperationException($"{nameof(treeNode)} cannot be a root.");
 57
 8598658        treeNode.Parent = this;
 8598659        treeNode.ParentListNode = Children.AddLast(treeNode);
 8598660    }
 61
 62    /// <summary>
 63    /// Removes this node from the <see cref="Children"/> of its <see cref="Parent"/>.
 64    /// </summary>
 65    public void DetachFromParent()
 7465066    {
 7465067        if (Parent == null || ParentListNode == null)
 268            throw new InvalidOperationException($"This node must be child of a node.");
 7464869        if (RootsListNode != null)
 170            throw new InvalidOperationException("Incoherent state: node both a child and a root.");
 71
 7464772        Parent.Children.Remove(ParentListNode!);
 73
 7464774        Parent = null;
 7464775        ParentListNode = null;
 7464776    }
 77
 78    /// <summary>
 79    /// Deep copies this <see cref="TreeNode{T}"/> and its entire structure.
 80    /// </summary>
 81    /// <returns>
 82    /// A new instance of <see cref="TreeNode{T}"/>, pointing to a new, separate but equivalent structure.
 83    /// </returns>
 84    /// <remarks>
 85    /// This method is supposed to be used for a <b>temporary copy</b> of the heap, in order to iterate over it
 86    /// without modifying the original heap.
 87    /// <br/>
 88    /// It is not conceived to support full clones of a heap, such the one required by <see cref="ICloneable"/>.
 89    /// <br/>
 90    /// It doesn't copy <see cref="Parent"/> for the top-level <see cref="TreeNode{T}"/>, nor its
 91    /// <see cref="RootsListNode"/> or <see cref="ParentListNode"/>: those have to be set, according to the
 92    /// scenario, by the caller of <see cref="DeepCopy"/>.
 93    /// </remarks>
 94    public TreeNode<T> DeepCopy()
 1148695    {
 96        // Parent and ParentListNode are taken care by the parent TreeNode.
 97        // RootsListNode is taken care by the top-level copy of the heap.
 98        // IsInAHeap is auto-calculated based on ParentListNode and RootsListNode.
 1148699        var copy = new TreeNode<T> { PrioritizedItem = PrioritizedItem, IsALoser = IsALoser };
 100
 65067101        foreach (var childCopy in Children.Select(c => c.DeepCopy()))
 10203102            copy.AddChild(childCopy);
 103
 11486104        return copy;
 11486105    }
 106
 107    /// <inheritdoc path="//*[not(self::summary)]"/>
 108    /// <summary>
 109    ///     <inheritdoc/>
 110    ///     <br/>
 111    ///     Includes the <see cref="PrioritizedItem"/>, <see cref="IsInAHeap"/> and <see cref="IsALoser"/>.
 112    /// </summary>
 113    public override string ToString() =>
 5114        $"{PrioritizedItem} [{(IsInAHeap ? "In a heap" : "Not in a heap")}] [{(IsALoser ? "Loser" : "Not a loser")}]";
 115}