コード例 #1
0
        public void AddChild_3Nodes_CorrectNextPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(0);
            var c1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var c2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);
            var c3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);

            _heap.AddChild(p1, c1);
            _heap.AddChild(p1, c2);
            _heap.AddChild(p1, c3);

            // Parent
            // c1 > c2 > c3

            Assert.Multiple(() =>
            {
                Assert.AreEqual(c3, c1.Prev);
                Assert.AreEqual(c2, c1.Next);

                Assert.AreEqual(c1, c2.Prev);
                Assert.AreEqual(c3, c2.Next);

                Assert.AreEqual(c2, c3.Prev);
                Assert.AreEqual(c1, c3.Next);
            });
        }
コード例 #2
0
        public void AddChild_1Node_CorrectParent()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.AddChild(p1, p2);

            Assert.AreEqual(p1, p2.Parent);
        }
コード例 #3
0
        public void AddChild_FirstChild_CorrectNextPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.AddChild(p1, p2);

            Assert.AreEqual(p2, p1.Child);
        }
コード例 #4
0
        public void Consolidate_2Node_CorrectMinReturned()
        {
            var node1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(5);
            var node2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(6);

            _heap.Join(node1, node2);
            var newMin = _heap.Consolidate(node1, 2);

            Assert.AreEqual(5, newMin.Value);
        }
コード例 #5
0
        public void Cut_2NodesWithIndirectParent_CorrectParent()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var c1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var c2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.AddChild(p1, c1);
            _heap.AddChild(p1, c2);
            _heap.Cut(c2);

            Assert.AreEqual(p1, c1.Parent);
        }
コード例 #6
0
        public void Consolidate_1Node_CorrectSiblings()
        {
            var node = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(5);

            _heap.Consolidate(node, 1);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(node, node.Prev);
                Assert.AreEqual(node, node.Next);
            });
        }
コード例 #7
0
        public void Cut_1Node_CorrectNextPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);

            _heap.Cut(p1);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(p1, p1.Next);
                Assert.AreEqual(p1, p1.Prev);
            });
        }
コード例 #8
0
        public void AddChild_1Node_CorrectNextPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.AddChild(p1, p2);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(p2, p2.Prev);
                Assert.AreEqual(p2, p2.Next);
            });
        }
コード例 #9
0
        public void Consolidate_2Node_CorrectParents()
        {
            var node1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(5);
            var node2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(6);

            _heap.Join(node1, node2);
            _heap.Consolidate(node1, 2);

            Assert.Multiple(() =>
            {
                Assert.IsNull(node1.Parent);
                Assert.AreEqual(node1, node2.Parent);
            });
        }
コード例 #10
0
        public void Consolidate_2Node_CorrectDegree()
        {
            var node1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(5);
            var node2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(6);

            _heap.Join(node1, node2);
            _heap.Consolidate(node1, 2);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(1, node1.Degree);
                Assert.AreEqual(0, node2.Degree);
            });
        }
コード例 #11
0
        public void AddChild_2Nodes_CorrectParents()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);
            var p3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);

            _heap.AddChild(p1, p2);
            _heap.AddChild(p1, p3);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(p1, p2.Parent);
                Assert.AreEqual(p1, p3.Parent);
            });
        }
コード例 #12
0
        public void Consolidate_3Node_CorrectMin()
        {
            var node1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);
            var node2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(4);

            _heap.AddChild(node1, node2);

            var node3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(5);

            _heap.Join(node3, node1);

            var newMin = _heap.Consolidate(node3, 2);

            Assert.AreEqual(3, newMin.Value);
        }
コード例 #13
0
        public void JoinNodes_2Nodes_CorrectNextAndPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.Join(p1, p2);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(p2, p1.Prev);
                Assert.AreEqual(p2, p1.Next);

                Assert.AreEqual(p1, p2.Prev);
                Assert.AreEqual(p1, p2.Next);
            });
        }
コード例 #14
0
        private Node <int> CreateSiblings(int[] tree)
        {
            Node <int> node = null;

            foreach (var level in tree)
            {
                var newNode = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(level);
                if (node == null)
                {
                    node = newNode;
                }
                else
                {
                    _heap.Join(node, newNode);
                }
            }
            return(node);
        }
コード例 #15
0
        public void Cut_2NodesWithDirectParent_CorrectNextPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var c1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var c2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);

            _heap.AddChild(p1, c1);
            _heap.AddChild(p1, c2);
            _heap.Cut(c1);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(c1, c1.Next);
                Assert.AreEqual(c1, c1.Prev);

                Assert.AreEqual(c2, c2.Next);
                Assert.AreEqual(c2, c2.Prev);
            });
        }
コード例 #16
0
        public void Iterate_Siblings_Success()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);
            var p3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);

            _heap.Join(p2, p3);
            _heap.Join(p1, p2);

            var actual = NodeDebugTools <int> .IterateSiblings(p1).ToList();

            Assert.Multiple(() =>
            {
                Assert.AreEqual(3, actual.Count());
                Assert.AreEqual(1, actual[0].Value);
                Assert.AreEqual(2, actual[1].Value);
                Assert.AreEqual(3, actual[2].Value);
            });
        }
コード例 #17
0
        public void JoinNodes_3NodesAnd3Nodes_CorrectNextAndPrev()
        {
            var p1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var p2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);
            var p3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);

            _heap.Join(p2, p3);
            _heap.Join(p1, p2);

            var n1 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(1);
            var n2 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(2);
            var n3 = FibonacciHeapTestHelpers.CreateNodeConnectedToSelf(3);

            _heap.Join(n2, n3);
            _heap.Join(n1, n2);

            _heap.Join(p1, n1);

            Assert.Multiple(() =>
            {
                Assert.AreEqual(n3, p1.Prev);
                Assert.AreEqual(p2, p1.Next);

                Assert.AreEqual(p1, p2.Prev);
                Assert.AreEqual(p3, p2.Next);

                Assert.AreEqual(p2, p3.Prev);
                Assert.AreEqual(n1, p3.Next);

                Assert.AreEqual(p3, n1.Prev);
                Assert.AreEqual(n2, n1.Next);

                Assert.AreEqual(n1, n2.Prev);
                Assert.AreEqual(n3, n2.Next);

                Assert.AreEqual(n2, n3.Prev);
                Assert.AreEqual(p1, n3.Next);
            });
        }