示例#1
0
        private static TryResult <TValue> TryGetValue(ArneTreeNode <TKey, TValue> node, TKey key)
        {
            while (true)
            {
                if (node == null)
                {
                    return(tryGetFailResult);
                }

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

                if (compare > 0)
                {
                    node = node.Left;
                }
                else if (compare < 0)
                {
                    node = node.Right;
                }
                else
                {
                    return(TryResult <TValue> .CreateSuccess(node.Value));
                }
            }
        }
示例#2
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);
        }
示例#3
0
        private static Boolean Contains(ArneTreeNode <TKey, TValue> node, TKey key)
        {
            while (true)
            {
                if (node == null)
                {
                    return(false);
                }

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

                if (compare > 0)
                {
                    node = node.Left;
                }
                else if (compare < 0)
                {
                    node = node.Right;
                }
                else
                {
                    return(true);
                }
            }
        }
示例#4
0
        public void NodeDecreaseLevel()
        {
            var rootNode = new ArneTreeNode <Int32, Int32>(100, 0);
            var aNode    = new ArneTreeNode <Int32, Int32>(90, 4);
            var bNode    = new ArneTreeNode <Int32, Int32>(20, 1);
            var cNode    = new ArneTreeNode <Int32, Int32>(120, 3);
            var dNode    = new ArneTreeNode <Int32, Int32>(10, 5);
            var eNode    = new ArneTreeNode <Int32, Int32>(30, 3);

            // initialize
            rootNode.Left = aNode;
            aNode.Left    = bNode;
            aNode.Right   = cNode;
            bNode.Left    = dNode;
            bNode.Right   = eNode;

            // perform opeation

            Assert.AreEqual(dNode, dNode.DecraseLevel());
            Assert.AreEqual(eNode, eNode.DecraseLevel());

            aNode.DecraseLevel();

            Assert.AreEqual(2u, aNode.Level);
            Assert.AreEqual(2u, aNode.Right.Level);
        }
示例#5
0
        internal ArneTreeNode(TValue value, UInt32 level)
            : base(value)
        {
            Level = level;

            left = null;

            right = null;
        }
示例#6
0
        internal ArneTreeNode(TValue value, UInt32 level, ArneTreeNode <TKey, TValue> left, ArneTreeNode <TKey, TValue> right)
            : base(value)
        {
            Level = level;

            Left = left;

            Right = right;
        }
示例#7
0
        public void Remove(TValue item)
        {
            if (root == null)
            {
                return;
            }

            root = Remove(root, item);

            Count--;
        }
示例#8
0
        public Boolean Add(TValue item)
        {
            var node = Add(root, item);

            if (node == null)
            {
                return(false);
            }

            root = node;

            Count++;

            return(true);
        }
示例#9
0
        public void NodeSplit()
        {
            // prepare data
            var rootNode = new ArneTreeNode <Char, Char>('0', 0);
            var tNode    = new ArneTreeNode <Char, Char>('T', 1);
            var rNode    = new ArneTreeNode <Char, Char>('R', 1);
            var xNode    = new ArneTreeNode <Char, Char>('X', 1);
            var aNode    = new ArneTreeNode <Char, Char>('A', 2);
            var bNode    = new ArneTreeNode <Char, Char>('B', 2);

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

            // perform operation
            Assert.AreEqual(xNode, xNode.Split());
            Assert.AreEqual(rNode, rNode.Split());
            Assert.AreEqual(rootNode, rootNode.Split());
            rootNode.Right = rootNode.Right.Split();

            // check results

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

            // levels
            Assert.AreEqual(0u, rootNode.Level);
            Assert.AreEqual(2u, rNode.Level);
            Assert.AreEqual(1u, tNode.Level);
            Assert.AreEqual(1u, xNode.Level);
            Assert.AreEqual(2u, aNode.Level);
            Assert.AreEqual(2u, bNode.Level);
        }
示例#10
0
        public void NodePredecessorAndSuccessor()
        {
            var rootNode = new ArneTreeNode <Int32, Int32>(100, 0);
            var aNode    = new ArneTreeNode <Int32, Int32>(30, 1);
            var bNode    = new ArneTreeNode <Int32, Int32>(60, 1);
            var cNode    = new ArneTreeNode <Int32, Int32>(10, 2);
            var dNode    = new ArneTreeNode <Int32, Int32>(20, 2);
            var eNode    = new ArneTreeNode <Int32, Int32>(40, 2);
            var fNode    = new ArneTreeNode <Int32, Int32>(50, 2);
            var gNode    = new ArneTreeNode <Int32, Int32>(18, 3);
            var hNode    = new ArneTreeNode <Int32, Int32>(42, 3);
            var iNode    = new ArneTreeNode <Int32, Int32>(15, 3);
            var jNode    = new ArneTreeNode <Int32, Int32>(45, 3);

            // initialize
            rootNode.Left  = aNode;
            rootNode.Right = bNode;

            Assert.AreEqual(null, aNode.Predecessor);
            Assert.AreEqual(null, bNode.Successor);

            aNode.Left  = cNode;
            aNode.Right = dNode;
            dNode.Left  = gNode;
            gNode.Left  = iNode;

            bNode.Left  = eNode;
            bNode.Right = fNode;
            eNode.Right = hNode;
            hNode.Right = jNode;

            // perform operation

            Assert.AreEqual(dNode, rootNode.Predecessor);
            Assert.AreEqual(aNode, iNode.Predecessor);

            Assert.AreEqual(eNode, rootNode.Successor);
            Assert.AreEqual(bNode, jNode.Successor);
        }
示例#11
0
        public void NodeCtor()
        {
            // test data
            var expectedLeft  = new ArneTreeNode <Int32, Int32>(0, 0);
            var expectedRight = new ArneTreeNode <Int32, Int32>(0, 0);

            const UInt32 ExpectedLevel = 777u;
            const Int32  ExpectdData   = 100;

            // test object
            var root = new ArneTreeNode <Int32, Int32>(ExpectdData, ExpectedLevel, expectedLeft, expectedRight);

            // tests
            Assert.AreEqual(ExpectedLevel, root.Level);
            Assert.AreEqual(ExpectdData, root.Value);

            // Parent
            Assert.AreEqual(root, expectedLeft.Parent);
            Assert.AreEqual(root, expectedRight.Parent);

            // IsLeaf
            Assert.IsTrue(expectedLeft.IsLeaf);
            Assert.IsFalse(root.IsLeaf);

            // IsRoot
            Assert.IsFalse(expectedLeft.IsRoot);
            Assert.IsTrue(root.IsRoot);

            // left
            Assert.AreEqual(expectedLeft, root.Left);
            root.Left = null;
            Assert.IsNull(expectedLeft.Parent);

            // right
            Assert.AreEqual(expectedRight, root.Right);
            root.Right = null;
            Assert.IsNull(expectedRight.Parent);
        }
示例#12
0
        public void Clear()
        {
            Count = 0;

            root = null;
        }
示例#13
0
        public ArneTree()
        {
            root = null;

            Count = 0;
        }
示例#14
0
        private static ArneTreeNode <TKey, TValue> Remove(ArneTreeNode <TKey, TValue> node, TValue item)
        {
            if (node == null)
            {
                return(null);
            }

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

            if (compare < 0)
            {
                node.Left = Remove(node.Left, item);
            }
            else if (compare > 0)
            {
                node.Right = Remove(node.Right, item);
            }
            else
            {
                // If NodeBase is leaf, easy, otherwise reduce to leaf case.
                if (node.IsLeaf)
                {
                    return(null);
                }

                if (node.Left == null)
                {
                    var right = node.Successor;

                    node.Right = Remove(node.Right, right.Value);

                    node = new ArneTreeNode <TKey, TValue>(right.Value, node.Level, node.Left, node.Right);
                }
                else
                {
                    var left = node.Predecessor;

                    node.Left = Remove(node.Left, left.Value);

                    node = new ArneTreeNode <TKey, TValue>(left.Value, node.Level, node.Left, node.Right);
                }
            }

            // Re-balance the tree.
            // Decrease the level of all nodes in this level if necessary
            node = node.DecraseLevel();

            // Skew and split all nodes in the new level.
            node = node.Skew();

            if (node.Right != null)
            {
                node.Right = node.Right.Skew();
            }

            if (node.Right?.Right != null)
            {
                node.Right.Right = node.Right.Right.Skew();
            }

            node = node.Split();

            if (node.Right != null)
            {
                node.Right = node.Right.Split();
            }

            return(node);
        }