Esempio n. 1
0
        public void Add(K key, V value)
        {
            if (this._root == null)
            {
                this._root = new BTreeNode(this.order, key, value);
                this.Count++;
            }
            else
            {
                BTreeNode parentNode  = null;
                BTreeNode currentNode = this._root;

                while (!currentNode.IsLeaf || !currentNode.HasRoom)
                {
                    if (!currentNode.HasRoom)
                    {
                        this.PushSidesDown(currentNode);
                        currentNode = this.AttachSingleNodeToParent(currentNode, parentNode);
                    }

                    BTreeNodeRecord closest = currentNode.ClosestRecord(key);

                    if (key.Equals(closest.Item.Key))
                    {
                        closest.Item = new KeyValuePair <K, V>(key, value);
                        return;
                    }
                    else
                    {
                        parentNode = currentNode;

                        if (key.CompareTo(closest.Item.Key) < 0 && closest.Left != null)
                        {
                            currentNode = closest.Left;
                        }
                        else
                        {
                            currentNode = closest.Right;
                        }
                    }
                }

                if (currentNode.ContainsKey(key))
                {
                    currentNode.ClosestRecord(key).Item = new KeyValuePair <K, V>(key, value);
                }
                else
                {
                    currentNode.Records.Add(new BTreeNodeRecord(key, value));
                    currentNode.Records.Sort();
                    this.Count++;
                }
            }
        }
Esempio n. 2
0
        private void FixPathSingles(K key)
        {
            BTreeNode parentNode  = null;
            BTreeNode currentNode = this._root;

            while (currentNode != null)
            {
                if (currentNode.Records.Count == 1 && !currentNode.Equals(this._root))
                {
                    currentNode = FixSingle(parentNode, currentNode);
                }

                if (currentNode.ContainsKey(key))
                {
                    break;
                }

                parentNode  = currentNode;
                currentNode = this.FindNode(currentNode, key, 1, false);
            }
        }