public void AddChildTest() { // Parent node HeapNode <float, string> node = new HeapNode <float, string>(0, "Potato"); // Add first child HeapNode <float, string> child1 = new HeapNode <float, string>(2, "Tomato"); node.AddChild(child1); Assert.AreEqual(child1.Parent, node); // Child is child of node Assert.AreEqual(node.Children.Count, 1); // Node has one child Assert.IsTrue(node.Children.Contains(child1)); // Node has child as a child Assert.AreEqual(node.Rank, 1); // Rank reflects number of children // Add second child HeapNode <float, string> child2 = new HeapNode <float, string>(7, "Orange"); node.AddChild(child2); Assert.AreEqual(child2.Parent, node); // Child is child of node Assert.AreEqual(node.Children.Count, 2); // Node has one child Assert.IsTrue(node.Children.Contains(child1)); // Node has child1 as a child Assert.IsTrue(node.Children.Contains(child2)); // Node has child2 as a child Assert.AreEqual(node.Rank, 2); // Rank reflects number of children }
private HeapNode CombineNodes(HeapNode first, HeapNode add) { int comp = Comparer.Compare(first.Key, add.Key); HeapNode smaller = first; HeapNode other = add; if (comp > 0) { Swap(ref smaller, ref other); } Debug.Assert(smaller.Order == other.Order); other.Cut(); smaller.AddChild(other); smaller.Order++; LastConsolidateDepth++; Debug.Assert(smaller.ChildrenCount <= smaller.Order); #if VERBOSE Console.WriteLine("Merging tree under another: {0} (under {1})", other, smaller); Console.WriteLine("All siblings ({0}): ", smaller.ChildrenCount); foreach (var siblingNode in smaller.FirstChild.GetSiblings().Take(4)) { Console.WriteLine(siblingNode); } #endif return(smaller); }
public void RemoveParentTest() { // Parent node HeapNode <float, string> node = new HeapNode <float, string>(0, "Potato"); // Add child, remove parent HeapNode <float, string> child = new HeapNode <float, string>(2, "Tomato"); node.AddChild(child); child.RemoveParent(); Assert.IsNull(child.Parent); // Child has no parent Assert.AreEqual(node.Children.Count, 0); // Node has no children Assert.IsFalse(node.Children.Contains(child)); // Child is not a child of node Assert.AreEqual(node.Rank, 0); // Rank reflects number of children }