コード例 #1
0
        private static ArneTreeNode <TKey, TValue> Add(ArneTreeNode <TKey, TValue> node, TValue leaf)
        {
            if (node == null)
            {
                return(new ArneTreeNode <TKey, TValue>(leaf, 1, null, null));
            }

            var compare = leaf.CompareTo(node.Value);

            if (compare < 0)
            {
                node.Left = Add(node.Left, leaf);
            }
            else if (compare > 0)
            {
                node.Right = Add(node.Right, leaf);
            }
            else
            {
                return(null);
            }

            // Perform skew and then split.
            // The conditionals that determine whether or not a rotation will occur or not are inside of the procedures, as given above.
            node = node.Skew();

            node = node.Split();

            return(node);
        }
コード例 #2
0
        public void NodeSkew()
        {
            // prepare data
            var rootNode = new ArneTreeNode <Char, Char>('0', 0);
            var tNode    = new ArneTreeNode <Char, Char>('T', 1);
            var lNode    = new ArneTreeNode <Char, Char>('L', 1);
            var aNode    = new ArneTreeNode <Char, Char>('A', 2);
            var bNode    = new ArneTreeNode <Char, Char>('B', 2);
            var rNode    = new ArneTreeNode <Char, Char>('R', 2);

            // initialize
            rootNode.Right = tNode;
            tNode.Left     = lNode;
            tNode.Right    = rNode;
            lNode.Left     = aNode;
            lNode.Right    = bNode;

            // perform operation
            Assert.AreEqual(aNode, aNode.Skew());
            Assert.AreEqual(lNode, lNode.Skew());
            rootNode.Right = rootNode.Right.Skew();

            // check results

            // references
            Assert.AreEqual(lNode, rootNode.Right);
            Assert.AreEqual(aNode, lNode.Left);
            Assert.AreEqual(tNode, lNode.Right);
            Assert.AreEqual(bNode, tNode.Left);
            Assert.AreEqual(rNode, tNode.Right);

            // levels
            Assert.AreEqual(0u, rootNode.Level);
            Assert.AreEqual(1u, tNode.Level);
            Assert.AreEqual(1u, lNode.Level);
            Assert.AreEqual(2u, aNode.Level);
            Assert.AreEqual(2u, bNode.Level);
            Assert.AreEqual(2u, rNode.Level);
        }