Ejemplo n.º 1
0
 // This is called only from inside persistenceStore.Write
 public void Add(JToken key)
 {
     IComparable actualKey = transform(key);
     Index = Index.AddOrUpdate(actualKey,
         new EmptyAVLTree<JToken, JToken>(JTokenComparer.Instance, JTokenCloner.Clone, JTokenCloner.Clone).Add(key, key),
         (comparable, tree) => tree.Add(key, key));
 }
Ejemplo n.º 2
0
        public void BinarySearchTree_02_FindMin_02_OnTree1Element()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeInt1Element();
            int expected = 4;

            // Act
            int actual = tree.FindMin();

            // Act & Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 3
0
        public void BinarySearchTree_05_ToString_01_OnEmptyTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntEmpty();
            string expected = "";

            // Act
            string actual = tree.ToString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 4
0
        public void BinarySearchTree_05_ToString_03_OnModerateTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntModerate();
            string expected = "3 7 8 12 17 24 26 32 34 37 42 45 50";

            // Act
            string actual = tree.ToString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 5
0
        public void BinarySearchTree_05_ToString_02_OnSmallTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntSmall();
            string expected = "2 4 5 6 7";

            // Act
            string actual = tree.ToString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 6
0
        public bool TryGetValue(TKey key, out TValue value)
        {
            value = default(TValue);
            IBinarySearchTree <TKey, TValue> tree = Search(key);

            if (tree.IsEmpty)
            {
                return(false);
            }
            value = tree.Value;
            return(true);
        }
Ejemplo n.º 7
0
        public void BinarySearchTree_02_FindMin_04_OnModerateIntTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntModerate();
            int expected = 3;

            // Act
            int actual = tree.FindMin();

            // Act & Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 8
0
        public IBinarySearchTree Search(int key)
        {
            IBinarySearchTree currentNode = this;

            while (currentNode != null && currentNode.Key != key)
            {
                currentNode = key < currentNode.Key
                    ? currentNode.Left
                    : currentNode.Right;
            }

            return(currentNode);
        }
Ejemplo n.º 9
0
        public void BinarySearchTree_01_Insert_02_After1Insert_SizeEquals1()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntEmpty();
            int expected = 1;

            // Act
            tree.Insert(3);
            int actual = tree.Size();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 10
0
        public void BinarySearchTree_04_Remove_06_OnNonEmptyTree_ElementWithBothChilds()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntModerate();
            string expected = "[ [ NIL 3 [ [ NIL 7 NIL ] 8 [ NIL 12 NIL ] ] ] 17 [ [ NIL 24 NIL ] 26 [ [ NIL 34 [ [ NIL 37 NIL ] 42 NIL ] ] 45 [ NIL 50 NIL ] ] ] ]";

            // Act
            tree.Remove(32);
            string actual = tree.ToInfixString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 11
0
        public void BinarySearchTree_01_Insert_03_After1Insert_HeightEquals0()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntEmpty();
            int expected = 0;

            // Act
            tree.Insert(3);
            int actual = tree.Height();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 12
0
        public void BinarySearchTree_01_Insert_01_After1Insert_IsEmptyReturnsFalse()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntEmpty();
            bool expected = false;

            // Act
            tree.Insert(3);
            bool actual = tree.IsEmpty();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 13
0
        public void BinarySearchTree_03_RemoveMin_02_OnTree1Element()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeInt1Element();
            string expected = "NIL";

            // Act
            tree.RemoveMin();
            string actual = tree.ToInfixString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 14
0
        public void BinarySearchTree_03_RemoveMin_04_OnModerateIntTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntModerate();
            string expected = "[ [ [ NIL 7 NIL ] 8 [ NIL 12 NIL ] ] 17 [ [ NIL 24 NIL ] 26 [ NIL 32 [ [ NIL 34 [ [ NIL 37 NIL ] 42 NIL ] ] 45 [ NIL 50 NIL ] ] ] ] ]";

            // Act
            tree.RemoveMin();
            string actual = tree.ToInfixString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 15
0
        public void BinarySearchTree_03_RemoveMin_03_OnSmallIntTree()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntSmall();
            string expected = "[ [ NIL 4 [ NIL 5 NIL ] ] 6 [ NIL 7 NIL ] ]";

            // Act
            tree.RemoveMin();
            string actual = tree.ToInfixString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
        public void RemoveNode(IBinarySearchTree <T> nd)
        {
            var node = nd as BinarySearchTree <T>;

            if (node == null)
            {
                return;
            }
            node.Value    = default;
            node.HasValue = false;
            node._left    = null;
            node._right   = null;
            node.Comparer = null;
        }
Ejemplo n.º 17
0
        public static void InOrder <T>(this IBinarySearchTree <T> tree, Action <IBinarySearchTree <T> > action)
        {
            void InOrderInternal(IBinarySearchTree <T> node)
            {
                if (node != null)
                {
                    InOrderInternal(node.Left);
                    action(node);
                    InOrderInternal(node.Right);
                }
            }

            InOrderInternal(tree);
        }
Ejemplo n.º 18
0
        private static IBinarySearchTree <K, V> RotateLeftLeft(IBinarySearchTree <K, V> tree)
        {
            var right = tree.Right;

            if (right.IsEmpty)
            {
                return(tree);
            }
            return(RotateLeft(new AvlTree <K, V>(
                                  tree.Left,
                                  tree.Key, tree.Value,
                                  RotateRight(right)
                                  )));
        }
Ejemplo n.º 19
0
        private static IBinarySearchTree <K, V> RotateRightRight(IBinarySearchTree <K, V> tree)
        {
            var left = tree.Left;

            if (left.IsEmpty)
            {
                return(tree);
            }
            return(RotateRight(new AvlTree <K, V>(
                                   RotateLeft(left),
                                   tree.Key, tree.Value,
                                   tree.Right
                                   )));
        }
Ejemplo n.º 20
0
 public void CleanupFromTests()
 {
     Empty               = null;
     RootOnly            = null;
     RootLeft            = null;
     RootRight           = null;
     ThreeNodesFull      = null;
     FourNodesLeftLeft   = null;
     FourNodesLeftRight  = null;
     FourNodesRightLeft  = null;
     FourNodesRightRight = null;
     Bigger              = null;
     CleanupCustom();
 }
Ejemplo n.º 21
0
        public void BinarySearchTree_01_Insert_06_After3UnorderedInserts_ToInfixStringCorrect()
        {
            // Arrange
            IBinarySearchTree <int> tree = DSBuilder.CreateBinarySearchTreeIntEmpty();
            string expected = "[ [ NIL 1 NIL ] 2 [ NIL 3 NIL ] ]";

            // Act
            tree.Insert(2);
            tree.Insert(1);
            tree.Insert(3);
            string actual = tree.ToInfixString();

            // Assert
            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 22
0
        public Manager(ILogger logger, IUserInteractions userInteractions,
                       IConfiguration configuration, IBinarySearchTree <BottomSizeTreeDataModel> mainTree,
                       ILinked_List <TimeListDataModel> linkedList, IRepository <Box> boxesRepository)
        {
            //Recive Implementations
            _logger           = logger;
            _userInteractions = userInteractions;
            _config           = configuration;
            _boxesRepository  = boxesRepository;
            _mainTree         = mainTree;
            _dataByTime       = linkedList;

            //Init The Manager
            Initialize();
        }
Ejemplo n.º 23
0
        public IBinarySearchTree <K, V> Remove(K key)
        {
            IBinarySearchTree <K, V> result;
            int compare = key.CompareTo(Key);

            if (compare == 0)
            {
                // We have a match. If this is a leaf, just remove it
                // by returning Empty.  If we have only one child,
                // replace the node with the child.

                if (Right.IsEmpty && Left.IsEmpty)
                {
                    result = Empty;
                }
                else if (Right.IsEmpty && !Left.IsEmpty)
                {
                    result = Left;
                }
                else if (!Right.IsEmpty && Left.IsEmpty)
                {
                    result = Right;
                }
                else
                {
                    // We have two children. Remove the next-highest node and replace
                    // this node with it.
                    IBinarySearchTree <K, V> successor = Right;

                    while (!successor.Left.IsEmpty)
                    {
                        successor = successor.Left;
                    }

                    result = new AVLTree <K, V>(successor.Key, successor.Value, Left, Right.Remove(successor.Key));
                }
            }
            else if (compare < 0)
            {
                result = new AVLTree <K, V>(Key, Value, Left.Remove(key), Right);
            }
            else
            {
                result = new AVLTree <K, V>(Key, Value, Left, Right.Remove(key));
            }

            return(MakeBalanced(result));
        }
Ejemplo n.º 24
0
        public IBinarySearchTree <TKey, TValue> TryRemove(TKey key, out bool removed, out TValue value)
        {
            IBinarySearchTree <TKey, TValue> result;
            int compare = comparer.Compare(key, theKey);

            if (compare == 0)
            {
                removed = true;
                value   = theValue;
                // We have a match. If this is a leaf, just remove it
                // by returning Empty.  If we have only one child,
                // replace the node with the child.
                if (Right.IsEmpty && Left.IsEmpty)
                {
                    result = new EmptyAVLTree <TKey, TValue>(comparer, deepCopyKey, deepCopyValue);
                }
                else if (Right.IsEmpty && !Left.IsEmpty)
                {
                    result = Left;
                }
                else if (!Right.IsEmpty && Left.IsEmpty)
                {
                    result = Right;
                }
                else
                {
                    // We have two children. Remove the next-highest node and replace
                    // this node with it.
                    IBinarySearchTree <TKey, TValue> successor = Right;
                    while (!successor.Left.IsEmpty)
                    {
                        successor = successor.Left;
                    }
                    bool   ignoredBool;
                    TValue ignoredValue;
                    result = new AVLTree <TKey, TValue>(comparer, deepCopyKey, deepCopyValue, successor.Key, successor.Value, Left, Right.TryRemove(successor.Key, out ignoredBool, out ignoredValue));
                }
            }
            else if (compare < 0)
            {
                result = new AVLTree <TKey, TValue>(comparer, deepCopyKey, deepCopyValue, theKey, theValue, Left.TryRemove(key, out removed, out value), Right);
            }
            else
            {
                result = new AVLTree <TKey, TValue>(comparer, deepCopyKey, deepCopyValue, theKey, theValue, Left, Right.TryRemove(key, out removed, out value));
            }
            return(MakeBalanced(result));
        }
Ejemplo n.º 25
0
        private IEnumerable <IBinarySearchTree <TKey, TValue> > EnumerateInReverseOrder()
        {
            var stack = Stack <IBinarySearchTree <TKey, TValue> > .Empty;

            for (IBinarySearchTree <TKey, TValue> current = this; !current.IsEmpty || !stack.IsEmpty; current = current.Left)
            {
                while (!current.IsEmpty)
                {
                    stack   = stack.Push(current);
                    current = current.Right;
                }
                current = stack.Peek();
                stack   = stack.Pop();
                yield return(current);
            }
        }
Ejemplo n.º 26
0
        public IBinarySearchTree Successor(IBinarySearchTree binarySearchTree)
        {
            if (binarySearchTree.Right != null)
            {
                return(binarySearchTree.Right.Min());
            }

            var currentParent = binarySearchTree.Parent;

            while (currentParent != null && binarySearchTree == currentParent.Right)
            {
                binarySearchTree = currentParent;
                currentParent    = currentParent.Parent;
            }

            return(currentParent);
        }
Ejemplo n.º 27
0
        public static bool Find <TLabel>(this IBinarySearchTree <TLabel> tree, TLabel item)
            where TLabel : IComparable <TLabel>
        {
            var compare = tree.Label.CompareTo(item);

            if (compare == 0)
            {
                return(true);
            }

            if (compare < 0)
            {
                return(tree.LeftChild == null ? false : tree.LeftChild.Find(item));
            }

            return(tree.RightChild == null ? false : tree.RightChild.Find(item));
        }
Ejemplo n.º 28
0
        private IBinarySearchTree <TLabel> CreateTree(IEnumerable <TLabel> values)
        {
            IBinarySearchTree <TLabel> tree = null;

            foreach (var value in values)
            {
                if (tree == null)
                {
                    tree = this.CreateTree(value);
                }
                else
                {
                    tree.Insert(value);
                }
            }

            return(tree);
        }
Ejemplo n.º 29
0
        public IBinarySearchTree <K, V> Search(K key)
        {
            IBinarySearchTree <K, V> current = this;
            var c = key.CompareKeys(current.Key);

            while ((0 != c) && !current.IsEmpty)
            {
                current = (0 < c)
                                        ? current.Right
                                        : current.Left
                ;
                if (!current.IsEmpty)
                {
                    c = key.CompareKeys(current.Key);
                }
            }
            return(current);
        }
Ejemplo n.º 30
0
        public IEnumerator <ReadResult> GetEnumerator()
        {
            IBinarySearchTree <RavenJToken, PositionInFile> keyToFilePos = null;

            persistentSource.Read(() => keyToFilePos = KeyToFilePos);
            foreach (var positionInFile in keyToFilePos.ValuesInOrder)
            {
                byte[] readData = null;
                var    pos      = positionInFile;
                yield return(new ReadResult
                {
                    Key = positionInFile.Key,
                    Position = positionInFile.Position,
                    Size = positionInFile.Size,
                    Data = () => readData ?? (readData = ReadData(pos.Position, pos.Size, pos.Key)),
                });
            }
        }
Ejemplo n.º 31
0
        private static IBinarySearchTree Transplant(IBinarySearchTree originalBst, IBinarySearchTree replacementBst)
        {
            if (originalBst == originalBst.Parent.Left)
            {
                originalBst.Parent.Left = replacementBst;
            }
            else
            {
                originalBst.Parent.Right = replacementBst;
            }

            if (replacementBst != null)
            {
                replacementBst.Parent = originalBst.Parent;
            }

            return(replacementBst);
        }
Ejemplo n.º 32
0
 // This is called only from inside persistenceStore.Write
 public void Remove(JToken key)
 {
     IComparable actualKey = transform(key);
     var result = Index.Search(actualKey);
     if (result.IsEmpty)
     {
         return;
     }
     bool removed;
     JToken _;
     var removedResult = result.Value.TryRemove(key, out removed, out _);
     if (removedResult.IsEmpty)
     {
         IBinarySearchTree<JToken, JToken> ignored;
         Index = Index.TryRemove(actualKey, out removed, out ignored);
     }
     else
     {
         Index = Index.AddOrUpdate(actualKey, removedResult, (comparable, tree) => removedResult);
     }
 }