| | 1 | | namespace 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> |
| | 6 | | public class TreeNode<T> |
| | 7 | | { |
| | 8 | | /// <summary> |
| | 9 | | /// The item of type <typeparamref name="T"/>, with its priority and push timestamp. |
| | 10 | | /// </summary> |
| 444284 | 11 | | 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> |
| 504050 | 16 | | 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> |
| 439330 | 21 | | 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> |
| 409576 | 27 | | 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> |
| 439468 | 33 | | 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> |
| 104512 | 39 | | 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> |
| 413 | 45 | | 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) |
| 85990 | 52 | | { |
| 85990 | 53 | | if (treeNode.Parent != null || treeNode.ParentListNode != null) |
| 3 | 54 | | throw new InvalidOperationException($"{nameof(treeNode)} cannot be already a child of another node."); |
| 85987 | 55 | | if (treeNode.RootsListNode != null) |
| 1 | 56 | | throw new InvalidOperationException($"{nameof(treeNode)} cannot be a root."); |
| | 57 | |
|
| 85986 | 58 | | treeNode.Parent = this; |
| 85986 | 59 | | treeNode.ParentListNode = Children.AddLast(treeNode); |
| 85986 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Removes this node from the <see cref="Children"/> of its <see cref="Parent"/>. |
| | 64 | | /// </summary> |
| | 65 | | public void DetachFromParent() |
| 74650 | 66 | | { |
| 74650 | 67 | | if (Parent == null || ParentListNode == null) |
| 2 | 68 | | throw new InvalidOperationException($"This node must be child of a node."); |
| 74648 | 69 | | if (RootsListNode != null) |
| 1 | 70 | | throw new InvalidOperationException("Incoherent state: node both a child and a root."); |
| | 71 | |
|
| 74647 | 72 | | Parent.Children.Remove(ParentListNode!); |
| | 73 | |
|
| 74647 | 74 | | Parent = null; |
| 74647 | 75 | | ParentListNode = null; |
| 74647 | 76 | | } |
| | 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() |
| 11486 | 95 | | { |
| | 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. |
| 11486 | 99 | | var copy = new TreeNode<T> { PrioritizedItem = PrioritizedItem, IsALoser = IsALoser }; |
| | 100 | |
|
| 65067 | 101 | | foreach (var childCopy in Children.Select(c => c.DeepCopy())) |
| 10203 | 102 | | copy.AddChild(childCopy); |
| | 103 | |
|
| 11486 | 104 | | return copy; |
| 11486 | 105 | | } |
| | 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() => |
| 5 | 114 | | $"{PrioritizedItem} [{(IsInAHeap ? "In a heap" : "Not in a heap")}] [{(IsALoser ? "Loser" : "Not a loser")}]"; |
| | 115 | | } |