Exemplo n.º 1
0
        private void DeleteKeyFromSubtree(BNode <TKey, T> parentNode, TKey keyToDelete, int subtreeIndexInNode)
        {
            BNode <TKey, T> node = this._build.BringNode(int.Parse(parentNode.Children[subtreeIndexInNode]));

            if (node.Data.Count <string>() > 0)
            {
                int             index1 = subtreeIndexInNode - 1;
                int             index2 = subtreeIndexInNode + 1;
                BNode <TKey, T> bnode1 = subtreeIndexInNode > 0 ? this._build.BringNode(int.Parse(parentNode.Children[index1])) : (BNode <TKey, T>)null;
                BNode <TKey, T> bnode2 = subtreeIndexInNode < parentNode.Children.Count <string>() - 1 ? this._build.BringNode(int.Parse(parentNode.Children[index2])) : (BNode <TKey, T>)null;
                if (bnode1 != null && bnode1.Data.Count <string>() > this._build.ObtainDegree() - 1)
                {
                    List <string> list1 = node.Data.OfType <string>().ToList <string>();
                    list1.Insert(0, parentNode.Data[subtreeIndexInNode]);
                    node.Data = list1;
                    List <string> list2 = node.Keys.OfType <string>().ToList <string>();
                    list2.Insert(0, parentNode.Keys[subtreeIndexInNode]);
                    node.Keys = list2;
                    parentNode.Data[subtreeIndexInNode] = bnode1.Data.Last <string>();
                    string keyToRemove  = bnode1.Keys[bnode1.Data.Count <string>() - 1];
                    string itemToRemove = bnode1.Data[bnode1.Data.Count <string>() - 1];
                    bnode1.Data = ((IEnumerable <string>)bnode1.Data.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    bnode1.Keys = ((IEnumerable <string>)bnode1.Keys.Where <string>((Func <string, bool>)(val => val != keyToRemove)).ToArray <string>()).ToList <string>();
                    if (!this.IsLeaf(bnode1.Children))
                    {
                        List <string> list3 = node.Children.OfType <string>().ToList <string>();
                        list3.Insert(0, bnode1.Children.Last <string>());
                        node.Children   = list3;
                        keyToRemove     = bnode1.Children[bnode1.Children.Count <string>() - 1];
                        bnode1.Children = ((IEnumerable <string>)bnode1.Children.Where <string>((Func <string, bool>)(val => val != keyToRemove)).ToArray <string>()).ToList <string>();
                    }
                    this._build.SaveNode(bnode1.Information());
                    this._build.SaveNode(parentNode.Information());
                    this._build.SaveNode(node.Information());
                }
                else if (bnode2 != null && bnode2.Data.Count <string>() > this._build.ObtainDegree() - 1)
                {
                    node.Children[node.Children.Count <string>() - 1] = parentNode.Children[subtreeIndexInNode];
                    parentNode.Data[subtreeIndexInNode] = bnode2.Data.First <string>();
                    parentNode.Keys[subtreeIndexInNode] = bnode2.Keys.First <string>();
                    string itemToRemove = bnode2.Data[0];
                    bnode2.Data  = ((IEnumerable <string>)bnode2.Data.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    itemToRemove = bnode2.Keys[0];
                    bnode2.Keys  = ((IEnumerable <string>)bnode2.Keys.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    if (!this.IsLeaf(bnode2.Children))
                    {
                        List <string> list = node.Children.OfType <string>().ToList <string>();
                        list.Insert(0, bnode2.Children.Last <string>());
                        node.Children   = list;
                        itemToRemove    = bnode2.Children[0];
                        bnode2.Children = ((IEnumerable <string>)bnode2.Children.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    }
                    this._build.SaveNode(bnode2.Information());
                    this._build.SaveNode(parentNode.Information());
                    this._build.SaveNode(node.Information());
                }
                else if (bnode1 != null)
                {
                    List <string> list1 = node.Data.OfType <string>().ToList <string>();
                    list1.Insert(0, parentNode.Data[subtreeIndexInNode]);
                    node.Data = list1;
                    List <string> list2 = node.Keys.OfType <string>().ToList <string>();
                    list2.Insert(0, parentNode.Keys[subtreeIndexInNode]);
                    node.Keys = list2;
                    List <string> newData = node.Data;
                    List <string> newKeys = node.Keys;
                    node.Data = bnode1.Data;
                    node.Keys = bnode1.Keys;
                    string[] array1 = new string[newData.Count + node.Data.Count];
                    node.Data.CopyTo(array1, 0);
                    newData.CopyTo(array1, node.Data.Count);
                    string[] array2 = new string[newKeys.Count + node.Keys.Count];
                    node.Keys.CopyTo(array2, 0);
                    newKeys.CopyTo(array2, node.Keys.Count);
                    if (!this.IsLeaf(bnode1.Children))
                    {
                        List <string> newChildren = node.Children;
                        node.Children = bnode1.Children;
                        string[] array3 = new string[newData.Count + node.Data.Count];
                        node.Data.CopyTo(array3, 0);
                        newData.CopyTo(array3, node.Data.Count);
                        string[] array4 = new string[newKeys.Count + node.Keys.Count];
                        node.Keys.CopyTo(array4, 0);
                        newKeys.CopyTo(array4, node.Keys.Count);
                    }
                    string itemToRemove = parentNode.Children[index1];
                    parentNode.Children = ((IEnumerable <string>)parentNode.Children.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    itemToRemove        = parentNode.Data[subtreeIndexInNode];
                    parentNode.Data     = ((IEnumerable <string>)parentNode.Data.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    itemToRemove        = parentNode.Keys[subtreeIndexInNode];
                    parentNode.Keys     = ((IEnumerable <string>)parentNode.Keys.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                    this._build.SaveNode(parentNode.Information());
                    this._build.SaveNode(node.Information());
                }
                else if (bnode2 != null)
                {
                    node.Data[node.Data.Count <string>() - 1] = parentNode.Data[subtreeIndexInNode];
                    node.Keys[node.Keys.Count <string>() - 1] = parentNode.Keys[subtreeIndexInNode];
                    if (bnode2 != null)
                    {
                        string[] array1 = new string[bnode2.Data.Count + node.Data.Count];
                        node.Data.CopyTo(array1, 0);
                        bnode2.Data.CopyTo(array1, node.Data.Count);
                        string[] array2 = new string[bnode2.Keys.Count + node.Keys.Count];
                        node.Keys.CopyTo(array2, 0);
                        bnode2.Keys.CopyTo(array2, node.Keys.Count);
                        if (!this.IsLeaf(bnode2.Children))
                        {
                            string[] array3 = new string[bnode2.Children.Count + node.Children.Count];
                            node.Children.CopyTo(array3, 0);
                            bnode2.Children.CopyTo(array3, node.Children.Count);
                        }
                        string itemToRemove = parentNode.Children[index2];
                        parentNode.Children = ((IEnumerable <string>)parentNode.Children.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                        itemToRemove        = parentNode.Data[subtreeIndexInNode];
                        parentNode.Data     = ((IEnumerable <string>)parentNode.Data.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                        itemToRemove        = parentNode.Keys[subtreeIndexInNode];
                        parentNode.Keys     = ((IEnumerable <string>)parentNode.Keys.Where <string>((Func <string, bool>)(val => val != itemToRemove)).ToArray <string>()).ToList <string>();
                        this._build.SaveNode(parentNode.Information());
                        this._build.SaveNode(node.Information());
                    }
                }
            }
            this.DeleteIntern(node, keyToDelete);
        }
Exemplo n.º 2
0
 private void DelteKeyFromSubtre(BNode <TKey, T> parentNode, TKey keyToDelete, int subtreeIndexInNode)
 {
     this.DeleteInternal(this._build.BringNode(int.Parse(parentNode.Children[subtreeIndexInNode])), keyToDelete);
 }
Exemplo n.º 3
0
        private void InsertInFullNode(BNode <TKey, T> node, TKey newKey, T newData)
        {
            string          str1   = newKey.ToString();
            string          str2   = newData.ToString();
            BNode <TKey, T> bnode1 = (BNode <TKey, T>)null;
            BNode <TKey, T> bnode2 = (BNode <TKey, T>)null;
            bool            flag   = false;

            do
            {
                if (node == null)
                {
                    int num = this._build.ObtainFreePosition();
                    this._build.NodeFromFactory();
                    this._build.ChangeRoot(num);
                    node       = this._build.BringNode(num);
                    this._root = node;
                    this._build.ChangeHeight(this._build.ObtainHeight() + 1);
                }
                BNode <TKey, T> bnode3 = this._build.BringNode(node.Father);
                int             num1;
                if (!this.SpaceAvailable(node.Keys))
                {
                    List <string> stringList1 = new List <string>();
                    List <string> stringList2 = new List <string>();
                    List <string> stringList3 = new List <string>();
                    int           actualNode  = this._build.ObtainFreePosition();
                    this._build.NodeFromFactory();
                    BNode <TKey, T> bnode4 = this._build.BringNode(actualNode);
                    int             index1 = 0;
                    while (str1.CompareTo(node.Keys[index1]) > 0 && index1 < this._degree - 1)
                    {
                        stringList1.Insert(index1, node.Keys[index1]);
                        stringList2.Insert(index1, node.Data[index1]);
                        stringList3.Insert(index1, node.Children[index1]);
                        ++index1;
                        if (index1 == node.Keys.Count <string>())
                        {
                            break;
                        }
                    }
                    stringList1.Insert(index1, str1);
                    stringList2.Insert(index1, str2);
                    if (bnode1 != null)
                    {
                        List <string> stringList4 = stringList3;
                        int           index2      = index1;
                        num1 = bnode1.Position;
                        string str3 = num1.ToString("D11");
                        stringList4.Insert(index2, str3);
                    }
                    else
                    {
                        List <string> stringList4 = stringList3;
                        int           index2      = index1;
                        num1 = int.MinValue;
                        string str3 = num1.ToString();
                        stringList4.Insert(index2, str3);
                    }
                    if (bnode2 != null)
                    {
                        List <string> stringList4 = stringList3;
                        int           index2      = index1 + 1;
                        num1 = bnode2.Position;
                        string str3 = num1.ToString("D11");
                        stringList4.Insert(index2, str3);
                    }
                    else
                    {
                        List <string> stringList4 = stringList3;
                        int           index2      = index1 + 1;
                        num1 = int.MinValue;
                        string str3 = num1.ToString();
                        stringList4.Insert(index2, str3);
                    }
                    for (; index1 < this._degree - 1; ++index1)
                    {
                        stringList1.Insert(index1 + 1, node.Keys[index1]);
                        stringList2.Insert(index1 + 1, node.Data[index1]);
                        stringList3.Insert(index1 + 2, node.Children[index1 + 1]);
                    }
                    int index3 = stringList1.Count % 2 != 0 ? stringList1.Count / 2 : stringList1.Count / 2 - 1;
                    for (int index2 = 0; index2 < node.Keys.Count; ++index2)
                    {
                        if (index2 < index3)
                        {
                            node.Keys[index2]     = stringList1[index2];
                            node.Data[index2]     = stringList2[index2];
                            node.Children[index2] = stringList3[index2];
                        }
                        else
                        {
                            node.Keys[index2] = this._keyNull;
                            node.Data[index2] = this._dataNull;
                            List <string> newChildren = node.Children;
                            int           index4      = index2;
                            num1 = int.MinValue;
                            string str3 = num1.ToString();
                            newChildren[index4] = str3;
                        }
                    }
                    node.Children[index3] = stringList3[index3];
                    if (index3 != node.Children.Count)
                    {
                        for (int index2 = index3 + 1; index2 < node.Children.Count; ++index2)
                        {
                            List <string> newChildren = node.Children;
                            int           index4      = index2;
                            num1 = int.MinValue;
                            string str3 = num1.ToString();
                            newChildren[index4] = str3;
                        }
                    }
                    for (int index2 = 0; index2 < bnode4.Keys.Count; ++index2)
                    {
                        if (this._degree % 2 == 0)
                        {
                            if (index2 <= index3)
                            {
                                bnode4.Keys[index2]     = stringList1[index3 + index2 + 1];
                                bnode4.Data[index2]     = stringList2[index3 + index2 + 1];
                                bnode4.Children[index2] = stringList3[index3 + index2 + 1];
                            }
                        }
                        else if (index2 < index3)
                        {
                            bnode4.Keys[index2]     = stringList1[index3 + index2 + 1];
                            bnode4.Data[index2]     = stringList2[index3 + index2 + 1];
                            bnode4.Children[index2] = stringList3[index3 + index2 + 1];
                        }
                    }
                    bnode4.Children[this.UsedSpaces(bnode4.Keys)] = stringList3[this._degree];
                    for (int index2 = 0; index2 <= this.UsedSpaces(node.Keys); ++index2)
                    {
                        BNode <TKey, T> bnode5 = this._build.BringNode(int.Parse(node.Children[index2]));
                        if (bnode5 != null)
                        {
                            bnode5.Father = node.Position;
                            this._build.SaveNode(bnode5.Information());
                        }
                    }
                    for (int index2 = 0; index2 <= this.UsedSpaces(bnode4.Keys); ++index2)
                    {
                        BNode <TKey, T> bnode5 = this._build.BringNode(int.Parse(bnode4.Children[index2]));
                        if (bnode5 != null)
                        {
                            bnode5.Father = bnode4.Position;
                            this._build.SaveNode(bnode5.Information());
                        }
                    }
                    str1   = stringList1[index3];
                    str2   = stringList2[index3];
                    bnode1 = node;
                    bnode2 = bnode4;
                    this._build.SaveNode(bnode1.Information());
                    this._build.SaveNode(bnode2.Information());
                    node = bnode3;
                }
                else
                {
                    int index1 = 0;
                    if (this.UsedSpaces(node.Keys) > 0)
                    {
                        int num2 = this.UsedSpaces(node.Keys);
                        while (index1 < num2 && str1.CompareTo(node.Keys[index1]) > 0)
                        {
                            ++index1;
                        }
                        for (int index2 = num2; index2 > index1; --index2)
                        {
                            node.Keys[index2] = node.Keys[index2 - 1];
                            node.Data[index2] = node.Data[index2 - 1];
                        }
                        for (int index2 = num2 + 1; index2 > index1; --index2)
                        {
                            node.Children[index2] = node.Children[index2 - 1];
                        }
                    }
                    node.Keys[index1] = str1;
                    node.Data[index1] = str2;
                    if (bnode1 != null)
                    {
                        List <string> newChildren = node.Children;
                        int           index2      = index1;
                        num1 = bnode1.Position;
                        num1 = int.Parse(num1.ToString());
                        string str3 = num1.ToString("D11");
                        newChildren[index2] = str3;
                        bnode1.Father       = node.Position;
                    }
                    else
                    {
                        List <string> newChildren = node.Children;
                        int           index2      = index1;
                        num1 = int.MinValue;
                        string str3 = num1.ToString();
                        newChildren[index2] = str3;
                    }
                    if (bnode2 != null)
                    {
                        List <string> newChildren = node.Children;
                        int           index2      = index1 + 1;
                        num1 = bnode2.Position;
                        num1 = int.Parse(num1.ToString());
                        string str3 = num1.ToString("D11");
                        newChildren[index2] = str3;
                        bnode2.Father       = node.Position;
                    }
                    else
                    {
                        List <string> newChildren = node.Children;
                        int           index2      = index1 + 1;
                        num1 = int.MinValue;
                        string str3 = num1.ToString();
                        newChildren[index2] = str3;
                    }
                    this._build.SaveNode(node.Information());
                    this._build.SaveNode(bnode1.Information());
                    this._build.SaveNode(bnode2.Information());
                    flag = true;
                }
            }while (!flag);
        }